-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Env } from '..'; | ||
import { bulkUpdateStatus, getEventsToBeScheduled } from '../db/data-layer'; | ||
import { chunkArray, groupBy } from '../utils/array'; | ||
|
||
export async function scheduleAllPendingEvents(env: Env) { | ||
const queuesBySlug = new Map<string, Queue>(); | ||
queuesBySlug.set('OUTBOUND_QUEUE', env.OUTBOUND_QUEUE); | ||
|
||
for await (const events of getEventsToBeScheduled(env)) { | ||
const eventsByQueue = groupBy(events, (e) => e.queueSlug); | ||
for (const [queueSlug, queueEvents] of eventsByQueue) { | ||
const queue = queuesBySlug.get(queueSlug); | ||
if (!queue) { | ||
throw new Error(`Queue not found for slug: ${queueSlug}`); | ||
} | ||
const chunks = chunkArray(queueEvents, 100); | ||
for (const chunk of chunks) { | ||
await queue.sendBatch( | ||
chunk.map((e) => ({ body: JSON.parse(e.data) as unknown, contentType: 'json' })) | ||
); | ||
await bulkUpdateStatus( | ||
chunk.map((e) => e.id), | ||
'SUCCEEDED', | ||
env | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
import { scheduleAllPendingEvents } from './business-logic/schedule-events'; | ||
import { app } from './routes'; | ||
|
||
export type Env = { | ||
ENVIRONMENT: 'testing' | 'development' | 'production'; | ||
__D1_BETA_DB: D1Database; | ||
DB: D1Database; | ||
OUTBOUND_QUEUE: Queue; | ||
}; | ||
|
||
export default { | ||
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { | ||
return app.fetch(request, env, ctx); | ||
}, | ||
|
||
async scheduled(_event: ScheduledEvent, env: Env, _ctx: ExecutionContext): Promise<void> { | ||
await scheduleAllPendingEvents(env); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Splits an array into chunks of the specified size. | ||
* | ||
* @param {Array<T>} array The array to split. | ||
* @param {number} chunkSize The size of each chunk. | ||
* @returns {Array<Array<T>>} An array of chunks. | ||
*/ | ||
export function chunkArray<T>(array: T[], chunkSize: number): T[][] { | ||
if (chunkSize < 1) { | ||
throw new Error(`chunkSize must be greater than 0, got ${chunkSize}`); | ||
} | ||
const chunkedArray: T[][] = []; | ||
for (let i = 0; i < array.length; i += chunkSize) { | ||
chunkedArray.push(array.slice(i, i + chunkSize)); | ||
} | ||
return chunkedArray; | ||
} | ||
|
||
/** | ||
* Groups an array of items by a callback. | ||
* | ||
* @param {Array<T>} array The array to group. | ||
* @param {GroupByCallback<T>} callback The callback to group by. | ||
* @returns {Map<string, T[]>} A map of groups. | ||
*/ | ||
export function groupBy<T>(array: T[], callback: GroupByCallback<T>): Map<string, T[]> { | ||
const map = new Map<string, T[]>(); | ||
|
||
for (const item of array) { | ||
const key = callback(item); | ||
const collection = map.get(key); | ||
if (!collection) { | ||
map.set(key, [item]); | ||
} else { | ||
collection.push(item); | ||
} | ||
} | ||
|
||
return map; | ||
} | ||
type GroupByCallback<T> = (item: T) => string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters