@aikirun/worker 0.14.0 → 0.16.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/dist/index.d.ts CHANGED
@@ -56,12 +56,14 @@ interface WorkerParams {
56
56
  name: string;
57
57
  workflows: WorkflowVersion<any, any, any, any>[];
58
58
  subscriber?: SubscriberStrategy;
59
- opts?: WorkerOptions;
59
+ opts?: WorkerDefinitionOptions;
60
60
  }
61
- interface WorkerOptions {
61
+ interface WorkerDefinitionOptions {
62
62
  maxConcurrentWorkflowRuns?: number;
63
63
  workflowRun?: WorkflowRunOptions;
64
64
  gracefulShutdownTimeoutMs?: number;
65
+ }
66
+ interface WorkerSpawnOptions extends WorkerDefinitionOptions {
65
67
  /**
66
68
  * Optional array of shards this worker should process.
67
69
  * When provided, the worker will only subscribe to sharded streams.
@@ -88,10 +90,6 @@ interface WorkflowRunOptions {
88
90
  */
89
91
  spinThresholdMs?: number;
90
92
  }
91
- interface WorkerBuilder {
92
- opt<Path extends PathFromObject<WorkerOptions>>(path: Path, value: TypeOfValueAtPath<WorkerOptions, Path>): WorkerBuilder;
93
- spawn: Worker["spawn"];
94
- }
95
93
  interface Worker {
96
94
  name: WorkerName;
97
95
  with(): WorkerBuilder;
@@ -102,5 +100,9 @@ interface WorkerHandle {
102
100
  name: WorkerName;
103
101
  stop: () => Promise<void>;
104
102
  }
103
+ interface WorkerBuilder {
104
+ opt<Path extends PathFromObject<WorkerSpawnOptions>>(path: Path, value: TypeOfValueAtPath<WorkerSpawnOptions, Path>): WorkerBuilder;
105
+ spawn: Worker["spawn"];
106
+ }
105
107
 
106
108
  export { type Worker, type WorkerParams, worker };
package/dist/index.js CHANGED
@@ -75,38 +75,39 @@ import {
75
75
  function worker(params) {
76
76
  return new WorkerImpl(params);
77
77
  }
78
- var WorkerImpl = class _WorkerImpl {
78
+ var WorkerImpl = class {
79
79
  constructor(params) {
80
80
  this.params = params;
81
81
  this.name = params.name;
82
82
  }
83
83
  name;
84
84
  with() {
85
- const optsOverrider = objectOverrider(this.params.opts ?? {});
86
- const createBuilder = (optsBuilder) => ({
87
- opt: (path, value) => createBuilder(optsBuilder.with(path, value)),
88
- spawn: (client) => new _WorkerImpl({ ...this.params, opts: optsBuilder.build() }).spawn(client)
89
- });
90
- return createBuilder(optsOverrider());
85
+ const spawnOpts = this.params.opts ?? {};
86
+ const spawnOptsOverrider = objectOverrider(spawnOpts);
87
+ return new WorkerBuilderImpl(this, spawnOptsOverrider());
91
88
  }
92
89
  async spawn(client) {
93
- const handle = new WorkerHandleImpl(client, this.params);
90
+ return this.spawnWithOpts(client, this.params.opts ?? {});
91
+ }
92
+ async spawnWithOpts(client, spawnOpts) {
93
+ const handle = new WorkerHandleImpl(client, this.params, spawnOpts);
94
94
  await handle._start();
95
95
  return handle;
96
96
  }
97
97
  };
98
98
  var WorkerHandleImpl = class {
99
- constructor(client, params) {
99
+ constructor(client, params, spawnOpts) {
100
100
  this.client = client;
101
101
  this.params = params;
102
+ this.spawnOpts = spawnOpts;
102
103
  this.id = crypto.randomUUID();
103
104
  this.name = params.name;
104
105
  this.workflowRunOpts = {
105
- heartbeatIntervalMs: this.params.opts?.workflowRun?.heartbeatIntervalMs ?? 3e4,
106
- spinThresholdMs: this.params.opts?.workflowRun?.spinThresholdMs ?? 10
106
+ heartbeatIntervalMs: this.spawnOpts.workflowRun?.heartbeatIntervalMs ?? 3e4,
107
+ spinThresholdMs: this.spawnOpts.workflowRun?.spinThresholdMs ?? 10
107
108
  };
108
109
  this.registry = workflowRegistry().addMany(this.params.workflows);
109
- const reference = this.params.opts?.reference;
110
+ const reference = this.spawnOpts.reference;
110
111
  this.logger = client.logger.child({
111
112
  "aiki.component": "worker",
112
113
  "aiki.workerId": this.id,
@@ -126,7 +127,7 @@ var WorkerHandleImpl = class {
126
127
  const subscriberStrategyBuilder = this.client[INTERNAL].subscriber.create(
127
128
  this.params.subscriber ?? { type: "redis" },
128
129
  this.registry.getAll(),
129
- this.params.opts?.shards
130
+ this.spawnOpts.shards
130
131
  );
131
132
  this.subscriberStrategy = await subscriberStrategyBuilder.init(this.id, {
132
133
  onError: (error) => this.handleSubscriberError(error),
@@ -149,7 +150,7 @@ var WorkerHandleImpl = class {
149
150
  if (activeWorkflowRuns.length === 0) {
150
151
  return;
151
152
  }
152
- const timeoutMs = this.params.opts?.gracefulShutdownTimeoutMs ?? 5e3;
153
+ const timeoutMs = this.spawnOpts.gracefulShutdownTimeoutMs ?? 5e3;
153
154
  if (timeoutMs > 0) {
154
155
  await Promise.race([Promise.allSettled(activeWorkflowRuns.map((w) => w.executionPromise)), delay(timeoutMs)]);
155
156
  }
@@ -169,7 +170,7 @@ var WorkerHandleImpl = class {
169
170
  this.logger.info("Worker started", {
170
171
  "aiki.registeredWorkflows": this.params.workflows.map((w) => `${w.name}/${w.versionId}`)
171
172
  });
172
- const maxConcurrentWorkflowRuns = this.params.opts?.maxConcurrentWorkflowRuns ?? 1;
173
+ const maxConcurrentWorkflowRuns = this.spawnOpts.maxConcurrentWorkflowRuns ?? 1;
173
174
  let nextDelayMs = this.subscriberStrategy.getNextDelay({ type: "polled", foundWork: false });
174
175
  let subscriberFailedAttempts = 0;
175
176
  while (!abortSignal.aborted) {
@@ -340,6 +341,18 @@ var WorkerHandleImpl = class {
340
341
  });
341
342
  }
342
343
  };
344
+ var WorkerBuilderImpl = class _WorkerBuilderImpl {
345
+ constructor(worker2, spawnOptsBuilder) {
346
+ this.worker = worker2;
347
+ this.spawnOptsBuilder = spawnOptsBuilder;
348
+ }
349
+ opt(path, value) {
350
+ return new _WorkerBuilderImpl(this.worker, this.spawnOptsBuilder.with(path, value));
351
+ }
352
+ spawn(client) {
353
+ return this.worker.spawnWithOpts(client, this.spawnOptsBuilder.build());
354
+ }
355
+ };
343
356
  export {
344
357
  worker
345
358
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aikirun/worker",
3
- "version": "0.14.0",
3
+ "version": "0.16.0",
4
4
  "description": "Worker SDK for Aiki - execute workflows and tasks with durable state management and automatic recovery",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -18,9 +18,9 @@
18
18
  "build": "tsup"
19
19
  },
20
20
  "dependencies": {
21
- "@aikirun/types": "0.14.0",
22
- "@aikirun/client": "0.14.0",
23
- "@aikirun/workflow": "0.14.0"
21
+ "@aikirun/types": "0.16.0",
22
+ "@aikirun/client": "0.16.0",
23
+ "@aikirun/workflow": "0.16.0"
24
24
  },
25
25
  "publishConfig": {
26
26
  "access": "public"