Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions packages/cron/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @lowerdeck/cron

## 1.0.3

### Patch Changes

- add sentry and execution context
- Updated dependencies
- @lowerdeck/queue@1.0.3

## 1.0.2

### Patch Changes
Expand Down
7 changes: 5 additions & 2 deletions packages/cron/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lowerdeck/cron",
"version": "1.0.2",
"version": "1.0.3",
"publishConfig": {
"access": "public"
},
Expand All @@ -24,8 +24,11 @@
"build": "microbundle"
},
"dependencies": {
"@lowerdeck/queue": "^1.0.2",
"@lowerdeck/execution-context": "^1.0.1",
"@lowerdeck/id": "^1.0.4",
"@lowerdeck/queue": "^1.0.3",
"@lowerdeck/redis": "^1.0.2",
"@lowerdeck/sentry": "^1.0.1",
"bullmq": "^5.34.3"
},
"devDependencies": {
Expand Down
29 changes: 27 additions & 2 deletions packages/cron/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { createExecutionContext, provideExecutionContext } from '@lowerdeck/execution-context';
import { generateCustomId } from '@lowerdeck/id';
import { IQueueProcessor } from '@lowerdeck/queue';
import { parseRedisUrl } from '@lowerdeck/redis';
import { getSentry } from '@lowerdeck/sentry';
import { Queue, Worker } from 'bullmq';

let Sentry = getSentry();

let log = (...any: any[]) => console.log('[CRON MANAGER]:', ...any);

let seenNames = new Set<string>();
Expand Down Expand Up @@ -49,8 +54,28 @@ export let createCron = (
let worker = new Worker(
opts.name,
async () => {
log(`Running cron job ${opts.name}`);
await handler();
provideExecutionContext(
createExecutionContext({
type: 'scheduled',
contextId: generateCustomId('cron_'),
cron: opts.cron,
name: opts.name
}),
async () => {
log(`Running cron job ${opts.name}`);

try {
await handler();
} catch (err) {
Sentry.captureException(err, {
tags: {
cronName: opts.name
}
});
throw err;
}
}
);
},
{ connection }
);
Expand Down
6 changes: 6 additions & 0 deletions packages/queue/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @lowerdeck/queue

## 1.0.3

### Patch Changes

- add sentry and execution context

## 1.0.2

### Patch Changes
Expand Down
5 changes: 4 additions & 1 deletion packages/queue/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lowerdeck/queue",
"version": "1.0.2",
"version": "1.0.3",
"publishConfig": {
"access": "public"
},
Expand All @@ -25,8 +25,11 @@
},
"dependencies": {
"@lowerdeck/delay": "^1.0.3",
"@lowerdeck/execution-context": "^1.0.1",
"@lowerdeck/id": "^1.0.4",
"@lowerdeck/memo": "^1.0.3",
"@lowerdeck/redis": "^1.0.2",
"@lowerdeck/sentry": "^1.0.1",
"bullmq": "^5.66.0",
"superjson": "^2.2.6"
},
Expand Down
126 changes: 82 additions & 44 deletions packages/queue/src/drivers/bullmq.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { delay } from '@lowerdeck/delay';
import {
createExecutionContext,
ExecutionContext,
provideExecutionContext,
withExecutionContextOptional
} from '@lowerdeck/execution-context';
import { generateSnowflakeId } from '@lowerdeck/id';
import { memo } from '@lowerdeck/memo';
import { parseRedisUrl } from '@lowerdeck/redis';
import { getSentry } from '@lowerdeck/sentry';
import {
DeduplicationOptions,
JobsOptions,
Expand All @@ -16,6 +24,8 @@ import { IQueue } from '../types';
// @ts-ignore
import SuperJson from 'superjson';

let Sentry = getSentry();

let log = (...any: any[]) => console.log('[QUEUE MANAGER]:', ...any);

let anyQueueStartedRef = { started: false };
Expand Down Expand Up @@ -57,16 +67,20 @@ export let createBullMqQueue = <JobData>(
name: opts.name,

add: async (payload, opts) => {
let job = await queue.add(
'j' as any,
{
payload: SuperJson.serialize(payload)
} as any,
{
delay: opts?.delay,
jobId: opts?.id,
deduplication: opts?.deduplication
}
let job = await withExecutionContextOptional(
async ctx =>
await queue.add(
'j' as any,
{
payload: SuperJson.serialize(payload),
$$execution_context$$: ctx
} as any,
{
delay: opts?.delay,
jobId: opts?.id,
deduplication: opts?.deduplication
}
)
);

return {
Expand All @@ -78,41 +92,47 @@ export let createBullMqQueue = <JobData>(
},

addMany: async (payloads, opts) => {
await queue.addBulk(
payloads.map(
payload =>
({
name: 'j',
data: {
payload: SuperJson.serialize(payload)
},
opts: {
delay: opts?.delay,
jobId: opts?.id,
deduplication: opts?.deduplication
}
}) as any
)
);
await withExecutionContextOptional(async ctx => {
await queue.addBulk(
payloads.map(
payload =>
({
name: 'j',
data: {
payload: SuperJson.serialize(payload),
$$execution_context$$: ctx
},
opts: {
delay: opts?.delay,
jobId: opts?.id,
deduplication: opts?.deduplication
}
}) as any
)
);
});
},

addManyWithOps: async payloads => {
await queue.addBulk(
payloads.map(
payload =>
({
name: 'j',
data: {
payload: SuperJson.serialize(payload.data)
},
opts: {
delay: payload.opts?.delay,
jobId: payload.opts?.id,
deduplication: payload.opts?.deduplication
}
}) as any
)
);
await withExecutionContextOptional(async ctx => {
await queue.addBulk(
payloads.map(
payload =>
({
name: 'j',
data: {
payload: SuperJson.serialize(payload.data),
$$execution_context$$: ctx
},
opts: {
delay: payload.opts?.delay,
jobId: payload.opts?.id,
deduplication: payload.opts?.deduplication
}
}) as any
)
);
});
},

process: cb => {
Expand Down Expand Up @@ -144,13 +164,31 @@ export let createBullMqQueue = <JobData>(
payload = data.payload;
}

await cb(payload as any, job);
let parentExecutionContext = (data as any)
.$$execution_context$$ as ExecutionContext;
while (
parentExecutionContext &&
parentExecutionContext.type == 'job' &&
parentExecutionContext.parent
)
parentExecutionContext = parentExecutionContext.parent;

await provideExecutionContext(
createExecutionContext({
type: 'job',
contextId: job.id ?? generateSnowflakeId(),
queue: opts.name,
parent: parentExecutionContext
}),
() => cb(payload as any, job)
);
} catch (e: any) {
if (e instanceof QueueRetryError) {
await delay(1000);
throw e;
} else {
console.error(`[QUEUE ERROR - ${opts.name}]`, e);
Sentry.captureException(e);
console.error(e);
throw e;
}
}
Expand Down
17 changes: 7 additions & 10 deletions packages/queue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ export let createQueue = <JobData>(opts: { driver?: 'bullmq' } & BullMqCreateOpt
}
seenNames.add(opts.name);

if (opts.driver === 'bullmq') {
return createBullMqQueue<JobData>({
name: opts.name,
jobOpts: opts.jobOpts,
queueOpts: opts.queueOpts,
workerOpts: opts.workerOpts,
redisUrl: opts.redisUrl
});
}
return createBullMqQueue<JobData>({
name: opts.name,
redisUrl: opts.redisUrl,

throw new Error(`Unknown queue driver: ${opts.driver}`);
jobOpts: opts.jobOpts,
queueOpts: opts.queueOpts,
workerOpts: opts.workerOpts
});
};

export let combineQueueProcessors = (opts: IQueueProcessor[]): IQueueProcessor => {
Expand Down
Loading