@aikirun/worker 0.24.0 → 0.24.2

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 (2) hide show
  1. package/dist/index.js +101 -1
  2. package/package.json +4 -5
package/dist/index.js CHANGED
@@ -52,8 +52,108 @@ var objectOverrider = (defaultObj) => (obj) => {
52
52
  return createBuilder([]);
53
53
  };
54
54
 
55
+ // ../../lib/retry/strategy.ts
56
+ function getRetryParams(attempts, strategy) {
57
+ const strategyType = strategy.type;
58
+ switch (strategyType) {
59
+ case "never":
60
+ return {
61
+ retriesLeft: false
62
+ };
63
+ case "fixed":
64
+ if (attempts >= strategy.maxAttempts) {
65
+ return {
66
+ retriesLeft: false
67
+ };
68
+ }
69
+ return {
70
+ retriesLeft: true,
71
+ delayMs: strategy.delayMs
72
+ };
73
+ case "exponential": {
74
+ if (attempts >= strategy.maxAttempts) {
75
+ return {
76
+ retriesLeft: false
77
+ };
78
+ }
79
+ const delayMs = strategy.baseDelayMs * (strategy.factor ?? 2) ** (attempts - 1);
80
+ return {
81
+ retriesLeft: true,
82
+ delayMs: Math.min(delayMs, strategy.maxDelayMs ?? Number.POSITIVE_INFINITY)
83
+ };
84
+ }
85
+ case "jittered": {
86
+ if (attempts >= strategy.maxAttempts) {
87
+ return {
88
+ retriesLeft: false
89
+ };
90
+ }
91
+ const base = strategy.baseDelayMs * (strategy.jitterFactor ?? 2) ** (attempts - 1);
92
+ const delayMs = Math.random() * base;
93
+ return {
94
+ retriesLeft: true,
95
+ delayMs: Math.min(delayMs, strategy.maxDelayMs ?? Number.POSITIVE_INFINITY)
96
+ };
97
+ }
98
+ default:
99
+ return strategyType;
100
+ }
101
+ }
102
+
103
+ // ../subscriber/db/db-subscriber.ts
104
+ function dbSubscriber(params) {
105
+ const { api, options } = params;
106
+ const intervalMs = options?.intervalMs ?? 1e3;
107
+ const maxRetryIntervalMs = options?.maxRetryIntervalMs ?? 3e4;
108
+ const atCapacityIntervalMs = options?.atCapacityIntervalMs ?? 500;
109
+ const claimMinIdleTimeMs = options?.claimMinIdleTimeMs ?? 9e4;
110
+ const getNextDelay = (delayParams) => {
111
+ switch (delayParams.type) {
112
+ case "polled":
113
+ case "heartbeat":
114
+ return intervalMs;
115
+ case "at_capacity":
116
+ return atCapacityIntervalMs;
117
+ case "retry": {
118
+ const retryParams = getRetryParams(delayParams.attemptNumber, {
119
+ type: "jittered",
120
+ maxAttempts: Number.POSITIVE_INFINITY,
121
+ baseDelayMs: intervalMs,
122
+ maxDelayMs: maxRetryIntervalMs
123
+ });
124
+ if (!retryParams.retriesLeft) {
125
+ return maxRetryIntervalMs;
126
+ }
127
+ return retryParams.delayMs;
128
+ }
129
+ default:
130
+ return delayParams;
131
+ }
132
+ };
133
+ return (context) => {
134
+ const { workerId, workflows, shards } = context;
135
+ const workflowFilters = !isNonEmptyArray(shards) ? workflows.map((workflow) => ({ name: workflow.name, versionId: workflow.versionId })) : workflows.flatMap(
136
+ (workflow) => shards.map((shard) => ({ name: workflow.name, versionId: workflow.versionId, shard }))
137
+ );
138
+ return {
139
+ getNextDelay,
140
+ async getNextBatch(size) {
141
+ const response = await api.workflowRun.claimReadyV1({
142
+ workerId,
143
+ workflows: workflowFilters,
144
+ limit: size,
145
+ claimMinIdleTimeMs
146
+ });
147
+ return response.runs.map((run) => ({
148
+ data: { workflowRunId: run.id }
149
+ }));
150
+ },
151
+ heartbeat: (workflowRunId) => api.workflowRun.heartbeatV1({ id: workflowRunId })
152
+ };
153
+ };
154
+ }
155
+
55
156
  // worker.ts
56
- import { dbSubscriber } from "@aikirun/subscriber-db";
57
157
  import {
58
158
  executeWorkflowRun,
59
159
  getSystemWorkflows,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aikirun/worker",
3
- "version": "0.24.0",
3
+ "version": "0.24.2",
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,10 +18,9 @@
18
18
  "build": "tsup"
19
19
  },
20
20
  "dependencies": {
21
- "@aikirun/types": "0.24.0",
22
- "@aikirun/client": "0.24.0",
23
- "@aikirun/subscriber-db": "0.24.0",
24
- "@aikirun/workflow": "0.24.0",
21
+ "@aikirun/types": "0.24.2",
22
+ "@aikirun/client": "0.24.2",
23
+ "@aikirun/workflow": "0.24.2",
25
24
  "ulidx": "^2.4.1"
26
25
  },
27
26
  "publishConfig": {