@contember/engine-common 2.2.0-alpha.2 → 2.2.0-alpha.21

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.
Files changed (67) hide show
  1. package/dist/development/index.cjs +7 -0
  2. package/dist/development/index.cjs.map +1 -1
  3. package/dist/development/index.js +8 -1
  4. package/dist/development/index.js.map +1 -1
  5. package/dist/development/src/egress/EgressGuard.cjs +170 -0
  6. package/dist/development/src/egress/EgressGuard.cjs.map +1 -0
  7. package/dist/development/src/egress/EgressGuard.js +170 -0
  8. package/dist/development/src/egress/EgressGuard.js.map +1 -0
  9. package/dist/development/src/egress/EgressPolicy.cjs +110 -0
  10. package/dist/development/src/egress/EgressPolicy.cjs.map +1 -0
  11. package/dist/development/src/egress/EgressPolicy.js +110 -0
  12. package/dist/development/src/egress/EgressPolicy.js.map +1 -0
  13. package/dist/development/src/egress/createEgressGuard.cjs +7 -0
  14. package/dist/development/src/egress/createEgressGuard.cjs.map +1 -0
  15. package/dist/development/src/egress/createEgressGuard.js +7 -0
  16. package/dist/development/src/egress/createEgressGuard.js.map +1 -0
  17. package/dist/development/src/runnable/Supervisor.cjs +38 -6
  18. package/dist/development/src/runnable/Supervisor.cjs.map +1 -1
  19. package/dist/development/src/runnable/Supervisor.js +38 -6
  20. package/dist/development/src/runnable/Supervisor.js.map +1 -1
  21. package/dist/production/index.cjs +7 -0
  22. package/dist/production/index.cjs.map +1 -1
  23. package/dist/production/index.js +8 -1
  24. package/dist/production/index.js.map +1 -1
  25. package/dist/production/src/egress/EgressGuard.cjs +170 -0
  26. package/dist/production/src/egress/EgressGuard.cjs.map +1 -0
  27. package/dist/production/src/egress/EgressGuard.js +170 -0
  28. package/dist/production/src/egress/EgressGuard.js.map +1 -0
  29. package/dist/production/src/egress/EgressPolicy.cjs +110 -0
  30. package/dist/production/src/egress/EgressPolicy.cjs.map +1 -0
  31. package/dist/production/src/egress/EgressPolicy.js +110 -0
  32. package/dist/production/src/egress/EgressPolicy.js.map +1 -0
  33. package/dist/production/src/egress/createEgressGuard.cjs +7 -0
  34. package/dist/production/src/egress/createEgressGuard.cjs.map +1 -0
  35. package/dist/production/src/egress/createEgressGuard.js +7 -0
  36. package/dist/production/src/egress/createEgressGuard.js.map +1 -0
  37. package/dist/production/src/runnable/Supervisor.cjs +38 -6
  38. package/dist/production/src/runnable/Supervisor.cjs.map +1 -1
  39. package/dist/production/src/runnable/Supervisor.js +38 -6
  40. package/dist/production/src/runnable/Supervisor.js.map +1 -1
  41. package/dist/types/MigrationVersionHelper.d.ts.map +1 -1
  42. package/dist/types/egress/EgressGuard.d.ts +37 -0
  43. package/dist/types/egress/EgressGuard.d.ts.map +1 -0
  44. package/dist/types/egress/EgressPolicy.d.ts +25 -0
  45. package/dist/types/egress/EgressPolicy.d.ts.map +1 -0
  46. package/dist/types/egress/createEgressGuard.d.ts +9 -0
  47. package/dist/types/egress/createEgressGuard.d.ts.map +1 -0
  48. package/dist/types/egress/index.d.ts +4 -0
  49. package/dist/types/egress/index.d.ts.map +1 -0
  50. package/dist/types/eventEmitter/events.d.ts.map +1 -1
  51. package/dist/types/index.d.ts +1 -0
  52. package/dist/types/index.d.ts.map +1 -1
  53. package/dist/types/runnable/Supervisor.d.ts +2 -0
  54. package/dist/types/runnable/Supervisor.d.ts.map +1 -1
  55. package/dist/types/runnable/utils.d.ts.map +1 -1
  56. package/dist/types/tsconfig.tsbuildinfo +1 -1
  57. package/dist/types/utils/PromiseMap.d.ts.map +1 -1
  58. package/package.json +2 -2
  59. package/src/egress/EgressGuard.ts +197 -0
  60. package/src/egress/EgressPolicy.ts +129 -0
  61. package/src/egress/createEgressGuard.ts +14 -0
  62. package/src/egress/index.ts +3 -0
  63. package/src/index.ts +1 -0
  64. package/src/runnable/Supervisor.ts +42 -8
  65. package/tests/cases/egress/egressGuard.test.ts +147 -0
  66. package/tests/cases/egress/egressPolicy.test.ts +78 -0
  67. package/tests/cases/supervisor.test.ts +239 -0
@@ -195,3 +195,242 @@ test('supervisor: failed to restart', async () => {
195
195
  'Runnable crashed, stopping: failed to restart',
196
196
  ])
197
197
  })
198
+
199
+ test('supervisor: backs off restarts after a healthy run without max', async () => {
200
+ const { logger, testLogger } = createTestLogger()
201
+ const lastAttempt = 7
202
+ let attempt = 0
203
+ let reachedLastAttempt: () => void = () => {}
204
+ const lastAttemptPromise = new Promise<void>(resolve => {
205
+ reachedLastAttempt = resolve
206
+ })
207
+ const runnable = new class FailingRunnable implements Runnable {
208
+ async run(args: RunnableArgs): Promise<Running> {
209
+ const thisAttempt = ++attempt
210
+ if (thisAttempt === 1) {
211
+ setTimeout(() => args.onError(new Error('crashed')), 1)
212
+ return { end: async () => {} }
213
+ }
214
+ if (thisAttempt === lastAttempt) {
215
+ reachedLastAttempt()
216
+ return { end: async () => {} }
217
+ }
218
+ throw new Error('dependency unavailable')
219
+ }
220
+ }()
221
+
222
+ const supervisor = new Supervisor(runnable, { minBackoff: 1, maxBackoff: 16 })
223
+ const running = await supervisor.run({
224
+ logger,
225
+ onError: shouldNotHappenCallback,
226
+ })
227
+ await lastAttemptPromise
228
+ await running.end()
229
+ expect(testLogger.messages.map(it => it.message).filter(it => it.startsWith('Runnable crashed'))).toStrictEqual([
230
+ 'Runnable crashed, restarting in 1 ms ...: crashed',
231
+ 'Runnable crashed, restarting in 2 ms ...: dependency unavailable',
232
+ 'Runnable crashed, restarting in 4 ms ...: dependency unavailable',
233
+ 'Runnable crashed, restarting in 8 ms ...: dependency unavailable',
234
+ 'Runnable crashed, restarting in 16 ms ...: dependency unavailable',
235
+ 'Runnable crashed, restarting in 16 ms ...: dependency unavailable',
236
+ ])
237
+ })
238
+
239
+ const runFlappingRunnable = async (options: { stableAfter?: number; uptimeMs: (attempt: number) => number; attempts: number }) => {
240
+ const { logger, testLogger } = createTestLogger()
241
+ let attempt = 0
242
+ let reachedLastAttempt: () => void = () => {}
243
+ const lastAttemptPromise = new Promise<void>(resolve => {
244
+ reachedLastAttempt = resolve
245
+ })
246
+ const runnable = new class FlappingRunnable implements Runnable {
247
+ async run(args: RunnableArgs): Promise<Running> {
248
+ const thisAttempt = ++attempt
249
+ if (thisAttempt === options.attempts) {
250
+ reachedLastAttempt()
251
+ } else {
252
+ setTimeout(() => args.onError(new Error(`crashed #${thisAttempt}`)), options.uptimeMs(thisAttempt))
253
+ }
254
+ return { end: async () => {} }
255
+ }
256
+ }()
257
+
258
+ const supervisor = new Supervisor(runnable, { minBackoff: 1, maxBackoff: 16, stableAfter: options.stableAfter })
259
+ const running = await supervisor.run({
260
+ logger,
261
+ onError: shouldNotHappenCallback,
262
+ })
263
+ await lastAttemptPromise
264
+ await running.end()
265
+ return testLogger.messages.map(it => it.message).filter(it => it.startsWith('Runnable crashed'))
266
+ }
267
+
268
+ test('supervisor: backs off a runnable that crashes right after every start', async () => {
269
+ expect(await runFlappingRunnable({ uptimeMs: () => 1, attempts: 7 })).toStrictEqual([
270
+ 'Runnable crashed, restarting in 1 ms ...: crashed #1',
271
+ 'Runnable crashed, restarting in 2 ms ...: crashed #2',
272
+ 'Runnable crashed, restarting in 4 ms ...: crashed #3',
273
+ 'Runnable crashed, restarting in 8 ms ...: crashed #4',
274
+ 'Runnable crashed, restarting in 16 ms ...: crashed #5',
275
+ 'Runnable crashed, restarting in 16 ms ...: crashed #6',
276
+ ])
277
+ })
278
+
279
+ test('supervisor: resets the backoff after a run stays up for stableAfter', async () => {
280
+ expect(await runFlappingRunnable({ stableAfter: 200, uptimeMs: attempt => attempt === 4 ? 250 : 1, attempts: 6 })).toStrictEqual([
281
+ 'Runnable crashed, restarting in 1 ms ...: crashed #1',
282
+ 'Runnable crashed, restarting in 2 ms ...: crashed #2',
283
+ 'Runnable crashed, restarting in 4 ms ...: crashed #3',
284
+ 'Runnable crashed, restarting in 1 ms ...: crashed #4',
285
+ 'Runnable crashed, restarting in 2 ms ...: crashed #5',
286
+ ])
287
+ })
288
+
289
+ test('supervisor: does not count backoff as uptime of a run that crashed before run() returned', async () => {
290
+ const { logger, testLogger } = createTestLogger()
291
+ let attempt = 0
292
+ let reachedLastAttempt: () => void = () => {}
293
+ const lastAttemptPromise = new Promise<void>(resolve => {
294
+ reachedLastAttempt = resolve
295
+ })
296
+ const runnable = new class InstantlyCrashingRunnable implements Runnable {
297
+ async run(args: RunnableArgs): Promise<Running> {
298
+ const thisAttempt = ++attempt
299
+ return await new Promise<Running>(async resolve => {
300
+ resolve({ end: async () => {} })
301
+ if (thisAttempt === 8) {
302
+ reachedLastAttempt()
303
+ return
304
+ }
305
+ await Promise.resolve()
306
+ args.onError(new Error(`crashed #${thisAttempt}`))
307
+ })
308
+ }
309
+ }()
310
+
311
+ const supervisor = new Supervisor(runnable, { minBackoff: 1, maxBackoff: 16, stableAfter: 10 })
312
+ const running = await supervisor.run({
313
+ logger,
314
+ onError: shouldNotHappenCallback,
315
+ })
316
+ await lastAttemptPromise
317
+ await running.end()
318
+ expect(testLogger.messages.map(it => it.message).filter(it => it.startsWith('Runnable crashed'))).toStrictEqual([
319
+ 'Runnable crashed, restarting in 1 ms ...: crashed #1',
320
+ 'Runnable crashed, restarting in 2 ms ...: crashed #2',
321
+ 'Runnable crashed, restarting in 4 ms ...: crashed #3',
322
+ 'Runnable crashed, restarting in 8 ms ...: crashed #4',
323
+ 'Runnable crashed, restarting in 16 ms ...: crashed #5',
324
+ 'Runnable crashed, restarting in 16 ms ...: crashed #6',
325
+ 'Runnable crashed, restarting in 16 ms ...: crashed #7',
326
+ ])
327
+ })
328
+
329
+ test('supervisor: first successful start resets the startup backoff', async () => {
330
+ const { logger, testLogger } = createTestLogger()
331
+ let attempt = 0
332
+ let reachedLastAttempt: () => void = () => {}
333
+ const lastAttemptPromise = new Promise<void>(resolve => {
334
+ reachedLastAttempt = resolve
335
+ })
336
+ const runnable = new class SlowStartingRunnable implements Runnable {
337
+ async run(args: RunnableArgs): Promise<Running> {
338
+ const thisAttempt = ++attempt
339
+ if (thisAttempt <= 3) {
340
+ throw new Error(`failed to start #${thisAttempt}`)
341
+ }
342
+ if (thisAttempt === 5) {
343
+ reachedLastAttempt()
344
+ } else {
345
+ setTimeout(() => args.onError(new Error(`crashed #${thisAttempt}`)), 1)
346
+ }
347
+ return { end: async () => {} }
348
+ }
349
+ }()
350
+
351
+ const supervisor = new Supervisor(runnable, { minBackoff: 1 })
352
+ const running = await supervisor.run({
353
+ logger,
354
+ onError: shouldNotHappenCallback,
355
+ })
356
+ await lastAttemptPromise
357
+ await running.end()
358
+ expect(testLogger.messages.map(it => it.message).filter(it => it.startsWith('Runnable crashed'))).toStrictEqual([
359
+ 'Runnable crashed, restarting in 1 ms ...: failed to start #1',
360
+ 'Runnable crashed, restarting in 2 ms ...: failed to start #2',
361
+ 'Runnable crashed, restarting in 4 ms ...: failed to start #3',
362
+ 'Runnable crashed, restarting in 1 ms ...: crashed #4',
363
+ ])
364
+ })
365
+
366
+ test('supervisor: max limits restarts of runs that crash before becoming stable', async () => {
367
+ const { logger, testLogger } = createTestLogger()
368
+ let attempt = 0
369
+ const runnable = new class FlappingRunnable implements Runnable {
370
+ async run(args: RunnableArgs): Promise<Running> {
371
+ const thisAttempt = ++attempt
372
+ setTimeout(() => args.onError(new Error(`crashed #${thisAttempt}`)), 1)
373
+ return { end: async () => {} }
374
+ }
375
+ }()
376
+
377
+ const supervisor = new Supervisor(runnable, { minBackoff: 1, max: 3 })
378
+ const errorPromise = new Promise(async (resolve, reject) => {
379
+ await supervisor.run({
380
+ logger,
381
+ onError: reject,
382
+ })
383
+ })
384
+
385
+ await expect(errorPromise).rejects.toThrow('crashed #4')
386
+ expect(testLogger.messages.map(it => it.message).filter(it => it.startsWith('Runnable crashed'))).toStrictEqual([
387
+ 'Runnable crashed, restarting in 1 ms ...: crashed #1',
388
+ 'Runnable crashed, restarting in 2 ms ...: crashed #2',
389
+ 'Runnable crashed, restarting in 4 ms ...: crashed #3',
390
+ 'Runnable crashed, stopping: crashed #4',
391
+ ])
392
+ })
393
+
394
+ test('supervisor: end() during a restart that is still starting ends the restarted run', async () => {
395
+ const { logger, testLogger } = createTestLogger()
396
+ let attempt = 0
397
+ let restartStarting = () => {}
398
+ const restartStarted = new Promise<void>(resolve => {
399
+ restartStarting = resolve
400
+ })
401
+ const runnable = new class implements Runnable {
402
+ async run(args: RunnableArgs): Promise<Running> {
403
+ const thisAttempt = ++attempt
404
+ logger.info(`Starting runnable #${thisAttempt}`)
405
+ if (thisAttempt === 1) {
406
+ setTimeout(() => args.onError(new Error()), 1)
407
+ } else {
408
+ restartStarting()
409
+ await new Promise(resolve => setTimeout(resolve, 50))
410
+ logger.info(`Runnable #${thisAttempt} started`)
411
+ }
412
+ return {
413
+ end: async () => {
414
+ logger.info(`Runnable #${thisAttempt} terminated`)
415
+ args.onClose()
416
+ },
417
+ }
418
+ }
419
+ }()
420
+
421
+ const supervisor = new Supervisor(runnable, { minBackoff: 1 })
422
+ const running = await supervisor.run({
423
+ logger,
424
+ onError: shouldNotHappenCallback,
425
+ })
426
+ await restartStarted
427
+ await running.end()
428
+ expect(testLogger.messages.map(it => it.message)).toStrictEqual([
429
+ 'Starting runnable #1',
430
+ 'Runnable crashed, restarting in 1 ms ...',
431
+ 'Restarting runnable now',
432
+ 'Starting runnable #2',
433
+ 'Runnable #2 started',
434
+ 'Runnable #2 terminated',
435
+ ])
436
+ })