@zintrust/workers 0.5.8 → 0.7.7

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.
@@ -643,18 +643,22 @@ const resolveStartFromPersistedProcessor = async (name, record, discovered) => {
643
643
  }
644
644
  }
645
645
  if (!processor) {
646
- const unresolvedError = ErrorFactory.createConfigError(`Worker "${name}" processor is not registered or resolvable. Register the processor at startup or persist a processorSpec.`);
647
- unresolvedError.code = 'WORKER_PROCESSOR_UNRESOLVABLE';
648
- unresolvedError.processorSpec = spec ?? null;
649
- unresolvedError.isPersistedWorkerProcessorMissing = spec !== undefined && !discovered && !isUrlSpec(spec);
646
+ const details = {
647
+ kind: 'worker_processor_unresolvable',
648
+ workerName: name,
649
+ processorSpec: spec ?? null,
650
+ isPersistedWorkerProcessorMissing: spec !== undefined && !discovered && !isUrlSpec(spec),
651
+ };
652
+ const unresolvedError = ErrorFactory.createConfigError(`Worker "${name}" processor is not registered or resolvable. Register the processor at startup or persist a processorSpec.`, details);
650
653
  throw unresolvedError;
651
654
  }
652
655
  return processor;
653
656
  };
654
657
  const isUnresolvablePersistedProcessorError = (value) => {
655
658
  return (isObject(value) &&
656
- value['code'] === 'WORKER_PROCESSOR_UNRESOLVABLE' &&
657
- value['isPersistedWorkerProcessorMissing'] === true);
659
+ isObject(value['details']) &&
660
+ value['details']['kind'] === 'worker_processor_unresolvable' &&
661
+ value['details']['isPersistedWorkerProcessorMissing'] === true);
658
662
  };
659
663
  const discoverFileBackedWorkers = async () => {
660
664
  const files = listWorkerDefinitionFiles();
@@ -2490,11 +2494,16 @@ export const WorkerFactory = Object.freeze({
2490
2494
  }
2491
2495
  catch (error) {
2492
2496
  if (isUnresolvablePersistedProcessorError(error)) {
2497
+ const staleDetails = error.details;
2493
2498
  Logger.warn(`Purging stale persisted worker "${name}" because its processorSpec is no longer resolvable`, {
2494
- processorSpec: record.processorSpec ?? null,
2499
+ processorSpec: staleDetails?.processorSpec ?? record.processorSpec ?? null,
2495
2500
  });
2496
2501
  await WorkerFactory.remove(name, persistenceOverride);
2497
- throw ErrorFactory.createNotFoundError(`Worker "${name}" was removed because its processorSpec no longer resolves to a live worker module.`);
2502
+ throw ErrorFactory.createNotFoundError(`Worker "${name}" was removed because its processorSpec no longer resolves to a live worker module.`, {
2503
+ kind: 'stale_persisted_worker_purged',
2504
+ workerName: name,
2505
+ processorSpec: staleDetails?.processorSpec ?? record.processorSpec ?? null,
2506
+ });
2498
2507
  }
2499
2508
  throw error;
2500
2509
  }
@@ -7,7 +7,7 @@
7
7
  * - Sets up auto-scaling and health checks
8
8
  * - Ensures graceful startup and shutdown
9
9
  */
10
- import { Env, Logger } from '@zintrust/core';
10
+ import { Env, isObject, Logger } from '@zintrust/core';
11
11
  import { ResourceMonitor } from './ResourceMonitor.js';
12
12
  import { WorkerFactory } from './WorkerFactory.js';
13
13
  import { WorkerShutdown } from './WorkerShutdown.js';
@@ -23,6 +23,11 @@ const state = {
23
23
  autoScaling: false,
24
24
  shutdownHandlersRegistered: false,
25
25
  };
26
+ const isStalePersistedWorkerPurgedError = (value) => {
27
+ return (isObject(value) &&
28
+ isObject(value['details']) &&
29
+ value['details']['kind'] === 'stale_persisted_worker_purged');
30
+ };
26
31
  // ============================================================================
27
32
  // Implementation
28
33
  // ============================================================================
@@ -223,7 +228,7 @@ const autoStartOneWorker = async (record) => {
223
228
  }
224
229
  catch (error) {
225
230
  const message = error instanceof Error ? error.message : String(error);
226
- if (message.includes('was removed because its processorSpec no longer resolves to a live worker module.')) {
231
+ if (isStalePersistedWorkerPurgedError(error)) {
227
232
  Logger.info(`Auto-start purged stale worker ${record.name}: ${message}`);
228
233
  return { name: record.name, started: false, skipped: true };
229
234
  }
@@ -17,6 +17,24 @@ const state = {
17
17
  };
18
18
  let shutdownHandlersRegistered = false;
19
19
  const signalHandlers = {};
20
+ const logInfoBestEffort = (message, meta) => {
21
+ const anyLogger = Logger;
22
+ if (typeof anyLogger.info === 'function') {
23
+ anyLogger.info(message, meta);
24
+ return;
25
+ }
26
+ if (typeof anyLogger.warn === 'function') {
27
+ anyLogger.warn(message, meta);
28
+ return;
29
+ }
30
+ if (typeof anyLogger.debug === 'function') {
31
+ anyLogger.debug(message, meta);
32
+ return;
33
+ }
34
+ if (typeof anyLogger.error === 'function') {
35
+ anyLogger.error(message, meta);
36
+ }
37
+ };
20
38
  /**
21
39
  * Perform graceful shutdown of all worker modules
22
40
  */
@@ -30,7 +48,7 @@ async function shutdown(options = {}) {
30
48
  state.isShuttingDown = true;
31
49
  state.startedAt = new Date();
32
50
  state.reason = `Signal: ${signal}`;
33
- Logger.info('🛑 Initiating graceful shutdown of worker management system', {
51
+ logInfoBestEffort('🛑 Initiating graceful shutdown of worker management system', {
34
52
  signal,
35
53
  timeout,
36
54
  forceExit,
@@ -49,7 +67,7 @@ async function shutdown(options = {}) {
49
67
  await WorkerFactory.shutdown();
50
68
  state.completedAt = new Date();
51
69
  const duration = state.completedAt.getTime() - (state.startedAt?.getTime() ?? 0);
52
- Logger.info('✅ Worker management system shutdown complete', {
70
+ logInfoBestEffort('✅ Worker management system shutdown complete', {
53
71
  duration: `${duration}ms`,
54
72
  signal,
55
73
  });
@@ -74,7 +92,7 @@ function registerShutdownHandlers() {
74
92
  Logger.debug('Registering worker management system shutdown handlers');
75
93
  // SIGTERM - graceful shutdown (Docker, systemd, etc.)
76
94
  signalHandlers.sigterm = async () => {
77
- Logger.info('📨 Received SIGTERM signal');
95
+ logInfoBestEffort('📨 Received SIGTERM signal');
78
96
  try {
79
97
  await shutdown({ signal: 'SIGTERM', timeout: 30000, forceExit: true });
80
98
  }
@@ -94,7 +112,7 @@ function registerShutdownHandlers() {
94
112
  // });
95
113
  // SIGHUP - terminal closed
96
114
  signalHandlers.sighup = async () => {
97
- Logger.info('📨 Received SIGHUP signal');
115
+ logInfoBestEffort('📨 Received SIGHUP signal');
98
116
  try {
99
117
  await shutdown({ signal: 'SIGHUP', timeout: 30000, forceExit: true });
100
118
  }
@@ -105,7 +123,7 @@ function registerShutdownHandlers() {
105
123
  process.on('SIGHUP', signalHandlers.sighup);
106
124
  // SIGUSR2 - watcher/hot-reload restart
107
125
  signalHandlers.sigusr2 = async () => {
108
- Logger.info('📨 Received SIGUSR2 signal');
126
+ logInfoBestEffort('📨 Received SIGUSR2 signal');
109
127
  try {
110
128
  await shutdown({ signal: 'SIGUSR2', timeout: 30000, forceExit: true });
111
129
  }
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@zintrust/workers",
3
- "version": "0.5.8",
4
- "buildDate": "2026-04-14T17:02:30.066Z",
3
+ "version": "0.7.7",
4
+ "buildDate": "2026-04-19T14:21:32.967Z",
5
5
  "buildEnvironment": {
6
6
  "node": "v20.20.2",
7
7
  "platform": "linux",
8
8
  "arch": "x64"
9
9
  },
10
10
  "git": {
11
- "commit": "8d52a13d",
11
+ "commit": "3921a3ee",
12
12
  "branch": "master"
13
13
  },
14
14
  "package": {
@@ -178,16 +178,16 @@
178
178
  "sha256": "3869f960c87260588e40941ff91bffcfa0757be7a04815fd28b57dd4840c51df"
179
179
  },
180
180
  "WorkerFactory.js": {
181
- "size": 107223,
182
- "sha256": "1ec8dc5cf8ecf25788c97f24916ca6520646f0e057c21086b3e5eb8105897d72"
181
+ "size": 107617,
182
+ "sha256": "c15002aa229ddf992d2b302a8503f00ecddb78ec97d1afa38de6d9685b9c8b2b"
183
183
  },
184
184
  "WorkerInit.d.ts": {
185
185
  "size": 3284,
186
186
  "sha256": "f902550cda7fca36f2db6297d39a4339ed6b5ff1c738da867233309cf0ca8fe1"
187
187
  },
188
188
  "WorkerInit.js": {
189
- "size": 14343,
190
- "sha256": "b61a4fa55f67bfc4320d526e0bfb15e44d59e20deac8c6ee0224869920183d93"
189
+ "size": 14490,
190
+ "sha256": "d0a59a24bdde2e69d86c33d51dde9661f333c1a68c73509d34e361b32180b423"
191
191
  },
192
192
  "WorkerMetrics.d.ts": {
193
193
  "size": 3304,
@@ -210,8 +210,8 @@
210
210
  "sha256": "84990e17667303d439abb3df4e0b127099df540aed820bbddf0ef53220493710"
211
211
  },
212
212
  "WorkerShutdown.js": {
213
- "size": 7177,
214
- "sha256": "59376b57b0e539bede2d659f0c995fca2ce21c671bc752cae99ab9788b71cd8f"
213
+ "size": 7711,
214
+ "sha256": "e43e163b1524b9cddaf7ec7432ffbb75686d6719881208dee5b1b8f97e997e5a"
215
215
  },
216
216
  "WorkerVersioning.d.ts": {
217
217
  "size": 2881,
@@ -403,7 +403,7 @@
403
403
  },
404
404
  "index.js": {
405
405
  "size": 2336,
406
- "sha256": "e0cb198965b23e9c073f33dfd7f9240ab305d695b90d865352396a7180618ee0"
406
+ "sha256": "8944f13c794e377c7e476191bd1c8f7c37e4867dc20d41e1cc3b6d421c82f6ab"
407
407
  },
408
408
  "queueMonitorHistory.d.ts": {
409
409
  "size": 433,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zintrust/workers",
3
- "version": "0.5.8",
3
+ "version": "0.7.7",
4
4
  "description": "Worker orchestration and background job management for ZinTrust.",
5
5
  "private": false,
6
6
  "type": "module",
@@ -40,7 +40,7 @@
40
40
  "node": ">=20.0.0"
41
41
  },
42
42
  "peerDependencies": {
43
- "@zintrust/core": ">=0.5.0 <0.6.0",
43
+ "@zintrust/core": ">=0.7.0 <0.8.0",
44
44
  "@zintrust/queue-monitor": "*",
45
45
  "@zintrust/queue-redis": "*"
46
46
  },
@@ -972,16 +972,18 @@ const resolveStartFromPersistedProcessor = async (
972
972
  }
973
973
 
974
974
  if (!processor) {
975
+ const details = {
976
+ kind: 'worker_processor_unresolvable',
977
+ workerName: name,
978
+ processorSpec: spec ?? null,
979
+ isPersistedWorkerProcessorMissing: spec !== undefined && !discovered && !isUrlSpec(spec),
980
+ } as const;
981
+
975
982
  const unresolvedError = ErrorFactory.createConfigError(
976
- `Worker "${name}" processor is not registered or resolvable. Register the processor at startup or persist a processorSpec.`
983
+ `Worker "${name}" processor is not registered or resolvable. Register the processor at startup or persist a processorSpec.`,
984
+ details
977
985
  );
978
986
 
979
- (unresolvedError as Error & { code?: string }).code = 'WORKER_PROCESSOR_UNRESOLVABLE';
980
- (unresolvedError as Error & { processorSpec?: string | null }).processorSpec = spec ?? null;
981
- (
982
- unresolvedError as Error & { isPersistedWorkerProcessorMissing?: boolean }
983
- ).isPersistedWorkerProcessorMissing = spec !== undefined && !discovered && !isUrlSpec(spec);
984
-
985
987
  throw unresolvedError;
986
988
  }
987
989
 
@@ -989,9 +991,12 @@ const resolveStartFromPersistedProcessor = async (
989
991
  };
990
992
 
991
993
  type UnresolvablePersistedProcessorError = Error & {
992
- code?: string;
993
- processorSpec?: string | null;
994
- isPersistedWorkerProcessorMissing?: boolean;
994
+ details?: {
995
+ kind?: string;
996
+ workerName?: string;
997
+ processorSpec?: string | null;
998
+ isPersistedWorkerProcessorMissing?: boolean;
999
+ };
995
1000
  };
996
1001
 
997
1002
  const isUnresolvablePersistedProcessorError = (
@@ -999,8 +1004,9 @@ const isUnresolvablePersistedProcessorError = (
999
1004
  ): value is UnresolvablePersistedProcessorError => {
1000
1005
  return (
1001
1006
  isObject(value) &&
1002
- value['code'] === 'WORKER_PROCESSOR_UNRESOLVABLE' &&
1003
- value['isPersistedWorkerProcessorMissing'] === true
1007
+ isObject(value['details']) &&
1008
+ value['details']['kind'] === 'worker_processor_unresolvable' &&
1009
+ value['details']['isPersistedWorkerProcessorMissing'] === true
1004
1010
  );
1005
1011
  };
1006
1012
 
@@ -3415,15 +3421,21 @@ export const WorkerFactory = Object.freeze({
3415
3421
  processor = await resolveStartFromPersistedProcessor(name, record, discovered);
3416
3422
  } catch (error) {
3417
3423
  if (isUnresolvablePersistedProcessorError(error)) {
3424
+ const staleDetails = error.details;
3418
3425
  Logger.warn(
3419
3426
  `Purging stale persisted worker "${name}" because its processorSpec is no longer resolvable`,
3420
3427
  {
3421
- processorSpec: record.processorSpec ?? null,
3428
+ processorSpec: staleDetails?.processorSpec ?? record.processorSpec ?? null,
3422
3429
  }
3423
3430
  );
3424
3431
  await WorkerFactory.remove(name, persistenceOverride);
3425
3432
  throw ErrorFactory.createNotFoundError(
3426
- `Worker "${name}" was removed because its processorSpec no longer resolves to a live worker module.`
3433
+ `Worker "${name}" was removed because its processorSpec no longer resolves to a live worker module.`,
3434
+ {
3435
+ kind: 'stale_persisted_worker_purged',
3436
+ workerName: name,
3437
+ processorSpec: staleDetails?.processorSpec ?? record.processorSpec ?? null,
3438
+ }
3427
3439
  );
3428
3440
  }
3429
3441
  throw error;
package/src/WorkerInit.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  * - Ensures graceful startup and shutdown
9
9
  */
10
10
 
11
- import { Env, Logger } from '@zintrust/core';
11
+ import { Env, isObject, Logger } from '@zintrust/core';
12
12
  import { ResourceMonitor } from './ResourceMonitor';
13
13
  import type { WorkerPersistenceConfig } from './WorkerFactory';
14
14
  import { WorkerFactory } from './WorkerFactory';
@@ -73,6 +73,24 @@ const state: IInitState = {
73
73
  shutdownHandlersRegistered: false,
74
74
  };
75
75
 
76
+ type StalePersistedWorkerPurgedError = Error & {
77
+ details?: {
78
+ kind?: string;
79
+ workerName?: string;
80
+ processorSpec?: string | null;
81
+ };
82
+ };
83
+
84
+ const isStalePersistedWorkerPurgedError = (
85
+ value: unknown
86
+ ): value is StalePersistedWorkerPurgedError => {
87
+ return (
88
+ isObject(value) &&
89
+ isObject(value['details']) &&
90
+ value['details']['kind'] === 'stale_persisted_worker_purged'
91
+ );
92
+ };
93
+
76
94
  // ============================================================================
77
95
  // Implementation
78
96
  // ============================================================================
@@ -347,11 +365,7 @@ const autoStartOneWorker = async (
347
365
  } catch (error) {
348
366
  const message = error instanceof Error ? error.message : String(error);
349
367
 
350
- if (
351
- message.includes(
352
- 'was removed because its processorSpec no longer resolves to a live worker module.'
353
- )
354
- ) {
368
+ if (isStalePersistedWorkerPurgedError(error)) {
355
369
  Logger.info(`Auto-start purged stale worker ${record.name}: ${message}`);
356
370
  return { name: record.name, started: false, skipped: true };
357
371
  }
@@ -57,6 +57,31 @@ const signalHandlers: {
57
57
  unhandledRejection?: (reason: unknown) => void;
58
58
  } = {};
59
59
 
60
+ const logInfoBestEffort = (message: string, meta?: unknown): void => {
61
+ const anyLogger = Logger as unknown as {
62
+ info?: (msg: string, data?: unknown) => void;
63
+ warn?: (msg: string, data?: unknown) => void;
64
+ debug?: (msg: string, data?: unknown) => void;
65
+ error?: (msg: string, data?: unknown) => void;
66
+ };
67
+
68
+ if (typeof anyLogger.info === 'function') {
69
+ anyLogger.info(message, meta);
70
+ return;
71
+ }
72
+ if (typeof anyLogger.warn === 'function') {
73
+ anyLogger.warn(message, meta);
74
+ return;
75
+ }
76
+ if (typeof anyLogger.debug === 'function') {
77
+ anyLogger.debug(message, meta);
78
+ return;
79
+ }
80
+ if (typeof anyLogger.error === 'function') {
81
+ anyLogger.error(message, meta);
82
+ }
83
+ };
84
+
60
85
  /**
61
86
  * Perform graceful shutdown of all worker modules
62
87
  */
@@ -73,7 +98,7 @@ async function shutdown(options: IShutdownOptions = {}): Promise<void> {
73
98
  state.startedAt = new Date();
74
99
  state.reason = `Signal: ${signal}`;
75
100
 
76
- Logger.info('🛑 Initiating graceful shutdown of worker management system', {
101
+ logInfoBestEffort('🛑 Initiating graceful shutdown of worker management system', {
77
102
  signal,
78
103
  timeout,
79
104
  forceExit,
@@ -96,7 +121,7 @@ async function shutdown(options: IShutdownOptions = {}): Promise<void> {
96
121
  state.completedAt = new Date();
97
122
  const duration = state.completedAt.getTime() - (state.startedAt?.getTime() ?? 0);
98
123
 
99
- Logger.info('✅ Worker management system shutdown complete', {
124
+ logInfoBestEffort('✅ Worker management system shutdown complete', {
100
125
  duration: `${duration}ms`,
101
126
  signal,
102
127
  });
@@ -124,7 +149,7 @@ function registerShutdownHandlers(): void {
124
149
 
125
150
  // SIGTERM - graceful shutdown (Docker, systemd, etc.)
126
151
  signalHandlers.sigterm = async (): Promise<void> => {
127
- Logger.info('📨 Received SIGTERM signal');
152
+ logInfoBestEffort('📨 Received SIGTERM signal');
128
153
  try {
129
154
  await shutdown({ signal: 'SIGTERM', timeout: 30000, forceExit: true });
130
155
  } catch (error) {
@@ -145,7 +170,7 @@ function registerShutdownHandlers(): void {
145
170
 
146
171
  // SIGHUP - terminal closed
147
172
  signalHandlers.sighup = async (): Promise<void> => {
148
- Logger.info('📨 Received SIGHUP signal');
173
+ logInfoBestEffort('📨 Received SIGHUP signal');
149
174
  try {
150
175
  await shutdown({ signal: 'SIGHUP', timeout: 30000, forceExit: true });
151
176
  } catch (error) {
@@ -156,7 +181,7 @@ function registerShutdownHandlers(): void {
156
181
 
157
182
  // SIGUSR2 - watcher/hot-reload restart
158
183
  signalHandlers.sigusr2 = async (): Promise<void> => {
159
- Logger.info('📨 Received SIGUSR2 signal');
184
+ logInfoBestEffort('📨 Received SIGUSR2 signal');
160
185
  try {
161
186
  await shutdown({ signal: 'SIGUSR2', timeout: 30000, forceExit: true });
162
187
  } catch (error) {