@platformatic/watt-extra 1.15.0-alpha.1 → 1.15.0-alpha.3
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/cron/cron.mjs +8 -2
- package/lib/watt.js +8 -15
- package/package.json +1 -1
- package/plugins/metadata.js +0 -11
- package/plugins/scheduler.js +42 -30
- package/plugins/update.js +19 -1
- package/test/scheduler-coordination.test.js +82 -24
package/clients/cron/cron.mjs
CHANGED
|
@@ -573,10 +573,16 @@ async function _getIccJobsName (url, request) {
|
|
|
573
573
|
}
|
|
574
574
|
}
|
|
575
575
|
const responseType = response.headers.get('content-type')?.startsWith('application/json') ? 'json' : 'text'
|
|
576
|
+
let body
|
|
577
|
+
try {
|
|
578
|
+
body = await response[responseType]()
|
|
579
|
+
} catch {
|
|
580
|
+
body = null
|
|
581
|
+
}
|
|
576
582
|
return {
|
|
577
583
|
statusCode: response.status,
|
|
578
584
|
headers: headersToJSON(response.headers),
|
|
579
|
-
body
|
|
585
|
+
body
|
|
580
586
|
}
|
|
581
587
|
}
|
|
582
588
|
|
|
@@ -869,4 +875,4 @@ export default function build (url, options) {
|
|
|
869
875
|
getMessagesIdCancel: _getMessagesIdCancel.bind(url, ...arguments),
|
|
870
876
|
putWattJobs: _putWattJobs.bind(url, ...arguments)
|
|
871
877
|
}
|
|
872
|
-
}
|
|
878
|
+
}
|
package/lib/watt.js
CHANGED
|
@@ -504,14 +504,7 @@ class Watt {
|
|
|
504
504
|
return
|
|
505
505
|
}
|
|
506
506
|
|
|
507
|
-
|
|
508
|
-
// ('external' mode): the jobs are registered and triggered by ICC, so
|
|
509
|
-
// running them locally would cause concurrent executions across pods.
|
|
510
|
-
// Older ICC versions don't report a scheduler mode but still register and
|
|
511
|
-
// trigger the jobs, so we keep disabling for backward compatibility.
|
|
512
|
-
// In 'local' mode ICC coordination is off and the jobs run locally.
|
|
513
|
-
const mode = this.#instanceConfig?.scheduler?.mode ?? 'external'
|
|
514
|
-
if (mode !== 'external') {
|
|
507
|
+
if (!this.#env.PLT_ICC_URL) {
|
|
515
508
|
return
|
|
516
509
|
}
|
|
517
510
|
|
|
@@ -538,19 +531,19 @@ class Watt {
|
|
|
538
531
|
return
|
|
539
532
|
}
|
|
540
533
|
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
534
|
+
if (!this.#env.PLT_ICC_URL) {
|
|
535
|
+
return
|
|
536
|
+
}
|
|
537
|
+
|
|
544
538
|
const jobs = await runtime.getScheduler()
|
|
545
|
-
const operation = mode === 'external' ? 'pauseSchedulerJob' : 'resumeSchedulerJob'
|
|
546
539
|
|
|
547
540
|
await Promise.all(
|
|
548
541
|
jobs
|
|
549
|
-
.filter(job => job.paused
|
|
550
|
-
.map(job => runtime
|
|
542
|
+
.filter(job => !job.paused)
|
|
543
|
+
.map(job => runtime.pauseSchedulerJob(job.name))
|
|
551
544
|
)
|
|
552
545
|
|
|
553
|
-
this.#logger.info({
|
|
546
|
+
this.#logger.info({ jobs: jobs.map(job => job.name) }, 'Runtime scheduler jobs paused for ICC coordination')
|
|
554
547
|
}
|
|
555
548
|
|
|
556
549
|
async #configureServices (runtime) {
|
package/package.json
CHANGED
package/plugins/metadata.js
CHANGED
|
@@ -56,16 +56,6 @@ async function metadata (app, _opts) {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
// Report the cron jobs defined in the runtime (scheduler config and
|
|
60
|
-
// application-level scheduled tasks), so that ICC can register and
|
|
61
|
-
// coordinate them centrally. Failures must not prevent the state report.
|
|
62
|
-
let scheduler = null
|
|
63
|
-
try {
|
|
64
|
-
scheduler = await app.collectSchedulerJobs()
|
|
65
|
-
} catch (error) {
|
|
66
|
-
app.log.warn(error, 'Failed to collect the scheduler jobs, not reporting them')
|
|
67
|
-
}
|
|
68
|
-
|
|
69
59
|
try {
|
|
70
60
|
// There is a better way? We need to set the default headers for the client
|
|
71
61
|
// every time, because the token might be expired
|
|
@@ -75,7 +65,6 @@ async function metadata (app, _opts) {
|
|
|
75
65
|
id: app.instanceId,
|
|
76
66
|
services,
|
|
77
67
|
metadata: runtimeMetadata,
|
|
78
|
-
...(scheduler ? { scheduler } : {})
|
|
79
68
|
}, {
|
|
80
69
|
headers: await app.getAuthorizationHeaders()
|
|
81
70
|
})
|
package/plugins/scheduler.js
CHANGED
|
@@ -82,21 +82,8 @@ async function scheduler (app, _opts) {
|
|
|
82
82
|
return
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (mode === 'local') {
|
|
88
|
-
await app.watt.applySchedulerMode()
|
|
89
|
-
app.log.info('ICC scheduler coordination is disabled, jobs will run locally')
|
|
90
|
-
return
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (mode === 'external') {
|
|
94
|
-
await app.watt.applySchedulerMode()
|
|
95
|
-
app.log.info('Scheduler coordination delegated to ICC')
|
|
96
|
-
return
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Older ICC versions: register each job directly on the cron service
|
|
85
|
+
// Register jobs directly on the cron service. Config jobs retain their
|
|
86
|
+
// callbacks; application jobs are mapped by new ICC versions to Main.
|
|
100
87
|
try {
|
|
101
88
|
const applicationId = app.instanceConfig?.applicationId
|
|
102
89
|
const { default: build, setDefaultHeaders } = await import('../clients/cron/cron.mjs')
|
|
@@ -109,36 +96,61 @@ async function scheduler (app, _opts) {
|
|
|
109
96
|
const cronClient = build(cronUrl)
|
|
110
97
|
setDefaultHeaders(await app.getAuthorizationHeaders())
|
|
111
98
|
|
|
112
|
-
const jobs =
|
|
99
|
+
const jobs = await collectSchedulerJobs()
|
|
100
|
+
const configJobs = jobs.filter((job) => job.source === 'config')
|
|
101
|
+
const applicationJobs = jobs.filter((job) => job.source === 'application')
|
|
113
102
|
|
|
114
|
-
|
|
115
|
-
for (const job of jobs) {
|
|
103
|
+
async function saveJob (job) {
|
|
116
104
|
const iccJob = {
|
|
117
105
|
name: job.name,
|
|
118
|
-
|
|
119
|
-
schedule: job.cron, // unfortunately, the ICC API uses `schedule` instead of `cron`
|
|
106
|
+
schedule: job.cron,
|
|
120
107
|
method: job.method,
|
|
121
108
|
maxRetries: job.maxRetries,
|
|
122
109
|
applicationId
|
|
123
110
|
}
|
|
124
|
-
|
|
125
|
-
|
|
111
|
+
|
|
112
|
+
if (job.source === 'application') {
|
|
113
|
+
iccJob.source = job.source
|
|
114
|
+
iccJob.scheduleId = job.scheduleId
|
|
115
|
+
iccJob.tasks = job.tasks
|
|
116
|
+
} else {
|
|
117
|
+
iccJob.callbackUrl = job.callbackUrl
|
|
118
|
+
if (job.headers && Object.keys(job.headers).length > 0) {
|
|
119
|
+
iccJob.headers = job.headers
|
|
120
|
+
}
|
|
121
|
+
if (job.body && (typeof job.body === 'string' ? job.body.length > 0 : Object.keys(job.body).length > 0)) {
|
|
122
|
+
iccJob.body = job.body
|
|
123
|
+
}
|
|
126
124
|
}
|
|
127
|
-
|
|
128
|
-
|
|
125
|
+
|
|
126
|
+
const result = await cronClient.putWattJobs(iccJob)
|
|
127
|
+
if (result.statusCode >= 400) {
|
|
128
|
+
const error = new Error(`ICC returned HTTP ${result.statusCode} while saving job "${job.name}"`)
|
|
129
|
+
error.statusCode = result.statusCode
|
|
130
|
+
throw error
|
|
129
131
|
}
|
|
130
|
-
saveJobs.push(cronClient.putWattJobs(iccJob))
|
|
131
132
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
133
|
+
|
|
134
|
+
for (const job of configJobs) {
|
|
135
|
+
await saveJob(job)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
for (const [index, job] of applicationJobs.entries()) {
|
|
139
|
+
try {
|
|
140
|
+
await saveJob(job)
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (index === 0 && error.statusCode === 400) {
|
|
143
|
+
app.log.warn('ICC does not support application scheduler jobs, skipping application registrations')
|
|
144
|
+
break
|
|
145
|
+
}
|
|
146
|
+
throw error
|
|
147
|
+
}
|
|
137
148
|
}
|
|
138
149
|
|
|
139
150
|
app.log.info('Scheduler configured')
|
|
140
151
|
} catch (error) {
|
|
141
152
|
app.log.error(error, 'Failed in configuring watt jobs in ICC')
|
|
153
|
+
throw error
|
|
142
154
|
}
|
|
143
155
|
}
|
|
144
156
|
|
package/plugins/update.js
CHANGED
|
@@ -41,7 +41,25 @@ async function updatePlugin (app) {
|
|
|
41
41
|
// each tick to exactly one pod of the application, this one.
|
|
42
42
|
if (command === 'run-scheduled-job') {
|
|
43
43
|
app.log.info({ command, job: message.params?.name }, 'Received run-scheduled-job command from ICC')
|
|
44
|
-
|
|
44
|
+
try {
|
|
45
|
+
const result = await app.runScheduledJob(message.params ?? {})
|
|
46
|
+
if (message.requestId) {
|
|
47
|
+
socket.send(JSON.stringify({
|
|
48
|
+
requestId: message.requestId,
|
|
49
|
+
success: result?.success !== false,
|
|
50
|
+
result
|
|
51
|
+
}))
|
|
52
|
+
}
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (message.requestId) {
|
|
55
|
+
socket.send(JSON.stringify({
|
|
56
|
+
requestId: message.requestId,
|
|
57
|
+
success: false,
|
|
58
|
+
error: { message: error.message, name: error.name }
|
|
59
|
+
}))
|
|
60
|
+
}
|
|
61
|
+
throw error
|
|
62
|
+
}
|
|
45
63
|
return
|
|
46
64
|
}
|
|
47
65
|
|
|
@@ -34,7 +34,7 @@ function controlPlaneResponseWithMode (applicationId, applicationName, mode) {
|
|
|
34
34
|
})
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
test('
|
|
37
|
+
test('ICC coordination: should disable local jobs and register config jobs directly', async (t) => {
|
|
38
38
|
const applicationName = 'test-app'
|
|
39
39
|
const applicationId = randomUUID()
|
|
40
40
|
const applicationPath = join(__dirname, 'fixtures', 'runtime-scheduler')
|
|
@@ -77,21 +77,21 @@ test('external mode: should disable local jobs, skip legacy registration and rep
|
|
|
77
77
|
assert.strictEqual(config.scheduler[0].enabled, false)
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
assert.deepStrictEqual(savedWattJob, {
|
|
81
|
+
name: 'test',
|
|
82
|
+
schedule: '*/5 * * * *',
|
|
83
|
+
method: 'GET',
|
|
84
|
+
maxRetries: 3,
|
|
85
|
+
applicationId,
|
|
86
|
+
callbackUrl: 'http://localhost:3000'
|
|
87
|
+
})
|
|
83
88
|
|
|
84
89
|
// The jobs are reported in the application state
|
|
85
90
|
assert.ok(savedState, 'the state should have been reported')
|
|
86
|
-
assert.
|
|
87
|
-
assert.strictEqual(savedState.scheduler.length, 1)
|
|
88
|
-
assert.strictEqual(savedState.scheduler[0].name, 'test')
|
|
89
|
-
assert.strictEqual(savedState.scheduler[0].cron, '*/5 * * * *')
|
|
90
|
-
assert.strictEqual(savedState.scheduler[0].callbackUrl, 'http://localhost:3000')
|
|
91
|
-
assert.strictEqual(savedState.scheduler[0].source, 'config')
|
|
91
|
+
assert.strictEqual(savedState.scheduler, undefined)
|
|
92
92
|
})
|
|
93
93
|
|
|
94
|
-
test('
|
|
94
|
+
test('ICC coordination: should disable local jobs regardless of scheduler mode', async (t) => {
|
|
95
95
|
const applicationName = 'test-app'
|
|
96
96
|
const applicationId = randomUUID()
|
|
97
97
|
const applicationPath = join(__dirname, 'fixtures', 'runtime-scheduler')
|
|
@@ -120,15 +120,11 @@ test('local mode: should keep the local jobs running and skip any registration',
|
|
|
120
120
|
await icc.close()
|
|
121
121
|
})
|
|
122
122
|
|
|
123
|
-
// ICC coordination
|
|
123
|
+
// ICC coordination always disables local execution.
|
|
124
124
|
const config = await app.watt.runtime.getRuntimeConfig()
|
|
125
|
-
assert.
|
|
126
|
-
if (typeof app.watt.runtime.getScheduler === 'function') {
|
|
127
|
-
assert.strictEqual(app.watt.runtime.getScheduler()[0].paused, false)
|
|
128
|
-
}
|
|
125
|
+
assert.strictEqual(config.scheduler[0].enabled, false)
|
|
129
126
|
|
|
130
|
-
|
|
131
|
-
assert.strictEqual(savedWattJob, null)
|
|
127
|
+
assert.ok(savedWattJob)
|
|
132
128
|
})
|
|
133
129
|
|
|
134
130
|
function createMockApp (port) {
|
|
@@ -152,8 +148,8 @@ function createMockApp (port) {
|
|
|
152
148
|
}
|
|
153
149
|
}
|
|
154
150
|
|
|
155
|
-
test('runtime scheduler ownership
|
|
156
|
-
const instanceConfig = {
|
|
151
|
+
test('runtime scheduler ownership always pauses jobs when ICC is configured', async () => {
|
|
152
|
+
const instanceConfig = {}
|
|
157
153
|
const jobs = [
|
|
158
154
|
{ name: 'configured', paused: false },
|
|
159
155
|
{ name: 'frontend:0', paused: false }
|
|
@@ -181,9 +177,8 @@ test('runtime scheduler ownership follows the ICC mode', async () => {
|
|
|
181
177
|
await watt.applySchedulerMode()
|
|
182
178
|
assert.deepStrictEqual(jobs.map(job => job.paused), [true, true])
|
|
183
179
|
|
|
184
|
-
instanceConfig.scheduler.mode = 'local'
|
|
185
180
|
await watt.applySchedulerMode()
|
|
186
|
-
assert.deepStrictEqual(jobs.map(job => job.paused), [
|
|
181
|
+
assert.deepStrictEqual(jobs.map(job => job.paused), [true, true])
|
|
187
182
|
})
|
|
188
183
|
|
|
189
184
|
function setupMockIccServer (wss) {
|
|
@@ -237,6 +232,7 @@ test('run-scheduled-job: should execute the job callback when triggered over the
|
|
|
237
232
|
|
|
238
233
|
getWs().send(JSON.stringify({
|
|
239
234
|
command: 'run-scheduled-job',
|
|
235
|
+
requestId: 'job-request-1',
|
|
240
236
|
params: {
|
|
241
237
|
name: 'cleanup',
|
|
242
238
|
callbackUrl: `http://127.0.0.1:${target.address().port}/cleanup`,
|
|
@@ -247,6 +243,13 @@ test('run-scheduled-job: should execute the job callback when triggered over the
|
|
|
247
243
|
|
|
248
244
|
await requestReceived
|
|
249
245
|
|
|
246
|
+
const response = JSON.parse((await once(getWs(), 'message'))[0].toString())
|
|
247
|
+
assert.deepStrictEqual(response, {
|
|
248
|
+
requestId: 'job-request-1',
|
|
249
|
+
success: true,
|
|
250
|
+
result: { name: 'cleanup', statusCode: 200 }
|
|
251
|
+
})
|
|
252
|
+
|
|
250
253
|
assert.deepStrictEqual(receivedRequests, [{ method: 'POST', url: '/cleanup' }])
|
|
251
254
|
})
|
|
252
255
|
|
|
@@ -347,8 +350,63 @@ test('collectSchedulerJobs: should use the runtime scheduler status when availab
|
|
|
347
350
|
assert.deepStrictEqual(jobs[1].tasks, ['db:cleanup'])
|
|
348
351
|
})
|
|
349
352
|
|
|
350
|
-
test('
|
|
353
|
+
test('sendSchedulerInfo: should register config jobs before skipping application jobs on old ICC', async () => {
|
|
354
|
+
const app = createMockApp(0)
|
|
355
|
+
app.instanceConfig.iccServices = { cron: { url: 'http://cron.local' } }
|
|
356
|
+
app.watt.runtime.getScheduler = async () => ([
|
|
357
|
+
{
|
|
358
|
+
name: 'config-job',
|
|
359
|
+
cron: '*/5 * * * *',
|
|
360
|
+
callbackUrl: 'http://service.plt.local/run',
|
|
361
|
+
source: 'config'
|
|
362
|
+
},
|
|
363
|
+
{
|
|
364
|
+
name: 'application:0',
|
|
365
|
+
cron: '* * * * *',
|
|
366
|
+
source: 'application',
|
|
367
|
+
applicationId: 'frontend',
|
|
368
|
+
scheduleId: '0',
|
|
369
|
+
tasks: ['cleanup']
|
|
370
|
+
},
|
|
371
|
+
{
|
|
372
|
+
name: 'application:1',
|
|
373
|
+
cron: '0 * * * *',
|
|
374
|
+
source: 'application',
|
|
375
|
+
applicationId: 'frontend',
|
|
376
|
+
scheduleId: '1',
|
|
377
|
+
tasks: ['sync']
|
|
378
|
+
}
|
|
379
|
+
])
|
|
380
|
+
|
|
381
|
+
const requests = []
|
|
382
|
+
const originalFetch = globalThis.fetch
|
|
383
|
+
globalThis.fetch = async (_url, options) => {
|
|
384
|
+
requests.push(JSON.parse(options.body))
|
|
385
|
+
const status = requests.length === 2 ? 400 : 200
|
|
386
|
+
return {
|
|
387
|
+
status,
|
|
388
|
+
headers: new Headers({ 'content-type': 'application/json' }),
|
|
389
|
+
text: async () => '',
|
|
390
|
+
json: async () => ({ error: 'unsupported' })
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
try {
|
|
395
|
+
await schedulerPlugin(app)
|
|
396
|
+
await app.sendSchedulerInfo()
|
|
397
|
+
} finally {
|
|
398
|
+
globalThis.fetch = originalFetch
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
assert.strictEqual(requests.length, 2)
|
|
402
|
+
assert.strictEqual(requests[0].callbackUrl, 'http://service.plt.local/run')
|
|
403
|
+
assert.strictEqual(requests[1].callbackUrl, undefined)
|
|
404
|
+
assert.deepStrictEqual(requests[1].tasks, ['cleanup'])
|
|
405
|
+
})
|
|
406
|
+
|
|
407
|
+
test('without ICC: should not apply runtime scheduler ownership', async (t) => {
|
|
351
408
|
const app = createMockApp(0)
|
|
409
|
+
app.env.PLT_ICC_URL = undefined
|
|
352
410
|
|
|
353
411
|
let applications = 0
|
|
354
412
|
app.watt.applySchedulerMode = async () => {
|
|
@@ -358,5 +416,5 @@ test('external mode: should apply runtime scheduler ownership', async (t) => {
|
|
|
358
416
|
await schedulerPlugin(app)
|
|
359
417
|
await app.sendSchedulerInfo()
|
|
360
418
|
|
|
361
|
-
assert.strictEqual(applications,
|
|
419
|
+
assert.strictEqual(applications, 0)
|
|
362
420
|
})
|