@lowerdeck/queue 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowerdeck/queue",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -25,8 +25,11 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@lowerdeck/delay": "^1.0.3",
28
+ "@lowerdeck/execution-context": "^1.0.1",
29
+ "@lowerdeck/id": "^1.0.4",
28
30
  "@lowerdeck/memo": "^1.0.3",
29
31
  "@lowerdeck/redis": "^1.0.2",
32
+ "@lowerdeck/sentry": "^1.0.1",
30
33
  "bullmq": "^5.66.0",
31
34
  "superjson": "^2.2.6"
32
35
  },
@@ -1,6 +1,14 @@
1
1
  import { delay } from '@lowerdeck/delay';
2
+ import {
3
+ createExecutionContext,
4
+ ExecutionContext,
5
+ provideExecutionContext,
6
+ withExecutionContextOptional
7
+ } from '@lowerdeck/execution-context';
8
+ import { generateSnowflakeId } from '@lowerdeck/id';
2
9
  import { memo } from '@lowerdeck/memo';
3
10
  import { parseRedisUrl } from '@lowerdeck/redis';
11
+ import { getSentry } from '@lowerdeck/sentry';
4
12
  import {
5
13
  DeduplicationOptions,
6
14
  JobsOptions,
@@ -16,6 +24,8 @@ import { IQueue } from '../types';
16
24
  // @ts-ignore
17
25
  import SuperJson from 'superjson';
18
26
 
27
+ let Sentry = getSentry();
28
+
19
29
  let log = (...any: any[]) => console.log('[QUEUE MANAGER]:', ...any);
20
30
 
21
31
  let anyQueueStartedRef = { started: false };
@@ -57,16 +67,20 @@ export let createBullMqQueue = <JobData>(
57
67
  name: opts.name,
58
68
 
59
69
  add: async (payload, opts) => {
60
- let job = await queue.add(
61
- 'j' as any,
62
- {
63
- payload: SuperJson.serialize(payload)
64
- } as any,
65
- {
66
- delay: opts?.delay,
67
- jobId: opts?.id,
68
- deduplication: opts?.deduplication
69
- }
70
+ let job = await withExecutionContextOptional(
71
+ async ctx =>
72
+ await queue.add(
73
+ 'j' as any,
74
+ {
75
+ payload: SuperJson.serialize(payload),
76
+ $$execution_context$$: ctx
77
+ } as any,
78
+ {
79
+ delay: opts?.delay,
80
+ jobId: opts?.id,
81
+ deduplication: opts?.deduplication
82
+ }
83
+ )
70
84
  );
71
85
 
72
86
  return {
@@ -78,41 +92,47 @@ export let createBullMqQueue = <JobData>(
78
92
  },
79
93
 
80
94
  addMany: async (payloads, opts) => {
81
- await queue.addBulk(
82
- payloads.map(
83
- payload =>
84
- ({
85
- name: 'j',
86
- data: {
87
- payload: SuperJson.serialize(payload)
88
- },
89
- opts: {
90
- delay: opts?.delay,
91
- jobId: opts?.id,
92
- deduplication: opts?.deduplication
93
- }
94
- }) as any
95
- )
96
- );
95
+ await withExecutionContextOptional(async ctx => {
96
+ await queue.addBulk(
97
+ payloads.map(
98
+ payload =>
99
+ ({
100
+ name: 'j',
101
+ data: {
102
+ payload: SuperJson.serialize(payload),
103
+ $$execution_context$$: ctx
104
+ },
105
+ opts: {
106
+ delay: opts?.delay,
107
+ jobId: opts?.id,
108
+ deduplication: opts?.deduplication
109
+ }
110
+ }) as any
111
+ )
112
+ );
113
+ });
97
114
  },
98
115
 
99
116
  addManyWithOps: async payloads => {
100
- await queue.addBulk(
101
- payloads.map(
102
- payload =>
103
- ({
104
- name: 'j',
105
- data: {
106
- payload: SuperJson.serialize(payload.data)
107
- },
108
- opts: {
109
- delay: payload.opts?.delay,
110
- jobId: payload.opts?.id,
111
- deduplication: payload.opts?.deduplication
112
- }
113
- }) as any
114
- )
115
- );
117
+ await withExecutionContextOptional(async ctx => {
118
+ await queue.addBulk(
119
+ payloads.map(
120
+ payload =>
121
+ ({
122
+ name: 'j',
123
+ data: {
124
+ payload: SuperJson.serialize(payload.data),
125
+ $$execution_context$$: ctx
126
+ },
127
+ opts: {
128
+ delay: payload.opts?.delay,
129
+ jobId: payload.opts?.id,
130
+ deduplication: payload.opts?.deduplication
131
+ }
132
+ }) as any
133
+ )
134
+ );
135
+ });
116
136
  },
117
137
 
118
138
  process: cb => {
@@ -144,13 +164,31 @@ export let createBullMqQueue = <JobData>(
144
164
  payload = data.payload;
145
165
  }
146
166
 
147
- await cb(payload as any, job);
167
+ let parentExecutionContext = (data as any)
168
+ .$$execution_context$$ as ExecutionContext;
169
+ while (
170
+ parentExecutionContext &&
171
+ parentExecutionContext.type == 'job' &&
172
+ parentExecutionContext.parent
173
+ )
174
+ parentExecutionContext = parentExecutionContext.parent;
175
+
176
+ await provideExecutionContext(
177
+ createExecutionContext({
178
+ type: 'job',
179
+ contextId: job.id ?? generateSnowflakeId(),
180
+ queue: opts.name,
181
+ parent: parentExecutionContext
182
+ }),
183
+ () => cb(payload as any, job)
184
+ );
148
185
  } catch (e: any) {
149
186
  if (e instanceof QueueRetryError) {
150
187
  await delay(1000);
151
188
  throw e;
152
189
  } else {
153
- console.error(`[QUEUE ERROR - ${opts.name}]`, e);
190
+ Sentry.captureException(e);
191
+ console.error(e);
154
192
  throw e;
155
193
  }
156
194
  }
package/src/index.ts CHANGED
@@ -14,17 +14,14 @@ export let createQueue = <JobData>(opts: { driver?: 'bullmq' } & BullMqCreateOpt
14
14
  }
15
15
  seenNames.add(opts.name);
16
16
 
17
- if (opts.driver === 'bullmq') {
18
- return createBullMqQueue<JobData>({
19
- name: opts.name,
20
- jobOpts: opts.jobOpts,
21
- queueOpts: opts.queueOpts,
22
- workerOpts: opts.workerOpts,
23
- redisUrl: opts.redisUrl
24
- });
25
- }
17
+ return createBullMqQueue<JobData>({
18
+ name: opts.name,
19
+ redisUrl: opts.redisUrl,
26
20
 
27
- throw new Error(`Unknown queue driver: ${opts.driver}`);
21
+ jobOpts: opts.jobOpts,
22
+ queueOpts: opts.queueOpts,
23
+ workerOpts: opts.workerOpts
24
+ });
28
25
  };
29
26
 
30
27
  export let combineQueueProcessors = (opts: IQueueProcessor[]): IQueueProcessor => {