@quantumbpm/sdk 1.2.0 → 1.3.0
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/README.md +38 -2
- package/dist/bpmn.d.ts +4 -2
- package/dist/bpmn.js +4 -4
- package/dist/generated/RawClient.js +2 -2
- package/dist/generated/core/OpenAPI.js +2 -2
- package/dist/generated/index.d.ts +1 -0
- package/dist/generated/index.js +1 -1
- package/dist/generated/models/BpmnIncident.d.ts +6 -0
- package/dist/generated/models/BpmnIncident.js +5 -1
- package/dist/generated/models/BpmnIncidentRecord.d.ts +4 -6
- package/dist/generated/models/BpmnIncidentRecord.js +3 -1
- package/dist/generated/models/BpmnInstance.d.ts +7 -0
- package/dist/generated/models/BpmnInstance.js +1 -1
- package/dist/generated/models/BpmnInstanceState.d.ts +18 -2
- package/dist/generated/models/BpmnInstanceState.js +1 -1
- package/dist/generated/models/CorrelationKeyNumber.d.ts +1 -1
- package/dist/generated/models/CorrelationKeyNumber.js +1 -1
- package/dist/generated/models/Error.d.ts +6 -0
- package/dist/generated/models/Error.js +5 -1
- package/dist/generated/models/EvaluateStoredRequest.d.ts +4 -1
- package/dist/generated/models/EvaluateStoredRequest.js +1 -1
- package/dist/generated/models/ExternalJob.d.ts +1 -1
- package/dist/generated/models/ExternalJob.js +1 -1
- package/dist/generated/models/ExternalJobBatchResponse.d.ts +2 -2
- package/dist/generated/models/ExternalJobBatchResponse.js +1 -1
- package/dist/generated/models/JoinArrivals.d.ts +8 -0
- package/dist/generated/models/JoinArrivals.js +3 -0
- package/dist/generated/models/PollBpmnJobRequest.d.ts +1 -1
- package/dist/generated/models/PollBpmnJobRequest.js +1 -1
- package/dist/generated/models/SuspensionEntry.d.ts +2 -2
- package/dist/generated/models/SuspensionEntry.js +1 -1
- package/dist/generated/models/UpdateUserTaskAssignmentRequest.d.ts +11 -4
- package/dist/generated/models/UpdateUserTaskAssignmentRequest.js +1 -1
- package/dist/generated/services/BpmnService.d.ts +18 -17
- package/dist/generated/services/BpmnService.js +41 -6
- package/dist/generated/services/DefaultService.d.ts +21 -14
- package/dist/generated/services/DefaultService.js +45 -12
- package/dist/workers.d.ts +39 -5
- package/dist/workers.js +184 -52
- package/package.json +1 -1
- package/src/bpmn.ts +7 -4
- package/src/generated/RawClient.ts +1 -1
- package/src/generated/core/OpenAPI.ts +1 -1
- package/src/generated/index.ts +1 -0
- package/src/generated/models/BpmnIncident.ts +6 -0
- package/src/generated/models/BpmnIncidentRecord.ts +4 -6
- package/src/generated/models/BpmnInstance.ts +7 -0
- package/src/generated/models/BpmnInstanceState.ts +18 -2
- package/src/generated/models/CorrelationKeyNumber.ts +1 -1
- package/src/generated/models/Error.ts +6 -0
- package/src/generated/models/EvaluateStoredRequest.ts +4 -1
- package/src/generated/models/ExternalJob.ts +1 -1
- package/src/generated/models/ExternalJobBatchResponse.ts +2 -2
- package/src/generated/models/JoinArrivals.ts +13 -0
- package/src/generated/models/PollBpmnJobRequest.ts +1 -1
- package/src/generated/models/SuspensionEntry.ts +2 -2
- package/src/generated/models/UpdateUserTaskAssignmentRequest.ts +11 -4
- package/src/generated/services/BpmnService.ts +51 -17
- package/src/generated/services/DefaultService.ts +49 -15
- package/src/index.test.ts +3 -0
- package/src/workers.test.ts +279 -1
- package/src/workers.ts +196 -57
package/src/workers.test.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { Worker, Job } from './workers';
|
|
1
|
+
import { Worker, Job, WorkerConfig, heartbeatIntervalMs, parseDurationMs } from './workers';
|
|
2
2
|
import { RawClient } from './generated/RawClient';
|
|
3
|
+
import { ApiError } from './generated/core/ApiError';
|
|
4
|
+
import { CancelablePromise } from './generated/core/CancelablePromise';
|
|
3
5
|
import { Vars } from './variables';
|
|
4
6
|
import type { ExternalJob } from './generated';
|
|
5
7
|
|
|
@@ -115,3 +117,279 @@ describe('Worker job dispatch', () => {
|
|
|
115
117
|
expect(received!.businessId).toBeUndefined();
|
|
116
118
|
});
|
|
117
119
|
});
|
|
120
|
+
|
|
121
|
+
describe('Worker clientId', () => {
|
|
122
|
+
const fakeRaw = {} as unknown as RawClient;
|
|
123
|
+
const projectId = '00000000-0000-0000-0000-000000000001';
|
|
124
|
+
|
|
125
|
+
test('keeps a configured clientId', () => {
|
|
126
|
+
const w = new Worker(fakeRaw, projectId, { clientId: 'billing-1' });
|
|
127
|
+
expect((w as any).clientId).toBe('billing-1');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// The server refuses a poll with an empty clientID, so blank falls back.
|
|
131
|
+
test.each(['', ' '])('falls back to a generated id for %j', (blank) => {
|
|
132
|
+
const w = new Worker(fakeRaw, projectId, { clientId: blank });
|
|
133
|
+
expect((w as any).clientId).toMatch(/^worker-.+-\d+$/);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
describe('parseDurationMs', () => {
|
|
138
|
+
test.each([
|
|
139
|
+
['30s', 30_000],
|
|
140
|
+
['12.5s', 12_500],
|
|
141
|
+
['1m30s', 90_000],
|
|
142
|
+
['1h2m3.5s', 3_723_500],
|
|
143
|
+
['500ms', 500],
|
|
144
|
+
['1.5h', 5_400_000],
|
|
145
|
+
['.5s', 500],
|
|
146
|
+
['5.s', 5_000],
|
|
147
|
+
['1500us', 1.5],
|
|
148
|
+
['1500µs', 1.5],
|
|
149
|
+
['2000000ns', 2],
|
|
150
|
+
['0', 0],
|
|
151
|
+
['+1s', 1_000],
|
|
152
|
+
['-1s', -1_000],
|
|
153
|
+
[' 30s', 30_000],
|
|
154
|
+
['30s ', 30_000],
|
|
155
|
+
])('parses %j', (input, ms) => {
|
|
156
|
+
expect(parseDurationMs(input)).toBeCloseTo(ms);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test.each(['', '30', '12.5', 's', '.s', '1.2.3s', '1m30', '30 s', '1d', '5sec', '1,5s', 'soon'])(
|
|
160
|
+
'rejects %j',
|
|
161
|
+
(input) => {
|
|
162
|
+
expect(parseDurationMs(input)).toBeNull();
|
|
163
|
+
},
|
|
164
|
+
);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe('heartbeatIntervalMs', () => {
|
|
168
|
+
const now = Date.parse('2026-10-21T10:00:00Z');
|
|
169
|
+
const at = (ms: number) => new Date(now + ms).toISOString();
|
|
170
|
+
|
|
171
|
+
test('uses the granted lock when the server clamped the request down', () => {
|
|
172
|
+
expect(heartbeatIntervalMs(660_000, at(300_000), now)).toBe(150_000);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test('uses the requested lock when it is shorter than the grant', () => {
|
|
176
|
+
expect(heartbeatIntervalMs(5_000, at(10_000), now)).toBe(2_500);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('handles a fractional lock', () => {
|
|
180
|
+
expect(heartbeatIntervalMs(12_500, at(12_500), now)).toBe(6_250);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test.each([
|
|
184
|
+
['missing', undefined],
|
|
185
|
+
['in the past', '2026-10-21T09:59:00Z'],
|
|
186
|
+
['unparseable', 'later'],
|
|
187
|
+
])('falls back to the requested lock when lockExpiresAt is %s', (_name, expiresAt) => {
|
|
188
|
+
expect(heartbeatIntervalMs(30_000, expiresAt, now)).toBe(15_000);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test('never goes below one second', () => {
|
|
192
|
+
expect(heartbeatIntervalMs(500, at(10_000), now)).toBe(1_000);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe('Worker duration validation', () => {
|
|
197
|
+
const fakeRaw = {} as unknown as RawClient;
|
|
198
|
+
const projectId = '00000000-0000-0000-0000-000000000001';
|
|
199
|
+
const handler = async () => new Vars();
|
|
200
|
+
|
|
201
|
+
test('handle accepts fractional and compound durations', () => {
|
|
202
|
+
const w = new Worker(fakeRaw, projectId);
|
|
203
|
+
w.handle('payment', handler, { lockDuration: '12.5s', pollTimeout: '1m30s' });
|
|
204
|
+
expect((w as any).registrations.get('payment').lockMs).toBe(12_500);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test('handle trims the durations it sends', () => {
|
|
208
|
+
const w = new Worker(fakeRaw, projectId);
|
|
209
|
+
w.handle('payment', handler, { lockDuration: ' 10s ', pollTimeout: '5s ' });
|
|
210
|
+
const reg = (w as any).registrations.get('payment');
|
|
211
|
+
expect([reg.options.lockDuration, reg.options.pollTimeout, reg.lockMs]).toEqual(['10s', '5s', 10_000]);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test('handle throws on an invalid lockDuration', () => {
|
|
215
|
+
const w = new Worker(fakeRaw, projectId);
|
|
216
|
+
expect(() => w.handle('payment', handler, { lockDuration: '12.5x' })).toThrow(/invalid lockDuration "12.5x"/);
|
|
217
|
+
expect((w as any).registrations.size).toBe(0);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test('handle throws on an invalid pollTimeout', () => {
|
|
221
|
+
const w = new Worker(fakeRaw, projectId);
|
|
222
|
+
expect(() => w.handle('payment', handler, { pollTimeout: 'soon' })).toThrow(/invalid pollTimeout "soon"/);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test('the constructor throws on an invalid shutdownTimeout', () => {
|
|
226
|
+
expect(() => new Worker(fakeRaw, projectId, { shutdownTimeout: '20 seconds' }))
|
|
227
|
+
.toThrow(/invalid shutdownTimeout "20 seconds"/);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
describe('Worker locks and shutdown', () => {
|
|
232
|
+
beforeEach(() => jest.useFakeTimers());
|
|
233
|
+
afterEach(() => jest.useRealTimers());
|
|
234
|
+
|
|
235
|
+
const job = {
|
|
236
|
+
executionKey: 'wf-1:node-a:1',
|
|
237
|
+
workflowID: 'wf-1',
|
|
238
|
+
taskType: 'payment',
|
|
239
|
+
} as unknown as ExternalJob;
|
|
240
|
+
|
|
241
|
+
function apiError(status: number, body?: unknown): ApiError {
|
|
242
|
+
return new ApiError(
|
|
243
|
+
{ method: 'POST', url: '' },
|
|
244
|
+
{ url: '', ok: false, status, statusText: '', body },
|
|
245
|
+
`status ${status}`,
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// The first poll hands out one job. Later polls hang until cancelled.
|
|
250
|
+
function setup(config: WorkerConfig = {}, polled: ExternalJob = job) {
|
|
251
|
+
let polls = 0;
|
|
252
|
+
const calls = {
|
|
253
|
+
poll: jest.fn(() => new CancelablePromise<ExternalJob[]>((resolve) => {
|
|
254
|
+
if (polls++ === 0) resolve([polled]);
|
|
255
|
+
})),
|
|
256
|
+
heartbeat: jest.fn(async (..._args: unknown[]) => {}),
|
|
257
|
+
complete: jest.fn(async (..._args: unknown[]) => {}),
|
|
258
|
+
throwError: jest.fn(async (..._args: unknown[]) => {}),
|
|
259
|
+
};
|
|
260
|
+
const raw = {
|
|
261
|
+
default: {
|
|
262
|
+
pollBpmnExternalJobs: calls.poll,
|
|
263
|
+
heartbeatBpmnExternalJob: calls.heartbeat,
|
|
264
|
+
completeBpmnExternalJob: calls.complete,
|
|
265
|
+
},
|
|
266
|
+
bpmn: { throwBpmnExternalJobError: calls.throwError },
|
|
267
|
+
} as unknown as RawClient;
|
|
268
|
+
const warnings: string[] = [];
|
|
269
|
+
const errors: string[] = [];
|
|
270
|
+
const w = new Worker(raw, '00000000-0000-0000-0000-000000000001', {
|
|
271
|
+
logger: {
|
|
272
|
+
error: (msg: string) => { errors.push(msg); },
|
|
273
|
+
warn: (msg: string) => { warnings.push(msg); },
|
|
274
|
+
},
|
|
275
|
+
...config,
|
|
276
|
+
});
|
|
277
|
+
return { w, calls, warnings, errors };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
test('a heartbeat 409 aborts the handler signal and sends no complete', async () => {
|
|
281
|
+
const { w, calls, warnings } = setup();
|
|
282
|
+
calls.heartbeat.mockRejectedValue(apiError(409));
|
|
283
|
+
let signal: AbortSignal | undefined;
|
|
284
|
+
w.handle('payment', (_job, s) => {
|
|
285
|
+
signal = s;
|
|
286
|
+
return new Promise((_resolve, reject) => {
|
|
287
|
+
s.addEventListener('abort', () => reject(new Error('aborted')));
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
const stop = new AbortController();
|
|
292
|
+
const running = w.run(stop.signal);
|
|
293
|
+
await jest.advanceTimersByTimeAsync(15_000);
|
|
294
|
+
expect(signal?.aborted).toBe(true);
|
|
295
|
+
|
|
296
|
+
await jest.advanceTimersByTimeAsync(60_000);
|
|
297
|
+
stop.abort();
|
|
298
|
+
await running;
|
|
299
|
+
|
|
300
|
+
expect(calls.heartbeat).toHaveBeenCalledTimes(1);
|
|
301
|
+
expect(calls.complete).not.toHaveBeenCalled();
|
|
302
|
+
expect(calls.throwError).not.toHaveBeenCalled();
|
|
303
|
+
expect(warnings.filter(m => m.includes('this worker no longer holds the job, aborting the handler'))).toHaveLength(1);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
test('heartbeats at half of the lock the server granted', async () => {
|
|
307
|
+
const clamped = { ...job, lockExpiresAt: new Date(Date.now() + 300_000).toISOString() } as ExternalJob;
|
|
308
|
+
const { w, calls } = setup({}, clamped);
|
|
309
|
+
let finish: (v: Vars) => void = () => {};
|
|
310
|
+
w.handle('payment', () => new Promise<Vars>((resolve) => { finish = resolve; }), { lockDuration: '11m' });
|
|
311
|
+
|
|
312
|
+
const stop = new AbortController();
|
|
313
|
+
const running = w.run(stop.signal);
|
|
314
|
+
await jest.advanceTimersByTimeAsync(149_999);
|
|
315
|
+
expect(calls.heartbeat).not.toHaveBeenCalled();
|
|
316
|
+
await jest.advanceTimersByTimeAsync(1);
|
|
317
|
+
expect(calls.heartbeat).toHaveBeenCalledTimes(1);
|
|
318
|
+
expect(calls.heartbeat.mock.calls[0][2]).toMatchObject({ lockDuration: '11m' });
|
|
319
|
+
|
|
320
|
+
stop.abort();
|
|
321
|
+
finish(new Vars());
|
|
322
|
+
await running;
|
|
323
|
+
expect(calls.complete).toHaveBeenCalledTimes(1);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test('after shutdown starts, a running handler keeps heartbeating and completes', async () => {
|
|
327
|
+
const { w, calls } = setup();
|
|
328
|
+
let signal: AbortSignal | undefined;
|
|
329
|
+
let finish: (v: Vars) => void = () => {};
|
|
330
|
+
w.handle('payment', (_job, s) => {
|
|
331
|
+
signal = s;
|
|
332
|
+
return new Promise<Vars>((resolve) => { finish = resolve; });
|
|
333
|
+
}, { maxJobs: 2, lockDuration: '10s' });
|
|
334
|
+
|
|
335
|
+
const stop = new AbortController();
|
|
336
|
+
const running = w.run(stop.signal);
|
|
337
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
338
|
+
expect(signal).toBeDefined();
|
|
339
|
+
// The second poll is the in-flight long-poll that the abort cancels.
|
|
340
|
+
expect(calls.poll).toHaveBeenCalledTimes(2);
|
|
341
|
+
|
|
342
|
+
stop.abort();
|
|
343
|
+
await jest.advanceTimersByTimeAsync(15_000);
|
|
344
|
+
expect(calls.heartbeat).toHaveBeenCalledTimes(3);
|
|
345
|
+
expect(signal!.aborted).toBe(false);
|
|
346
|
+
|
|
347
|
+
finish(new Vars().set('ok', true));
|
|
348
|
+
await running;
|
|
349
|
+
|
|
350
|
+
expect(calls.poll).toHaveBeenCalledTimes(2);
|
|
351
|
+
expect(calls.complete).toHaveBeenCalledTimes(1);
|
|
352
|
+
expect(calls.complete.mock.calls[0][1]).toBe('wf-1:node-a:1');
|
|
353
|
+
expect(calls.complete.mock.calls[0][2]).toMatchObject({ variables: { ok: true } });
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test('the shutdown timeout aborts a handler that does not finish and run resolves', async () => {
|
|
357
|
+
const { w, calls, warnings } = setup({ shutdownTimeout: '5s' });
|
|
358
|
+
let signal: AbortSignal | undefined;
|
|
359
|
+
w.handle('payment', (_job, s) => {
|
|
360
|
+
signal = s;
|
|
361
|
+
return new Promise<Vars>(() => {});
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
const stop = new AbortController();
|
|
365
|
+
let resolved = false;
|
|
366
|
+
const running = w.run(stop.signal).then(() => { resolved = true; });
|
|
367
|
+
await jest.advanceTimersByTimeAsync(0);
|
|
368
|
+
stop.abort();
|
|
369
|
+
|
|
370
|
+
await jest.advanceTimersByTimeAsync(4_999);
|
|
371
|
+
expect(resolved).toBe(false);
|
|
372
|
+
expect(signal!.aborted).toBe(false);
|
|
373
|
+
|
|
374
|
+
await jest.advanceTimersByTimeAsync(1);
|
|
375
|
+
await running;
|
|
376
|
+
expect(signal!.aborted).toBe(true);
|
|
377
|
+
expect(calls.complete).not.toHaveBeenCalled();
|
|
378
|
+
expect(calls.throwError).not.toHaveBeenCalled();
|
|
379
|
+
expect(warnings.some(m => m.includes('shutdown timeout'))).toBe(true);
|
|
380
|
+
// Heartbeat and shutdown timers are all cleared.
|
|
381
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
test('a complete 409 is logged as a discarded result', async () => {
|
|
385
|
+
const { w, calls, warnings, errors } = setup();
|
|
386
|
+
calls.complete.mockRejectedValue(apiError(409, { code: 'CONFLICT', message: 'the job was canceled' }));
|
|
387
|
+
w.handle('payment', async () => new Vars());
|
|
388
|
+
|
|
389
|
+
const registration = (w as any).registrations.get('payment');
|
|
390
|
+
await (w as any).dispatch(registration, job, new AbortController().signal);
|
|
391
|
+
|
|
392
|
+
expect(warnings).toContain('workers: complete wf-1:node-a:1: result discarded: the job was canceled');
|
|
393
|
+
expect(errors).toHaveLength(0);
|
|
394
|
+
});
|
|
395
|
+
});
|