feat: centralize application framework primitives
This commit is contained in:
1 parent
96e082b943
commit
a3ddd39b7b
73 files changed
+1429
-84
No files matched your search
@@ -54,6 +54,18 @@ export function redisQueueStore(client: RedisQueueClient, prefix = "wrnexus:queu
|
||||
.map((value) => JSON.parse(value) as Job)
|
||||
.filter((job) => !name || job.name === name);
|
||||
},
|
||||
async findByIdempotencyKey(name, idempotencyKey) {
|
||||
const ids = await client.smembers(jobs);
|
||||
const values = await Promise.all(ids.map((id) => client.get(key(id))));
|
||||
const found = values
|
||||
.filter((value): value is string => value !== null)
|
||||
.map((value) => JSON.parse(value) as Job)
|
||||
.find((job) => job.name === name && job.idempotencyKey === idempotencyKey);
|
||||
return found ?? null;
|
||||
},
|
||||
async size() {
|
||||
return (await client.smembers(jobs)).length;
|
||||
},
|
||||
async claim(id, worker, leaseUntil) {
|
||||
const ttl = Math.max(1, leaseUntil - Date.now());
|
||||
return Boolean(await client.set(`${prefix}:lease:${id}`, worker, { NX: true, PX: ttl }));
|
||||
@@ -98,6 +110,19 @@ export function postgresQueueStore(db: SqlQueueClient, table = "wrnexus_jobs"):
|
||||
);
|
||||
return result.rows.map((row) => row.payload);
|
||||
},
|
||||
async findByIdempotencyKey(name, key) {
|
||||
const result = await db.query<{ payload: Job }>(
|
||||
`SELECT payload FROM ${table} WHERE name=$1 AND payload->>'idempotencyKey'=$2 LIMIT 1`,
|
||||
[name, key],
|
||||
);
|
||||
return result.rows[0]?.payload ?? null;
|
||||
},
|
||||
async size() {
|
||||
const result = await db.query<{ total: number | string }>(
|
||||
`SELECT COUNT(*) AS total FROM ${table}`,
|
||||
);
|
||||
return Number(result.rows[0]?.total ?? 0);
|
||||
},
|
||||
async claim(id, worker, leaseUntil) {
|
||||
const result = await db.query(
|
||||
`UPDATE ${table} SET lease_owner=$2,lease_until=$3 WHERE id=$1 AND (lease_until IS NULL OR lease_until < $4) RETURNING id`,
|
||||
|
||||
Reference in new issue
Block a user