@hatchet-dev/typescript-sdk 1.12.0 → 1.12.1
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/clients/dispatcher/dispatcher-client.d.ts +14 -1
- package/clients/dispatcher/dispatcher-client.js +25 -2
- package/clients/rest/generated/Api.d.ts +9 -1
- package/clients/rest/generated/data-contracts.d.ts +43 -7
- package/clients/rest/generated/data-contracts.js +13 -2
- package/package.json +1 -1
- package/protoc/dispatcher/dispatcher.d.ts +46 -1
- package/protoc/dispatcher/dispatcher.js +214 -2
- package/protoc/v1/workflows.d.ts +11 -0
- package/protoc/v1/workflows.js +122 -2
- package/step.d.ts +6 -0
- package/step.js +9 -1
- package/v1/client/client.js +1 -1
- package/v1/client/worker/context.d.ts +6 -0
- package/v1/client/worker/context.js +8 -0
- package/v1/client/worker/deprecated/deprecation.d.ts +44 -0
- package/v1/client/worker/deprecated/deprecation.js +95 -0
- package/v1/client/worker/deprecated/index.d.ts +4 -0
- package/v1/client/worker/deprecated/index.js +15 -0
- package/v1/client/worker/deprecated/legacy-registration.d.ts +18 -0
- package/v1/client/worker/deprecated/legacy-registration.js +35 -0
- package/v1/client/worker/deprecated/legacy-v1-worker.d.ts +15 -0
- package/v1/client/worker/deprecated/legacy-v1-worker.js +39 -0
- package/v1/client/worker/deprecated/legacy-worker.d.ts +41 -0
- package/v1/client/worker/deprecated/legacy-worker.js +148 -0
- package/v1/client/worker/slot-utils.d.ts +21 -0
- package/v1/client/worker/slot-utils.js +73 -0
- package/v1/client/worker/worker-internal.d.ts +14 -3
- package/v1/client/worker/worker-internal.js +29 -12
- package/v1/client/worker/worker.d.ts +12 -15
- package/v1/client/worker/worker.js +45 -49
- package/v1/index.d.ts +1 -0
- package/v1/index.js +1 -0
- package/v1/slot-types.d.ts +5 -0
- package/v1/slot-types.js +9 -0
- package/v1/task.d.ts +2 -0
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/workflow.d.ts +2 -2
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Generic time-aware deprecation helper.
|
|
4
|
+
*
|
|
5
|
+
* Timeline (from a given start date, with configurable windows):
|
|
6
|
+
* 0 to warnDays: WARNING logged once per feature
|
|
7
|
+
* warnDays to errorDays: ERROR logged once per feature
|
|
8
|
+
* after errorDays: throws an error 1-in-5 calls (20% chance)
|
|
9
|
+
*
|
|
10
|
+
* Defaults: warnDays=90, errorDays=undefined (error phase disabled unless set).
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.DeprecationError = void 0;
|
|
14
|
+
exports.parseSemver = parseSemver;
|
|
15
|
+
exports.semverLessThan = semverLessThan;
|
|
16
|
+
exports.emitDeprecationNotice = emitDeprecationNotice;
|
|
17
|
+
const DEFAULT_WARN_DAYS = 90;
|
|
18
|
+
const MS_PER_DAY = 24 * 60 * 60 * 1000;
|
|
19
|
+
/** Tracks which features have already been logged (keyed by feature name). */
|
|
20
|
+
const alreadyLogged = new Set();
|
|
21
|
+
class DeprecationError extends Error {
|
|
22
|
+
constructor(feature, message) {
|
|
23
|
+
super(`${feature}: ${message}`);
|
|
24
|
+
this.name = 'DeprecationError';
|
|
25
|
+
this.feature = feature;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.DeprecationError = DeprecationError;
|
|
29
|
+
/**
|
|
30
|
+
* Emit a time-aware deprecation notice.
|
|
31
|
+
*
|
|
32
|
+
* @param feature - A short identifier for deduplication (each feature logs once).
|
|
33
|
+
* @param message - The human-readable deprecation message.
|
|
34
|
+
* @param start - The Date when the deprecation window began.
|
|
35
|
+
* @param logger - A Logger instance for outputting warnings/errors.
|
|
36
|
+
* @param opts - Optional configuration for time windows.
|
|
37
|
+
* @throws DeprecationError after the errorDays window (~20% chance).
|
|
38
|
+
*/
|
|
39
|
+
/**
|
|
40
|
+
* Parses a semver string like "v0.78.23" into [major, minor, patch].
|
|
41
|
+
* Returns [0, 0, 0] if parsing fails.
|
|
42
|
+
*/
|
|
43
|
+
function parseSemver(v) {
|
|
44
|
+
let s = v.startsWith('v') ? v.slice(1) : v;
|
|
45
|
+
const dashIdx = s.indexOf('-');
|
|
46
|
+
if (dashIdx !== -1)
|
|
47
|
+
s = s.slice(0, dashIdx);
|
|
48
|
+
const parts = s.split('.');
|
|
49
|
+
if (parts.length !== 3)
|
|
50
|
+
return [0, 0, 0];
|
|
51
|
+
return [parseInt(parts[0], 10) || 0, parseInt(parts[1], 10) || 0, parseInt(parts[2], 10) || 0];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns true if semver string a is strictly less than b.
|
|
55
|
+
*/
|
|
56
|
+
function semverLessThan(a, b) {
|
|
57
|
+
const [aMaj, aMin, aPat] = parseSemver(a);
|
|
58
|
+
const [bMaj, bMin, bPat] = parseSemver(b);
|
|
59
|
+
if (aMaj !== bMaj)
|
|
60
|
+
return aMaj < bMaj;
|
|
61
|
+
if (aMin !== bMin)
|
|
62
|
+
return aMin < bMin;
|
|
63
|
+
return aPat < bPat;
|
|
64
|
+
}
|
|
65
|
+
function emitDeprecationNotice(feature, message, start, logger, opts) {
|
|
66
|
+
var _a;
|
|
67
|
+
const warnMs = ((_a = opts === null || opts === void 0 ? void 0 : opts.warnDays) !== null && _a !== void 0 ? _a : DEFAULT_WARN_DAYS) * MS_PER_DAY;
|
|
68
|
+
const errorDays = opts === null || opts === void 0 ? void 0 : opts.errorDays;
|
|
69
|
+
const errorMs = errorDays != null ? errorDays * MS_PER_DAY : undefined;
|
|
70
|
+
const elapsed = Date.now() - start.getTime();
|
|
71
|
+
if (elapsed < warnMs) {
|
|
72
|
+
// Phase 1: warning
|
|
73
|
+
if (!alreadyLogged.has(feature)) {
|
|
74
|
+
logger.warn(message);
|
|
75
|
+
alreadyLogged.add(feature);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else if (errorMs === undefined || elapsed < errorMs) {
|
|
79
|
+
// Phase 2: error-level log (indefinite when errorDays is not set)
|
|
80
|
+
if (!alreadyLogged.has(feature)) {
|
|
81
|
+
logger.error(`${message} This fallback will be removed soon. Upgrade immediately.`);
|
|
82
|
+
alreadyLogged.add(feature);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
// Phase 3: throw 1-in-5 times
|
|
87
|
+
if (!alreadyLogged.has(feature)) {
|
|
88
|
+
logger.error(`${message} This fallback is no longer supported and will fail intermittently.`);
|
|
89
|
+
alreadyLogged.add(feature);
|
|
90
|
+
}
|
|
91
|
+
if (Math.random() < 0.2) {
|
|
92
|
+
throw new DeprecationError(feature, message);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { isLegacyEngine, LegacyDualWorker } from './legacy-worker';
|
|
2
|
+
export { LegacyV1Worker } from './legacy-v1-worker';
|
|
3
|
+
export { legacyGetActionListener } from './legacy-registration';
|
|
4
|
+
export { emitDeprecationNotice, DeprecationError, parseSemver, semverLessThan, } from './deprecation';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.semverLessThan = exports.parseSemver = exports.DeprecationError = exports.emitDeprecationNotice = exports.legacyGetActionListener = exports.LegacyV1Worker = exports.LegacyDualWorker = exports.isLegacyEngine = void 0;
|
|
4
|
+
var legacy_worker_1 = require("./legacy-worker");
|
|
5
|
+
Object.defineProperty(exports, "isLegacyEngine", { enumerable: true, get: function () { return legacy_worker_1.isLegacyEngine; } });
|
|
6
|
+
Object.defineProperty(exports, "LegacyDualWorker", { enumerable: true, get: function () { return legacy_worker_1.LegacyDualWorker; } });
|
|
7
|
+
var legacy_v1_worker_1 = require("./legacy-v1-worker");
|
|
8
|
+
Object.defineProperty(exports, "LegacyV1Worker", { enumerable: true, get: function () { return legacy_v1_worker_1.LegacyV1Worker; } });
|
|
9
|
+
var legacy_registration_1 = require("./legacy-registration");
|
|
10
|
+
Object.defineProperty(exports, "legacyGetActionListener", { enumerable: true, get: function () { return legacy_registration_1.legacyGetActionListener; } });
|
|
11
|
+
var deprecation_1 = require("./deprecation");
|
|
12
|
+
Object.defineProperty(exports, "emitDeprecationNotice", { enumerable: true, get: function () { return deprecation_1.emitDeprecationNotice; } });
|
|
13
|
+
Object.defineProperty(exports, "DeprecationError", { enumerable: true, get: function () { return deprecation_1.DeprecationError; } });
|
|
14
|
+
Object.defineProperty(exports, "parseSemver", { enumerable: true, get: function () { return deprecation_1.parseSemver; } });
|
|
15
|
+
Object.defineProperty(exports, "semverLessThan", { enumerable: true, get: function () { return deprecation_1.semverLessThan; } });
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Legacy worker registration using the deprecated `slots` proto field
|
|
3
|
+
* instead of `slotConfig`. For backward compatibility with engines
|
|
4
|
+
* that do not support multiple slot types.
|
|
5
|
+
*/
|
|
6
|
+
import { DispatcherClient, WorkerLabels } from '../../../../clients/dispatcher/dispatcher-client';
|
|
7
|
+
import { ActionListener } from '../../../../clients/dispatcher/action-listener';
|
|
8
|
+
export interface LegacyRegistrationOptions {
|
|
9
|
+
workerName: string;
|
|
10
|
+
services: string[];
|
|
11
|
+
actions: string[];
|
|
12
|
+
slots: number;
|
|
13
|
+
labels: WorkerLabels;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Registers a worker using the legacy `slots` proto field instead of `slotConfig`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function legacyGetActionListener(dispatcher: DispatcherClient, options: LegacyRegistrationOptions): Promise<ActionListener>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Legacy worker registration using the deprecated `slots` proto field
|
|
4
|
+
* instead of `slotConfig`. For backward compatibility with engines
|
|
5
|
+
* that do not support multiple slot types.
|
|
6
|
+
*/
|
|
7
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
8
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
9
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
10
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
11
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
12
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
13
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.legacyGetActionListener = legacyGetActionListener;
|
|
18
|
+
const dispatcher_client_1 = require("../../../../clients/dispatcher/dispatcher-client");
|
|
19
|
+
const action_listener_1 = require("../../../../clients/dispatcher/action-listener");
|
|
20
|
+
/**
|
|
21
|
+
* Registers a worker using the legacy `slots` proto field instead of `slotConfig`.
|
|
22
|
+
*/
|
|
23
|
+
function legacyGetActionListener(dispatcher, options) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const registration = yield dispatcher.client.register({
|
|
26
|
+
workerName: options.workerName,
|
|
27
|
+
services: options.services,
|
|
28
|
+
actions: options.actions,
|
|
29
|
+
slots: options.slots,
|
|
30
|
+
labels: options.labels ? (0, dispatcher_client_1.mapLabels)(options.labels) : undefined,
|
|
31
|
+
runtimeInfo: dispatcher.getRuntimeInfo(),
|
|
32
|
+
});
|
|
33
|
+
return new action_listener_1.ActionListener(dispatcher, registration.workerId);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Legacy V1Worker subclass that registers with the old `slots` proto field
|
|
3
|
+
* instead of `slotConfig`. Used when connected to pre-slot-config engines.
|
|
4
|
+
*/
|
|
5
|
+
import { ActionListener } from '../../../../clients/dispatcher/action-listener';
|
|
6
|
+
import { HatchetClient } from '../../..';
|
|
7
|
+
import { V1Worker } from '../worker-internal';
|
|
8
|
+
export declare class LegacyV1Worker extends V1Worker {
|
|
9
|
+
private _legacySlotCount;
|
|
10
|
+
constructor(client: HatchetClient, options: ConstructorParameters<typeof V1Worker>[1], legacySlots: number);
|
|
11
|
+
/**
|
|
12
|
+
* Override registration to use the legacy `slots` proto field.
|
|
13
|
+
*/
|
|
14
|
+
protected createListener(): Promise<ActionListener>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Legacy V1Worker subclass that registers with the old `slots` proto field
|
|
4
|
+
* instead of `slotConfig`. Used when connected to pre-slot-config engines.
|
|
5
|
+
*/
|
|
6
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
7
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.LegacyV1Worker = void 0;
|
|
17
|
+
const worker_internal_1 = require("../worker-internal");
|
|
18
|
+
const legacy_registration_1 = require("./legacy-registration");
|
|
19
|
+
class LegacyV1Worker extends worker_internal_1.V1Worker {
|
|
20
|
+
constructor(client, options, legacySlots) {
|
|
21
|
+
super(client, options);
|
|
22
|
+
this._legacySlotCount = legacySlots;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Override registration to use the legacy `slots` proto field.
|
|
26
|
+
*/
|
|
27
|
+
createListener() {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
return (0, legacy_registration_1.legacyGetActionListener)(this.client._v0.dispatcher, {
|
|
30
|
+
workerName: this.name,
|
|
31
|
+
services: ['default'],
|
|
32
|
+
actions: Object.keys(this.action_registry),
|
|
33
|
+
slots: this._legacySlotCount,
|
|
34
|
+
labels: this.labels,
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.LegacyV1Worker = LegacyV1Worker;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Legacy dual-worker implementation for pre-slot-config engines.
|
|
3
|
+
*
|
|
4
|
+
* When connected to an older Hatchet engine that does not support multiple slot types,
|
|
5
|
+
* this module provides the old worker start flow which creates separate durable and
|
|
6
|
+
* non-durable workers, each registered with the legacy `slots` proto field.
|
|
7
|
+
*/
|
|
8
|
+
import { HatchetClient } from '../../..';
|
|
9
|
+
import { CreateWorkerOpts } from '../worker';
|
|
10
|
+
import { LegacyV1Worker } from './legacy-v1-worker';
|
|
11
|
+
/**
|
|
12
|
+
* Checks if the connected engine is legacy by comparing its semantic version
|
|
13
|
+
* against the minimum required version for slot_config support.
|
|
14
|
+
* Returns true if the engine is legacy, false otherwise.
|
|
15
|
+
* Emits a time-aware deprecation notice when a legacy engine is detected.
|
|
16
|
+
*/
|
|
17
|
+
export declare function isLegacyEngine(v1: HatchetClient): Promise<boolean>;
|
|
18
|
+
/**
|
|
19
|
+
* LegacyDualWorker manages two V1Worker instances (nonDurable + durable)
|
|
20
|
+
* for engines that don't support slot_config.
|
|
21
|
+
* Uses the legacy `slots` proto field (maxRuns) instead of `slotConfig`.
|
|
22
|
+
*/
|
|
23
|
+
export declare class LegacyDualWorker {
|
|
24
|
+
private nonDurable;
|
|
25
|
+
private durable;
|
|
26
|
+
private name;
|
|
27
|
+
constructor(name: string, nonDurable: LegacyV1Worker, durable?: LegacyV1Worker);
|
|
28
|
+
/**
|
|
29
|
+
* Creates a legacy dual-worker setup from the given options.
|
|
30
|
+
* Workers are created with legacy registration (old `slots` proto field).
|
|
31
|
+
*/
|
|
32
|
+
static create(v1: HatchetClient, name: string, options: CreateWorkerOpts): Promise<LegacyDualWorker>;
|
|
33
|
+
/**
|
|
34
|
+
* Starts both workers using Promise.all.
|
|
35
|
+
*/
|
|
36
|
+
start(): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Stops both workers.
|
|
39
|
+
*/
|
|
40
|
+
stop(): Promise<void>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Legacy dual-worker implementation for pre-slot-config engines.
|
|
4
|
+
*
|
|
5
|
+
* When connected to an older Hatchet engine that does not support multiple slot types,
|
|
6
|
+
* this module provides the old worker start flow which creates separate durable and
|
|
7
|
+
* non-durable workers, each registered with the legacy `slots` proto field.
|
|
8
|
+
*/
|
|
9
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
10
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
11
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
12
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
13
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
14
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
15
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.LegacyDualWorker = void 0;
|
|
20
|
+
exports.isLegacyEngine = isLegacyEngine;
|
|
21
|
+
const nice_grpc_1 = require("nice-grpc");
|
|
22
|
+
const declaration_1 = require("../../../declaration");
|
|
23
|
+
const legacy_v1_worker_1 = require("./legacy-v1-worker");
|
|
24
|
+
const deprecation_1 = require("./deprecation");
|
|
25
|
+
const DEFAULT_DEFAULT_SLOTS = 100;
|
|
26
|
+
const DEFAULT_DURABLE_SLOTS = 1000;
|
|
27
|
+
/** The date when slot_config support was released. */
|
|
28
|
+
const LEGACY_ENGINE_START = new Date('2026-02-12T00:00:00Z');
|
|
29
|
+
/** Minimum engine version that supports multiple slot types. */
|
|
30
|
+
const MIN_SLOT_CONFIG_VERSION = 'v0.78.23';
|
|
31
|
+
const LEGACY_ENGINE_MESSAGE = 'Connected to an older Hatchet engine that does not support multiple slot types. ' +
|
|
32
|
+
'Falling back to legacy worker registration. ' +
|
|
33
|
+
'Please upgrade your Hatchet engine to the latest version.';
|
|
34
|
+
/**
|
|
35
|
+
* Checks if the connected engine is legacy by comparing its semantic version
|
|
36
|
+
* against the minimum required version for slot_config support.
|
|
37
|
+
* Returns true if the engine is legacy, false otherwise.
|
|
38
|
+
* Emits a time-aware deprecation notice when a legacy engine is detected.
|
|
39
|
+
*/
|
|
40
|
+
function isLegacyEngine(v1) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
try {
|
|
43
|
+
const version = yield v1._v0.dispatcher.getVersion();
|
|
44
|
+
// If the version is empty or older than the minimum, treat as legacy
|
|
45
|
+
if (!version || (0, deprecation_1.semverLessThan)(version, MIN_SLOT_CONFIG_VERSION)) {
|
|
46
|
+
const logger = v1._v0.config.logger('Worker', v1._v0.config.log_level);
|
|
47
|
+
(0, deprecation_1.emitDeprecationNotice)('legacy-engine', LEGACY_ENGINE_MESSAGE, LEGACY_ENGINE_START, logger, {
|
|
48
|
+
errorDays: 180,
|
|
49
|
+
});
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
if ((e === null || e === void 0 ? void 0 : e.code) === nice_grpc_1.Status.UNIMPLEMENTED) {
|
|
56
|
+
const logger = v1._v0.config.logger('Worker', v1._v0.config.log_level);
|
|
57
|
+
(0, deprecation_1.emitDeprecationNotice)('legacy-engine', LEGACY_ENGINE_MESSAGE, LEGACY_ENGINE_START, logger, {
|
|
58
|
+
errorDays: 180,
|
|
59
|
+
});
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
// For other errors, assume new engine and let registration fail naturally
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* LegacyDualWorker manages two V1Worker instances (nonDurable + durable)
|
|
69
|
+
* for engines that don't support slot_config.
|
|
70
|
+
* Uses the legacy `slots` proto field (maxRuns) instead of `slotConfig`.
|
|
71
|
+
*/
|
|
72
|
+
class LegacyDualWorker {
|
|
73
|
+
constructor(name, nonDurable, durable) {
|
|
74
|
+
this.name = name;
|
|
75
|
+
this.nonDurable = nonDurable;
|
|
76
|
+
this.durable = durable;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Creates a legacy dual-worker setup from the given options.
|
|
80
|
+
* Workers are created with legacy registration (old `slots` proto field).
|
|
81
|
+
*/
|
|
82
|
+
static create(v1, name, options) {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
const defaultSlots = options.slots || options.maxRuns || DEFAULT_DEFAULT_SLOTS;
|
|
85
|
+
const durableSlots = options.durableSlots || DEFAULT_DURABLE_SLOTS;
|
|
86
|
+
// Create the non-durable worker with legacy registration
|
|
87
|
+
const nonDurable = new legacy_v1_worker_1.LegacyV1Worker(v1, { name, labels: options.labels, handleKill: options.handleKill }, defaultSlots);
|
|
88
|
+
// Check if any workflows have durable tasks
|
|
89
|
+
let hasDurableTasks = false;
|
|
90
|
+
for (const wf of options.workflows || []) {
|
|
91
|
+
if (wf instanceof declaration_1.BaseWorkflowDeclaration) {
|
|
92
|
+
if (wf.definition._durableTasks.length > 0) {
|
|
93
|
+
hasDurableTasks = true;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
let durableWorker;
|
|
99
|
+
if (hasDurableTasks) {
|
|
100
|
+
// Create the durable worker with legacy registration
|
|
101
|
+
durableWorker = new legacy_v1_worker_1.LegacyV1Worker(v1, { name: `${name}-durable`, labels: options.labels, handleKill: options.handleKill }, durableSlots);
|
|
102
|
+
}
|
|
103
|
+
const legacyWorker = new LegacyDualWorker(name, nonDurable, durableWorker);
|
|
104
|
+
// Register workflows on appropriate workers
|
|
105
|
+
for (const wf of options.workflows || []) {
|
|
106
|
+
if (wf instanceof declaration_1.BaseWorkflowDeclaration) {
|
|
107
|
+
if (wf.definition._durableTasks.length > 0 && durableWorker) {
|
|
108
|
+
yield durableWorker.registerWorkflowV1(wf);
|
|
109
|
+
durableWorker.registerDurableActionsV1(wf.definition);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
yield nonDurable.registerWorkflowV1(wf);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
// fallback to v0 client for backwards compatibility
|
|
117
|
+
yield nonDurable.registerWorkflow(wf);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return legacyWorker;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Starts both workers using Promise.all.
|
|
125
|
+
*/
|
|
126
|
+
start() {
|
|
127
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
+
const promises = [this.nonDurable.start()];
|
|
129
|
+
if (this.durable) {
|
|
130
|
+
promises.push(this.durable.start());
|
|
131
|
+
}
|
|
132
|
+
yield Promise.all(promises);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Stops both workers.
|
|
137
|
+
*/
|
|
138
|
+
stop() {
|
|
139
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
140
|
+
const promises = [this.nonDurable.stop()];
|
|
141
|
+
if (this.durable) {
|
|
142
|
+
promises.push(this.durable.stop());
|
|
143
|
+
}
|
|
144
|
+
yield Promise.all(promises);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
exports.LegacyDualWorker = LegacyDualWorker;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Workflow as V0Workflow } from '../../../workflow';
|
|
2
|
+
import { BaseWorkflowDeclaration } from '../../declaration';
|
|
3
|
+
import { SlotConfig } from '../../slot-types';
|
|
4
|
+
export interface WorkerSlotOptions {
|
|
5
|
+
/** (optional) Maximum number of concurrent runs on this worker, defaults to 100 */
|
|
6
|
+
slots?: number;
|
|
7
|
+
/** (optional) Maximum number of concurrent durable tasks, defaults to 1,000 */
|
|
8
|
+
durableSlots?: number;
|
|
9
|
+
/** (optional) Array of workflows to register */
|
|
10
|
+
workflows?: BaseWorkflowDeclaration<any, any>[] | V0Workflow[];
|
|
11
|
+
/** @deprecated Use slots instead */
|
|
12
|
+
maxRuns?: number;
|
|
13
|
+
}
|
|
14
|
+
export declare function resolveWorkerOptions<T extends WorkerSlotOptions>(options: T): T & {
|
|
15
|
+
slots?: number;
|
|
16
|
+
durableSlots?: number;
|
|
17
|
+
slotConfig: SlotConfig;
|
|
18
|
+
};
|
|
19
|
+
export declare const testingExports: {
|
|
20
|
+
resolveWorkerOptions: typeof resolveWorkerOptions;
|
|
21
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.testingExports = void 0;
|
|
4
|
+
exports.resolveWorkerOptions = resolveWorkerOptions;
|
|
5
|
+
const declaration_1 = require("../../declaration");
|
|
6
|
+
const slot_types_1 = require("../../slot-types");
|
|
7
|
+
const DEFAULT_DEFAULT_SLOTS = 100;
|
|
8
|
+
const DEFAULT_DURABLE_SLOTS = 1000;
|
|
9
|
+
function resolveWorkerOptions(options) {
|
|
10
|
+
const requiredSlotTypes = options.workflows
|
|
11
|
+
? getRequiredSlotTypes(options.workflows)
|
|
12
|
+
: new Set();
|
|
13
|
+
const slotConfig = options.slots || options.durableSlots || options.maxRuns
|
|
14
|
+
? Object.assign(Object.assign({}, (options.slots || options.maxRuns
|
|
15
|
+
? { [slot_types_1.SlotType.Default]: options.slots || options.maxRuns || 0 }
|
|
16
|
+
: {})), (options.durableSlots ? { [slot_types_1.SlotType.Durable]: options.durableSlots } : {})) : {};
|
|
17
|
+
if (requiredSlotTypes.has(slot_types_1.SlotType.Default) && slotConfig[slot_types_1.SlotType.Default] == null) {
|
|
18
|
+
slotConfig[slot_types_1.SlotType.Default] = DEFAULT_DEFAULT_SLOTS;
|
|
19
|
+
}
|
|
20
|
+
if (requiredSlotTypes.has(slot_types_1.SlotType.Durable) && slotConfig[slot_types_1.SlotType.Durable] == null) {
|
|
21
|
+
slotConfig[slot_types_1.SlotType.Durable] = DEFAULT_DURABLE_SLOTS;
|
|
22
|
+
}
|
|
23
|
+
if (Object.keys(slotConfig).length === 0) {
|
|
24
|
+
slotConfig[slot_types_1.SlotType.Default] = DEFAULT_DEFAULT_SLOTS;
|
|
25
|
+
}
|
|
26
|
+
return Object.assign(Object.assign({}, options), { slots: options.slots ||
|
|
27
|
+
options.maxRuns ||
|
|
28
|
+
(slotConfig[slot_types_1.SlotType.Default] != null ? slotConfig[slot_types_1.SlotType.Default] : undefined), durableSlots: options.durableSlots ||
|
|
29
|
+
(slotConfig[slot_types_1.SlotType.Durable] != null ? slotConfig[slot_types_1.SlotType.Durable] : undefined), slotConfig });
|
|
30
|
+
}
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
32
|
+
exports.testingExports = {
|
|
33
|
+
resolveWorkerOptions,
|
|
34
|
+
};
|
|
35
|
+
function getRequiredSlotTypes(workflows) {
|
|
36
|
+
const required = new Set();
|
|
37
|
+
const addFromRequests = (requests, fallbackType) => {
|
|
38
|
+
if (requests && Object.keys(requests).length > 0) {
|
|
39
|
+
if (requests[slot_types_1.SlotType.Default] !== undefined) {
|
|
40
|
+
required.add(slot_types_1.SlotType.Default);
|
|
41
|
+
}
|
|
42
|
+
if (requests[slot_types_1.SlotType.Durable] !== undefined) {
|
|
43
|
+
required.add(slot_types_1.SlotType.Durable);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
required.add(fallbackType);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
for (const wf of workflows) {
|
|
51
|
+
if (wf instanceof declaration_1.BaseWorkflowDeclaration) {
|
|
52
|
+
// eslint-disable-next-line dot-notation
|
|
53
|
+
const tasks = wf.definition['_tasks'];
|
|
54
|
+
for (const task of tasks) {
|
|
55
|
+
addFromRequests(task.slotRequests, slot_types_1.SlotType.Default);
|
|
56
|
+
}
|
|
57
|
+
// eslint-disable-next-line dot-notation
|
|
58
|
+
const durableTasks = wf.definition['_durableTasks'];
|
|
59
|
+
if (durableTasks.length > 0) {
|
|
60
|
+
required.add(slot_types_1.SlotType.Durable);
|
|
61
|
+
}
|
|
62
|
+
if (wf.definition.onFailure) {
|
|
63
|
+
const opts = typeof wf.definition.onFailure === 'object' ? wf.definition.onFailure : undefined;
|
|
64
|
+
addFromRequests(opts === null || opts === void 0 ? void 0 : opts.slotRequests, slot_types_1.SlotType.Default);
|
|
65
|
+
}
|
|
66
|
+
if (wf.definition.onSuccess) {
|
|
67
|
+
const opts = typeof wf.definition.onSuccess === 'object' ? wf.definition.onSuccess : undefined;
|
|
68
|
+
addFromRequests(opts === null || opts === void 0 ? void 0 : opts.slotRequests, slot_types_1.SlotType.Default);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return required;
|
|
73
|
+
}
|
|
@@ -8,11 +8,13 @@ import { BaseWorkflowDeclaration, WorkflowDefinition, HatchetClient } from '../.
|
|
|
8
8
|
import { WorkerLabels } from '../../../clients/dispatcher/dispatcher-client';
|
|
9
9
|
import { StepRunFunction } from '../../../step';
|
|
10
10
|
import { Context } from './context';
|
|
11
|
+
import { SlotConfig } from '../../slot-types';
|
|
11
12
|
export type ActionRegistry = Record<Action['actionId'], Function>;
|
|
12
13
|
export interface WorkerOpts {
|
|
13
14
|
name: string;
|
|
14
15
|
handleKill?: boolean;
|
|
15
|
-
|
|
16
|
+
slots?: number;
|
|
17
|
+
durableSlots?: number;
|
|
16
18
|
labels?: WorkerLabels;
|
|
17
19
|
healthPort?: number;
|
|
18
20
|
enableHealthServer?: boolean;
|
|
@@ -28,7 +30,9 @@ export declare class V1Worker {
|
|
|
28
30
|
listener: ActionListener | undefined;
|
|
29
31
|
futures: Record<Action['taskRunExternalId'], HatchetPromise<any>>;
|
|
30
32
|
contexts: Record<Action['taskRunExternalId'], Context<any, any>>;
|
|
31
|
-
|
|
33
|
+
slots?: number;
|
|
34
|
+
durableSlots?: number;
|
|
35
|
+
slotConfig: SlotConfig;
|
|
32
36
|
logger: Logger;
|
|
33
37
|
registeredWorkflowPromises: Array<Promise<any>>;
|
|
34
38
|
labels: WorkerLabels;
|
|
@@ -39,7 +43,9 @@ export declare class V1Worker {
|
|
|
39
43
|
constructor(client: HatchetClient, options: {
|
|
40
44
|
name: string;
|
|
41
45
|
handleKill?: boolean;
|
|
42
|
-
|
|
46
|
+
slots?: number;
|
|
47
|
+
durableSlots?: number;
|
|
48
|
+
slotConfig?: SlotConfig;
|
|
43
49
|
labels?: WorkerLabels;
|
|
44
50
|
});
|
|
45
51
|
private initializeHealthServer;
|
|
@@ -66,6 +72,11 @@ export declare class V1Worker {
|
|
|
66
72
|
handleCancelStepRun(action: Action): Promise<void>;
|
|
67
73
|
stop(): Promise<void>;
|
|
68
74
|
exitGracefully(handleKill: boolean): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Creates an action listener by registering the worker with the dispatcher.
|
|
77
|
+
* Override in subclasses to change registration behavior (e.g. legacy engines).
|
|
78
|
+
*/
|
|
79
|
+
protected createListener(): Promise<ActionListener>;
|
|
69
80
|
start(): Promise<void>;
|
|
70
81
|
handleAction(action: Action): Promise<void>;
|
|
71
82
|
upsertLabels(labels: WorkerLabels): Promise<WorkerLabels>;
|
|
@@ -46,7 +46,9 @@ class V1Worker {
|
|
|
46
46
|
this.client = client;
|
|
47
47
|
this.name = (0, apply_namespace_1.applyNamespace)(options.name, this.client.config.namespace);
|
|
48
48
|
this.action_registry = {};
|
|
49
|
-
this.
|
|
49
|
+
this.slots = options.slots;
|
|
50
|
+
this.durableSlots = options.durableSlots;
|
|
51
|
+
this.slotConfig = options.slotConfig || {};
|
|
50
52
|
this.labels = options.labels || {};
|
|
51
53
|
this.enableHealthServer = (_b = (_a = client.config.healthcheck) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : false;
|
|
52
54
|
this.healthPort = (_d = (_c = client.config.healthcheck) === null || _c === void 0 ? void 0 : _c.port) !== null && _d !== void 0 ? _d : 8001;
|
|
@@ -67,11 +69,10 @@ class V1Worker {
|
|
|
67
69
|
this.healthServer = new health_server_1.HealthServer(this.healthPort, () => this.status, this.name, () => this.getAvailableSlots(), () => this.getRegisteredActions(), () => this.getFilteredLabels(), this.logger);
|
|
68
70
|
}
|
|
69
71
|
getAvailableSlots() {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
72
|
+
// sum all the slots in the slot config
|
|
73
|
+
const totalSlots = Object.values(this.slotConfig).reduce((acc, curr) => acc + curr, 0);
|
|
73
74
|
const currentRuns = Object.keys(this.futures).length;
|
|
74
|
-
return Math.max(0,
|
|
75
|
+
return Math.max(0, totalSlots - currentRuns);
|
|
75
76
|
}
|
|
76
77
|
getRegisteredActions() {
|
|
77
78
|
return Object.keys(this.action_registry);
|
|
@@ -177,6 +178,8 @@ class V1Worker {
|
|
|
177
178
|
rateLimits: [],
|
|
178
179
|
workerLabels: {},
|
|
179
180
|
concurrency: [],
|
|
181
|
+
isDurable: false,
|
|
182
|
+
slotRequests: { default: 1 },
|
|
180
183
|
};
|
|
181
184
|
}
|
|
182
185
|
if (workflow.onFailure && typeof workflow.onFailure === 'object') {
|
|
@@ -194,6 +197,8 @@ class V1Worker {
|
|
|
194
197
|
concurrency: [],
|
|
195
198
|
backoffFactor: ((_f = onFailure.backoff) === null || _f === void 0 ? void 0 : _f.factor) || ((_h = (_g = workflow.taskDefaults) === null || _g === void 0 ? void 0 : _g.backoff) === null || _h === void 0 ? void 0 : _h.factor),
|
|
196
199
|
backoffMaxSeconds: ((_j = onFailure.backoff) === null || _j === void 0 ? void 0 : _j.maxSeconds) || ((_l = (_k = workflow.taskDefaults) === null || _k === void 0 ? void 0 : _k.backoff) === null || _l === void 0 ? void 0 : _l.maxSeconds),
|
|
200
|
+
isDurable: false,
|
|
201
|
+
slotRequests: { default: 1 },
|
|
197
202
|
};
|
|
198
203
|
}
|
|
199
204
|
let onSuccessTask;
|
|
@@ -252,6 +257,7 @@ class V1Worker {
|
|
|
252
257
|
const jsonSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(workflow.inputValidator);
|
|
253
258
|
inputJsonSchema = new TextEncoder().encode(JSON.stringify(jsonSchema));
|
|
254
259
|
}
|
|
260
|
+
const durableTaskSet = new Set(workflow._durableTasks);
|
|
255
261
|
const registeredWorkflow = this.client._v0.admin.putWorkflowV1({
|
|
256
262
|
name: workflow.name,
|
|
257
263
|
description: workflow.description || '',
|
|
@@ -282,6 +288,8 @@ class V1Worker {
|
|
|
282
288
|
backoffFactor: ((_h = task.backoff) === null || _h === void 0 ? void 0 : _h.factor) || ((_k = (_j = workflow.taskDefaults) === null || _j === void 0 ? void 0 : _j.backoff) === null || _k === void 0 ? void 0 : _k.factor),
|
|
283
289
|
backoffMaxSeconds: ((_l = task.backoff) === null || _l === void 0 ? void 0 : _l.maxSeconds) || ((_o = (_m = workflow.taskDefaults) === null || _m === void 0 ? void 0 : _m.backoff) === null || _o === void 0 ? void 0 : _o.maxSeconds),
|
|
284
290
|
conditions: (0, transformer_1.taskConditionsToPb)(task),
|
|
291
|
+
isDurable: durableTaskSet.has(task),
|
|
292
|
+
slotRequests: task.slotRequests || (durableTaskSet.has(task) ? { durable: 1 } : { default: 1 }),
|
|
285
293
|
concurrency: task.concurrency
|
|
286
294
|
? Array.isArray(task.concurrency)
|
|
287
295
|
? task.concurrency
|
|
@@ -671,6 +679,21 @@ class V1Worker {
|
|
|
671
679
|
}
|
|
672
680
|
});
|
|
673
681
|
}
|
|
682
|
+
/**
|
|
683
|
+
* Creates an action listener by registering the worker with the dispatcher.
|
|
684
|
+
* Override in subclasses to change registration behavior (e.g. legacy engines).
|
|
685
|
+
*/
|
|
686
|
+
createListener() {
|
|
687
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
688
|
+
return this.client._v0.dispatcher.getActionListener({
|
|
689
|
+
workerName: this.name,
|
|
690
|
+
services: ['default'],
|
|
691
|
+
actions: Object.keys(this.action_registry),
|
|
692
|
+
slotConfig: this.slotConfig,
|
|
693
|
+
labels: this.labels,
|
|
694
|
+
});
|
|
695
|
+
});
|
|
696
|
+
}
|
|
674
697
|
start() {
|
|
675
698
|
return __awaiter(this, void 0, void 0, function* () {
|
|
676
699
|
var _a, e_1, _b, _c;
|
|
@@ -691,13 +714,7 @@ class V1Worker {
|
|
|
691
714
|
return;
|
|
692
715
|
}
|
|
693
716
|
try {
|
|
694
|
-
this.listener = yield this.
|
|
695
|
-
workerName: this.name,
|
|
696
|
-
services: ['default'],
|
|
697
|
-
actions: Object.keys(this.action_registry),
|
|
698
|
-
maxRuns: this.maxRuns,
|
|
699
|
-
labels: this.labels,
|
|
700
|
-
});
|
|
717
|
+
this.listener = yield this.createListener();
|
|
701
718
|
this.workerId = this.listener.workerId;
|
|
702
719
|
this.setStatus(health_server_1.workerStatus.HEALTHY);
|
|
703
720
|
const generator = this.listener.actions();
|