Skip to content

Commit

Permalink
feat(locksmith): Less concurrency (#15391)
Browse files Browse the repository at this point in the history
* changing indices on process hook items

* even less concurrency

* fixed
  • Loading branch information
julien51 committed Jan 22, 2025
1 parent 3e8d21d commit 2d886aa
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 52 deletions.
5 changes: 1 addition & 4 deletions locksmith/src/worker/helpers/renewal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { networks } from '@unlock-protocol/networks'
import { isProduction } from '../../config/config'

export async function runRenewal(fn: (network: number) => Promise<void>) {
const tasks: Promise<void>[] = []
for (const network of Object.values(networks)) {
// Don't run jobs on test networks in production
if (isProduction && network.isTestNetwork) {
Expand All @@ -11,8 +10,6 @@ export async function runRenewal(fn: (network: number) => Promise<void>) {
if (network.id === 31337) {
continue
}
const task = fn(network.id)
tasks.push(task)
await fn(network.id)
}
await Promise.allSettled(tasks)
}
18 changes: 7 additions & 11 deletions locksmith/src/worker/jobs/expiredKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,21 @@ export const notifyExpiredKeysForLock = async (keys: any[]) => {
hooks,
TOPIC_EXPIRED_KEYS_ON_NETWORK
)

const events = await Promise.allSettled(
expiredKeysHook.map(async (hook) => {
const data = keys.filter((key: any) => key.lock.id === hook.lock)
const hookEvent = await notifyHook(hook, {
data,
network: hook.network,
})
return hookEvent
for (let i = 0; i < expiredKeysHook.length; i++) {
const hook = expiredKeysHook[i]
const data = keys.filter((key: any) => key.lock.id === hook.lock)
await notifyHook(hook, {
data,
network: hook.network,
})
)
}
const items = keys.map((key: any) => ({
type: 'expired-keys',
objectId: key.id,
network: key.lock.network,
}))

await ProcessedHookItem.bulkCreate(items)
return events
}

export const notifyExpiredKeysForNetwork: Task = async () => {
Expand Down
42 changes: 20 additions & 22 deletions locksmith/src/worker/jobs/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,26 @@ async function notifyHooksOfAllUnprocessedKeys(hooks: Hook[], network: number) {
keys: keys.map((key: any) => [network, key.lock.address, key.id]),
})

await Promise.allSettled([
notifyNewKeysToWedlocks(keys, network), // send emails when applicable!
// Send notification to hooks subscribed to keys on a specific lock address
...keysOnLockHooks.map(async (keysOnLockHook) => {
const data = keys.filter(
(key: any) => key.lock.id === keysOnLockHook.lock
)
const hookEvent = await notifyHook(keysOnLockHook, {
data,
network,
})
return hookEvent
}),
// Send notification to hooks subscribed to keys on a whole network
...keysOnNetworkHooks.map(async (keysOnNetworkHook) => {
const hookEvent = await notifyHook(keysOnNetworkHook, {
network,
data: keys,
})
return hookEvent
}),
])
await notifyNewKeysToWedlocks(keys, network) // send emails when applicable!

for (let i = 0; i < keysOnLockHooks.length; i++) {
const keysOnLockHook = keysOnLockHooks[i]
const data = keys.filter(
(key: any) => key.lock.id === keysOnLockHook.lock
)
await notifyHook(keysOnLockHook, {
data,
network,
})
}

for (let i = 0; i < keysOnNetworkHooks.length; i++) {
const keysOnNetworkHook = keysOnNetworkHooks[i]
await notifyHook(keysOnNetworkHook, {
network,
data: keys,
})
}

const processedHookItems = keys.map((key: any) => {
return {
Expand Down
25 changes: 10 additions & 15 deletions locksmith/src/worker/jobs/locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,13 @@ async function notifyHooksOfNewLocks(
locks: any[],
network: number
) {
return Promise.all(
hooks.map(async (hook) => {
return notifyHook(hook, {
data: locks,
network,
})
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i]
await notifyHook(hook, {
data: locks,
network,
})
)
}
}

/**
Expand Down Expand Up @@ -187,10 +186,8 @@ async function markLocksAsProcessed(locks: any[], network: number) {
* @param network - Network ID where locks exist
*/
async function finalizeLockProcessing(locks: any[], network: number) {
await Promise.all([
updatePendingEvents(locks, network),
markLocksAsProcessed(locks, network),
])
await updatePendingEvents(locks, network)
await markLocksAsProcessed(locks, network)
}

/**
Expand All @@ -202,10 +199,8 @@ async function finalizeLockProcessing(locks: any[], network: number) {
* @param network - Network ID where locks were found
*/
async function processLockBatch(hooks: Hook[], locks: any[], network: number) {
await Promise.all([
notifyHooksOfNewLocks(hooks, locks, network),
finalizeLockProcessing(locks, network),
])
await notifyHooksOfNewLocks(hooks, locks, network)
await finalizeLockProcessing(locks, network)
}

/**
Expand Down

0 comments on commit 2d886aa

Please sign in to comment.