-
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.
add generic processor with graceful stop and specialized event processor
- Loading branch information
1 parent
83195c1
commit 157dbef
Showing
7 changed files
with
250 additions
and
95 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
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,3 @@ | ||
{ | ||
"watch": ["index.ts", "../../src/processor.ts"] | ||
} |
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
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,55 +1,55 @@ | ||
import { Client } from "pg"; | ||
import { TxOBEvent, TxOBProcessorClient } from "../processor"; | ||
|
||
interface Querier { | ||
query: Client["query"]; | ||
} | ||
|
||
export const createProcessorClient = <EventType extends string>( | ||
querier: Querier, | ||
table: string = "events", | ||
): TxOBProcessorClient<EventType> => ({ | ||
getUnprocessedEvents: async (opts) => { | ||
const events = await querier.query< | ||
Pick<TxOBEvent<EventType>, "id" | "errors"> | ||
>( | ||
`SELECT id, errors FROM ${table} WHERE processed_at IS NULL AND (backoff_until IS NULL OR backoff_until < NOW()) AND errors < $1`, | ||
[opts.maxErrors], | ||
); | ||
return events.rows; | ||
}, | ||
transaction: async (fn) => { | ||
try { | ||
await querier.query("BEGIN"); | ||
await fn({ | ||
getEventByIdForUpdateSkipLocked: async (eventId) => { | ||
const event = await querier.query<TxOBEvent<EventType>>( | ||
`SELECT id, timestamp, type, data, correlation_id, handler_results, errors, backoff_until, processed_at FROM ${table} WHERE id = $1 FOR UPDATE SKIP LOCKED`, | ||
[eventId], | ||
); | ||
if (event.rowCount === 0) { | ||
return null; | ||
} | ||
|
||
return event.rows[0]; | ||
}, | ||
updateEvent: async (event) => { | ||
await querier.query( | ||
`UPDATE ${table} SET handler_results = $1, errors = $2, processed_at = $3, backoff_until = $4 WHERE id = $5`, | ||
[ | ||
event.handler_results, | ||
event.errors, | ||
event.processed_at, | ||
event.backoff_until, | ||
event.id, | ||
], | ||
); | ||
}, | ||
}); | ||
await querier.query("COMMIT"); | ||
} catch (error) { | ||
await querier.query("ROLLBACK").catch(() => {}); | ||
throw error; | ||
} | ||
}, | ||
}); | ||
import { Client } from "pg"; | ||
import { TxOBEvent, TxOBProcessorClient } from "../processor"; | ||
|
||
interface Querier { | ||
query: Client["query"]; | ||
} | ||
|
||
export const createProcessorClient = <EventType extends string>( | ||
querier: Querier, | ||
table: string = "events", | ||
): TxOBProcessorClient<EventType> => ({ | ||
getUnprocessedEvents: async (opts) => { | ||
const events = await querier.query< | ||
Pick<TxOBEvent<EventType>, "id" | "errors"> | ||
>( | ||
`SELECT id, errors FROM ${table} WHERE processed_at IS NULL AND (backoff_until IS NULL OR backoff_until < NOW()) AND errors < $1`, | ||
[opts.maxErrors], | ||
); | ||
return events.rows; | ||
}, | ||
transaction: async (fn) => { | ||
try { | ||
await querier.query("BEGIN"); | ||
await fn({ | ||
getEventByIdForUpdateSkipLocked: async (eventId) => { | ||
const event = await querier.query<TxOBEvent<EventType>>( | ||
`SELECT id, timestamp, type, data, correlation_id, handler_results, errors, backoff_until, processed_at FROM ${table} WHERE id = $1 FOR UPDATE SKIP LOCKED`, | ||
[eventId], | ||
); | ||
if (event.rowCount === 0) { | ||
return null; | ||
} | ||
|
||
return event.rows[0]; | ||
}, | ||
updateEvent: async (event) => { | ||
await querier.query( | ||
`UPDATE ${table} SET handler_results = $1, errors = $2, processed_at = $3, backoff_until = $4 WHERE id = $5`, | ||
[ | ||
event.handler_results, | ||
event.errors, | ||
event.processed_at, | ||
event.backoff_until, | ||
event.id, | ||
], | ||
); | ||
}, | ||
}); | ||
await querier.query("COMMIT"); | ||
} catch (error) { | ||
await querier.query("ROLLBACK").catch(() => {}); | ||
throw error; | ||
} | ||
}, | ||
}); |
Oops, something went wrong.