@empyria/restate 0.4.13 → 0.4.14
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/.github/dependabot.yml +4 -4
- package/README.md +4 -0
- package/lib/Cron.js +27 -2
- package/package.json +1 -1
- package/test/Cron.test.js +71 -2
package/.github/dependabot.yml
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
version: 2
|
|
7
7
|
updates:
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
- package-ecosystem: "" # See documentation for possible values
|
|
9
|
+
directory: "/" # Location of package manifests
|
|
10
|
+
schedule:
|
|
11
|
+
interval: "daily"
|
package/README.md
CHANGED
|
@@ -118,6 +118,10 @@ endpoint, then create jobs with either:
|
|
|
118
118
|
starts with the application.** The job has the stable `id` you give it, so calling it on every start never adds a second
|
|
119
119
|
job: the same request changes nothing, and a changed request (another schedule, target or payload) replaces the pending
|
|
120
120
|
run under the same ID. Call it once the endpoint has registered, e.g. from `setupRestate`'s `registrationCallbackFn`.
|
|
121
|
+
It also repairs a job whose chain broke: each run schedules the next, so a pending run that was killed, cancelled or
|
|
122
|
+
lost with a deleted deployment would stop the job for good. When the same request finds its pending run overdue by
|
|
123
|
+
more than `OVERDUE_GRACE_MS` (60 s), `ensure` schedules a fresh one; a replaced run that still arrives does nothing,
|
|
124
|
+
because a run only works when it is the job's current one.
|
|
121
125
|
|
|
122
126
|
Cron expressions are `croner` patterns (five fields, or six with seconds for tests). The target handler is sent the
|
|
123
127
|
`payload` as its input, or no input when there is none.
|
package/lib/Cron.js
CHANGED
|
@@ -27,6 +27,15 @@ export const RETRY_FOREVER = {
|
|
|
27
27
|
exponentiationFactor: 2,
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* How late a job's pending run may be before `ensure` treats it as lost and schedules a fresh one. A run is normally
|
|
32
|
+
* late only by the time the services were down; a run that is late by more than this, when the application starts and
|
|
33
|
+
* calls `ensure`, has most likely been killed, cancelled or lost with a deleted deployment, and nothing else would ever
|
|
34
|
+
* schedule the next one. Replacing a run that is merely slow is harmless: `execute` ignores a run that is no longer
|
|
35
|
+
* the job's current one.
|
|
36
|
+
*/
|
|
37
|
+
export const OVERDUE_GRACE_MS = 60_000
|
|
38
|
+
|
|
30
39
|
export const cronJobInitiator = restate.service({
|
|
31
40
|
name: 'CronJobInitiator',
|
|
32
41
|
handlers: {
|
|
@@ -56,7 +65,8 @@ export const cronJob = restate.object({
|
|
|
56
65
|
* code that starts with the application and cannot know whether the job was created before.
|
|
57
66
|
*
|
|
58
67
|
* - no job yet: schedules it, as `initiate` does;
|
|
59
|
-
* - the same request already scheduled: changes nothing and returns the current state
|
|
68
|
+
* - the same request already scheduled: changes nothing and returns the current state, unless its pending run
|
|
69
|
+
* is overdue by more than {@link OVERDUE_GRACE_MS} (killed, cancelled or lost): then it schedules a fresh one;
|
|
60
70
|
* - a different request (another schedule, target or payload): cancels the pending run and
|
|
61
71
|
* schedules the new request under the same job ID.
|
|
62
72
|
*/
|
|
@@ -64,7 +74,11 @@ export const cronJob = restate.object({
|
|
|
64
74
|
const wanted = normalizeRequest(request)
|
|
65
75
|
const jobState = await ctx.get(JOB_STATE_NAME)
|
|
66
76
|
|
|
67
|
-
if (jobState && sameRequest(jobState.request, wanted))
|
|
77
|
+
if (jobState && sameRequest(jobState.request, wanted)) {
|
|
78
|
+
const dueAt = jobState.next_execution_at ?? Date.parse(jobState.next_execution_time)
|
|
79
|
+
const now = await ctx.date.now()
|
|
80
|
+
if (!(Number.isFinite(dueAt) && now > dueAt + OVERDUE_GRACE_MS)) return jobState
|
|
81
|
+
}
|
|
68
82
|
if (jobState) ctx.cancel(jobState.next_execution_id)
|
|
69
83
|
|
|
70
84
|
return await scheduleNextExecution(ctx, wanted)
|
|
@@ -74,6 +88,15 @@ export const cronJob = restate.object({
|
|
|
74
88
|
if (!jobState) {
|
|
75
89
|
throw new TerminalError('Job not found.')
|
|
76
90
|
}
|
|
91
|
+
// Only the job's current run works and schedules the next one. A run that `ensure` replaced (or that a
|
|
92
|
+
// changed schedule superseded) and that still arrives does nothing, so a job never runs as two chains.
|
|
93
|
+
const self = ctx.request?.().id
|
|
94
|
+
if (self && jobState.next_execution_id && self !== jobState.next_execution_id) {
|
|
95
|
+
ctx.console?.log?.(
|
|
96
|
+
`CronJob ${ctx.key}: run ${self} is not the current one (${jobState.next_execution_id}), skipped`,
|
|
97
|
+
)
|
|
98
|
+
return
|
|
99
|
+
}
|
|
77
100
|
|
|
78
101
|
// execute the task
|
|
79
102
|
const { service, method, key, payload } = jobState.request
|
|
@@ -156,6 +179,8 @@ const scheduleNextExecution = async (ctx, request) => {
|
|
|
156
179
|
const jobState = {
|
|
157
180
|
request,
|
|
158
181
|
next_execution_time: next.toString(),
|
|
182
|
+
// The same instant as a number, so `ensure` can tell an overdue run without parsing a date string.
|
|
183
|
+
next_execution_at: next.getTime(),
|
|
159
184
|
next_execution_id: await handle.invocationId,
|
|
160
185
|
}
|
|
161
186
|
ctx.set(JOB_STATE_NAME, jobState)
|
package/package.json
CHANGED
package/test/Cron.test.js
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
ensureCronJob,
|
|
6
6
|
setJobStateName,
|
|
7
7
|
JOB_STATE_NAME,
|
|
8
|
+
OVERDUE_GRACE_MS,
|
|
8
9
|
} from '../lib/Cron.js'
|
|
9
10
|
import { TerminalError } from '@restatedev/restate-sdk'
|
|
10
11
|
|
|
@@ -34,7 +35,7 @@ describe('cronJobInitiator.create', () => {
|
|
|
34
35
|
|
|
35
36
|
// Every cronJob handler that schedules a next run needs the same "date/objectSendClient/set"
|
|
36
37
|
// wiring; this factory keeps the mocked context consistent across tests.
|
|
37
|
-
function makeSchedulingCtx({ existing } = {}) {
|
|
38
|
+
function makeSchedulingCtx({ existing, now = Date.now(), invocationId } = {}) {
|
|
38
39
|
const sets = []
|
|
39
40
|
const cancelled = []
|
|
40
41
|
return {
|
|
@@ -42,7 +43,8 @@ function makeSchedulingCtx({ existing } = {}) {
|
|
|
42
43
|
cancelled,
|
|
43
44
|
cancel: (invocationId) => cancelled.push(invocationId),
|
|
44
45
|
get: async () => existing,
|
|
45
|
-
date: { now: async () =>
|
|
46
|
+
date: { now: async () => now },
|
|
47
|
+
...(invocationId ? { request: () => ({ id: invocationId }) } : {}),
|
|
46
48
|
key: 'job-1',
|
|
47
49
|
objectSendClient: () => ({
|
|
48
50
|
execute: () => ({ invocationId: Promise.resolve('exec-inv-1') }),
|
|
@@ -109,6 +111,37 @@ describe('cronJob.execute', () => {
|
|
|
109
111
|
expect(ctx.sets.length).toBe(1)
|
|
110
112
|
})
|
|
111
113
|
|
|
114
|
+
test("a run that is not the job's current one does nothing and schedules nothing", async () => {
|
|
115
|
+
const sent = []
|
|
116
|
+
const ctx = makeSchedulingCtx({
|
|
117
|
+
invocationId: 'inv-replaced',
|
|
118
|
+
existing: {
|
|
119
|
+
request: { service: 'Svc', method: 'do', cronExpression: '* * * * *' },
|
|
120
|
+
next_execution_id: 'inv-current',
|
|
121
|
+
},
|
|
122
|
+
})
|
|
123
|
+
ctx.genericSend = (opts) => sent.push(opts)
|
|
124
|
+
await cronJob.object.execute(ctx)
|
|
125
|
+
expect(sent).toEqual([])
|
|
126
|
+
expect(ctx.sets).toEqual([])
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
test('the current run works and schedules the next one', async () => {
|
|
130
|
+
const sent = []
|
|
131
|
+
const ctx = makeSchedulingCtx({
|
|
132
|
+
invocationId: 'inv-current',
|
|
133
|
+
existing: {
|
|
134
|
+
request: { service: 'Svc', method: 'do', cronExpression: '* * * * *' },
|
|
135
|
+
next_execution_id: 'inv-current',
|
|
136
|
+
},
|
|
137
|
+
})
|
|
138
|
+
ctx.genericSend = (opts) => sent.push(opts)
|
|
139
|
+
await cronJob.object.execute(ctx)
|
|
140
|
+
expect(sent.length).toBe(1)
|
|
141
|
+
expect(ctx.sets.length).toBe(1)
|
|
142
|
+
expect(ctx.sets[0][1].next_execution_at).toBeNumber()
|
|
143
|
+
})
|
|
144
|
+
|
|
112
145
|
test('throws when there is no job state to execute', async () => {
|
|
113
146
|
const ctx = makeSchedulingCtx({ existing: undefined })
|
|
114
147
|
await expect(cronJob.object.execute(ctx)).rejects.toThrow(TerminalError)
|
|
@@ -179,6 +212,42 @@ describe('cronJob.ensure', () => {
|
|
|
179
212
|
expect(ctx.cancelled).toEqual([])
|
|
180
213
|
})
|
|
181
214
|
|
|
215
|
+
test('the same request with a pending run that is long overdue (lost) schedules a fresh run', async () => {
|
|
216
|
+
const now = Date.parse('2026-10-05T08:00:00Z')
|
|
217
|
+
const existing = {
|
|
218
|
+
request,
|
|
219
|
+
next_execution_time: new Date(now - OVERDUE_GRACE_MS - 1000).toString(),
|
|
220
|
+
next_execution_at: now - OVERDUE_GRACE_MS - 1000,
|
|
221
|
+
next_execution_id: 'inv-killed',
|
|
222
|
+
}
|
|
223
|
+
const ctx = makeSchedulingCtx({ existing, now })
|
|
224
|
+
const state = await cronJob.object.ensure(ctx, request)
|
|
225
|
+
expect(state.next_execution_id).toBe('exec-inv-1')
|
|
226
|
+
expect(state.next_execution_at).toBeGreaterThan(now)
|
|
227
|
+
expect(ctx.cancelled).toEqual(['inv-killed'])
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
test('a state written before next_execution_at existed is judged by its date string', async () => {
|
|
231
|
+
const now = Date.parse('2026-10-05T08:00:00Z')
|
|
232
|
+
const existing = {
|
|
233
|
+
request,
|
|
234
|
+
next_execution_time: new Date(now - 10 * 60_000).toString(),
|
|
235
|
+
next_execution_id: 'inv-killed',
|
|
236
|
+
}
|
|
237
|
+
const ctx = makeSchedulingCtx({ existing, now })
|
|
238
|
+
expect((await cronJob.object.ensure(ctx, request)).next_execution_id).toBe('exec-inv-1')
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
test('a pending run that is due soon, or only a little late, is left alone', async () => {
|
|
242
|
+
const now = Date.parse('2026-10-05T08:00:00Z')
|
|
243
|
+
for (const dueAt of [now + 30_000, now - OVERDUE_GRACE_MS + 1000]) {
|
|
244
|
+
const existing = { request, next_execution_at: dueAt, next_execution_id: 'inv-pending' }
|
|
245
|
+
const ctx = makeSchedulingCtx({ existing, now })
|
|
246
|
+
expect(await cronJob.object.ensure(ctx, request)).toBe(existing)
|
|
247
|
+
expect(ctx.cancelled).toEqual([])
|
|
248
|
+
}
|
|
249
|
+
})
|
|
250
|
+
|
|
182
251
|
test('unknown fields and field order do not make a request "different"', async () => {
|
|
183
252
|
const existing = { request, next_execution_time: 'T', next_execution_id: 'inv-old' }
|
|
184
253
|
const ctx = makeSchedulingCtx({ existing })
|