@platformatic/watt-extra 1.15.0-alpha.2 → 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 +17 -35
- package/test/scheduler-coordination.test.js +81 -122
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
|
@@ -13,28 +13,12 @@ function createWebSocketUrl (httpUrl, path, queryParams = {}) {
|
|
|
13
13
|
return url.toString()
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
async function sendCommandResult (connection, requestId, result) {
|
|
17
|
-
if (connection.readyState !== WebSocket.OPEN) {
|
|
18
|
-
throw new Error(`Cannot send command result for request "${requestId}": WebSocket is not open`)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
await new Promise((resolve, reject) => {
|
|
22
|
-
connection.send(JSON.stringify({ command: 'command-result', requestId, ...result }), err => {
|
|
23
|
-
if (err) {
|
|
24
|
-
reject(err)
|
|
25
|
-
} else {
|
|
26
|
-
resolve()
|
|
27
|
-
}
|
|
28
|
-
})
|
|
29
|
-
})
|
|
30
|
-
}
|
|
31
|
-
|
|
32
16
|
async function updatePlugin (app) {
|
|
33
17
|
const reconnectInterval = app.env.PLT_UPDATES_RECONNECT_INTERVAL_SEC * 1000
|
|
34
18
|
|
|
35
19
|
let socket = null
|
|
36
20
|
|
|
37
|
-
async function processMessage (
|
|
21
|
+
async function processMessage (data) {
|
|
38
22
|
try {
|
|
39
23
|
const message = JSON.parse(data)
|
|
40
24
|
const { topic, type, command } = message
|
|
@@ -57,26 +41,24 @@ async function updatePlugin (app) {
|
|
|
57
41
|
// each tick to exactly one pod of the application, this one.
|
|
58
42
|
if (command === 'run-scheduled-job') {
|
|
59
43
|
app.log.info({ command, job: message.params?.name }, 'Received run-scheduled-job command from ICC')
|
|
60
|
-
|
|
61
44
|
try {
|
|
62
45
|
const result = await app.runScheduledJob(message.params ?? {})
|
|
63
|
-
if (
|
|
64
|
-
|
|
46
|
+
if (message.requestId) {
|
|
47
|
+
socket.send(JSON.stringify({
|
|
48
|
+
requestId: message.requestId,
|
|
49
|
+
success: result?.success !== false,
|
|
50
|
+
result
|
|
51
|
+
}))
|
|
65
52
|
}
|
|
66
|
-
} catch (
|
|
67
|
-
if (
|
|
68
|
-
|
|
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
|
+
}))
|
|
69
60
|
}
|
|
70
|
-
|
|
71
|
-
await sendCommandResult(connection, message.requestId, {
|
|
72
|
-
success: false,
|
|
73
|
-
error: {
|
|
74
|
-
name: err.name,
|
|
75
|
-
message: err.message,
|
|
76
|
-
code: err.code,
|
|
77
|
-
statusCode: err.statusCode
|
|
78
|
-
}
|
|
79
|
-
})
|
|
61
|
+
throw error
|
|
80
62
|
}
|
|
81
63
|
return
|
|
82
64
|
}
|
|
@@ -118,7 +100,7 @@ async function updatePlugin (app) {
|
|
|
118
100
|
try {
|
|
119
101
|
const headers = await app.getAuthorizationHeaders()
|
|
120
102
|
|
|
121
|
-
|
|
103
|
+
socket = new WebSocket(wsUrl, { headers })
|
|
122
104
|
await once(socket, 'open')
|
|
123
105
|
|
|
124
106
|
app.log.info('Connected to updates websocket')
|
|
@@ -135,7 +117,7 @@ async function updatePlugin (app) {
|
|
|
135
117
|
app.log.info('Received subscription acknowledgment from updates websocket')
|
|
136
118
|
|
|
137
119
|
// listen for subsequent messages
|
|
138
|
-
socket.on('message',
|
|
120
|
+
socket.on('message', processMessage)
|
|
139
121
|
|
|
140
122
|
socket.on('error', (err) => {
|
|
141
123
|
app.log.error(err, 'Error in updates websocket connection')
|
|
@@ -4,7 +4,6 @@ import { once } from 'node:events'
|
|
|
4
4
|
import { createServer } from 'node:http'
|
|
5
5
|
import { join, dirname } from 'node:path'
|
|
6
6
|
import { test } from 'node:test'
|
|
7
|
-
import { setImmediate as immediate } from 'node:timers/promises'
|
|
8
7
|
import { fileURLToPath } from 'node:url'
|
|
9
8
|
import WebSocket from 'ws'
|
|
10
9
|
import { start } from '../index.js'
|
|
@@ -35,7 +34,7 @@ function controlPlaneResponseWithMode (applicationId, applicationName, mode) {
|
|
|
35
34
|
})
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
test('
|
|
37
|
+
test('ICC coordination: should disable local jobs and register config jobs directly', async (t) => {
|
|
39
38
|
const applicationName = 'test-app'
|
|
40
39
|
const applicationId = randomUUID()
|
|
41
40
|
const applicationPath = join(__dirname, 'fixtures', 'runtime-scheduler')
|
|
@@ -78,21 +77,21 @@ test('external mode: should disable local jobs, skip legacy registration and rep
|
|
|
78
77
|
assert.strictEqual(config.scheduler[0].enabled, false)
|
|
79
78
|
}
|
|
80
79
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
80
|
+
assert.deepStrictEqual(savedWattJob, {
|
|
81
|
+
name: 'test',
|
|
82
|
+
schedule: '*/5 * * * *',
|
|
83
|
+
method: 'GET',
|
|
84
|
+
maxRetries: 3,
|
|
85
|
+
applicationId,
|
|
86
|
+
callbackUrl: 'http://localhost:3000'
|
|
87
|
+
})
|
|
84
88
|
|
|
85
89
|
// The jobs are reported in the application state
|
|
86
90
|
assert.ok(savedState, 'the state should have been reported')
|
|
87
|
-
assert.
|
|
88
|
-
assert.strictEqual(savedState.scheduler.length, 1)
|
|
89
|
-
assert.strictEqual(savedState.scheduler[0].name, 'test')
|
|
90
|
-
assert.strictEqual(savedState.scheduler[0].cron, '*/5 * * * *')
|
|
91
|
-
assert.strictEqual(savedState.scheduler[0].callbackUrl, 'http://localhost:3000')
|
|
92
|
-
assert.strictEqual(savedState.scheduler[0].source, 'config')
|
|
91
|
+
assert.strictEqual(savedState.scheduler, undefined)
|
|
93
92
|
})
|
|
94
93
|
|
|
95
|
-
test('
|
|
94
|
+
test('ICC coordination: should disable local jobs regardless of scheduler mode', async (t) => {
|
|
96
95
|
const applicationName = 'test-app'
|
|
97
96
|
const applicationId = randomUUID()
|
|
98
97
|
const applicationPath = join(__dirname, 'fixtures', 'runtime-scheduler')
|
|
@@ -121,15 +120,11 @@ test('local mode: should keep the local jobs running and skip any registration',
|
|
|
121
120
|
await icc.close()
|
|
122
121
|
})
|
|
123
122
|
|
|
124
|
-
// ICC coordination
|
|
123
|
+
// ICC coordination always disables local execution.
|
|
125
124
|
const config = await app.watt.runtime.getRuntimeConfig()
|
|
126
|
-
assert.
|
|
127
|
-
if (typeof app.watt.runtime.getScheduler === 'function') {
|
|
128
|
-
assert.strictEqual(app.watt.runtime.getScheduler()[0].paused, false)
|
|
129
|
-
}
|
|
125
|
+
assert.strictEqual(config.scheduler[0].enabled, false)
|
|
130
126
|
|
|
131
|
-
|
|
132
|
-
assert.strictEqual(savedWattJob, null)
|
|
127
|
+
assert.ok(savedWattJob)
|
|
133
128
|
})
|
|
134
129
|
|
|
135
130
|
function createMockApp (port) {
|
|
@@ -153,8 +148,8 @@ function createMockApp (port) {
|
|
|
153
148
|
}
|
|
154
149
|
}
|
|
155
150
|
|
|
156
|
-
test('runtime scheduler ownership
|
|
157
|
-
const instanceConfig = {
|
|
151
|
+
test('runtime scheduler ownership always pauses jobs when ICC is configured', async () => {
|
|
152
|
+
const instanceConfig = {}
|
|
158
153
|
const jobs = [
|
|
159
154
|
{ name: 'configured', paused: false },
|
|
160
155
|
{ name: 'frontend:0', paused: false }
|
|
@@ -182,15 +177,12 @@ test('runtime scheduler ownership follows the ICC mode', async () => {
|
|
|
182
177
|
await watt.applySchedulerMode()
|
|
183
178
|
assert.deepStrictEqual(jobs.map(job => job.paused), [true, true])
|
|
184
179
|
|
|
185
|
-
instanceConfig.scheduler.mode = 'local'
|
|
186
180
|
await watt.applySchedulerMode()
|
|
187
|
-
assert.deepStrictEqual(jobs.map(job => job.paused), [
|
|
181
|
+
assert.deepStrictEqual(jobs.map(job => job.paused), [true, true])
|
|
188
182
|
})
|
|
189
183
|
|
|
190
184
|
function setupMockIccServer (wss) {
|
|
191
185
|
let ws = null
|
|
192
|
-
const clientMessages = []
|
|
193
|
-
const messageWaiters = []
|
|
194
186
|
|
|
195
187
|
const waitForClientSubscription = once(wss, 'connection').then(([socket]) => {
|
|
196
188
|
ws = socket
|
|
@@ -202,29 +194,12 @@ function setupMockIccServer (wss) {
|
|
|
202
194
|
if (message.command === 'subscribe' && message.topic === '/config') {
|
|
203
195
|
socket.send(JSON.stringify({ command: 'ack' }))
|
|
204
196
|
resolve()
|
|
205
|
-
return
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
const waiter = messageWaiters.shift()
|
|
209
|
-
if (waiter) {
|
|
210
|
-
waiter(message)
|
|
211
|
-
} else {
|
|
212
|
-
clientMessages.push(message)
|
|
213
197
|
}
|
|
214
198
|
})
|
|
215
199
|
})
|
|
216
200
|
})
|
|
217
201
|
|
|
218
|
-
|
|
219
|
-
const message = clientMessages.shift()
|
|
220
|
-
if (message) {
|
|
221
|
-
return Promise.resolve(message)
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
return new Promise(resolve => messageWaiters.push(resolve))
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
return { waitForClientSubscription, waitForClientMessage, getClientMessages: () => clientMessages, getWs: () => ws }
|
|
202
|
+
return { waitForClientSubscription, getWs: () => ws }
|
|
228
203
|
}
|
|
229
204
|
|
|
230
205
|
test('run-scheduled-job: should execute the job callback when triggered over the websocket', async (t) => {
|
|
@@ -234,7 +209,7 @@ test('run-scheduled-job: should execute the job callback when triggered over the
|
|
|
234
209
|
const wss = new WebSocket.Server({ port })
|
|
235
210
|
t.after(() => wss.close())
|
|
236
211
|
|
|
237
|
-
const { waitForClientSubscription,
|
|
212
|
+
const { waitForClientSubscription, getWs } = setupMockIccServer(wss)
|
|
238
213
|
|
|
239
214
|
// The target the job callback should hit
|
|
240
215
|
const receivedRequests = []
|
|
@@ -257,7 +232,7 @@ test('run-scheduled-job: should execute the job callback when triggered over the
|
|
|
257
232
|
|
|
258
233
|
getWs().send(JSON.stringify({
|
|
259
234
|
command: 'run-scheduled-job',
|
|
260
|
-
requestId: 'request-1',
|
|
235
|
+
requestId: 'job-request-1',
|
|
261
236
|
params: {
|
|
262
237
|
name: 'cleanup',
|
|
263
238
|
callbackUrl: `http://127.0.0.1:${target.address().port}/cleanup`,
|
|
@@ -267,86 +242,15 @@ test('run-scheduled-job: should execute the job callback when triggered over the
|
|
|
267
242
|
}))
|
|
268
243
|
|
|
269
244
|
await requestReceived
|
|
270
|
-
const completion = await waitForClientMessage()
|
|
271
245
|
|
|
272
|
-
|
|
273
|
-
assert.deepStrictEqual(
|
|
274
|
-
|
|
275
|
-
requestId: 'request-1',
|
|
246
|
+
const response = JSON.parse((await once(getWs(), 'message'))[0].toString())
|
|
247
|
+
assert.deepStrictEqual(response, {
|
|
248
|
+
requestId: 'job-request-1',
|
|
276
249
|
success: true,
|
|
277
250
|
result: { name: 'cleanup', statusCode: 200 }
|
|
278
251
|
})
|
|
279
|
-
})
|
|
280
|
-
|
|
281
|
-
test('run-scheduled-job: should report runtime failures over the websocket', async (t) => {
|
|
282
|
-
setUpEnvironment()
|
|
283
|
-
|
|
284
|
-
const wss = new WebSocket.Server({ port: 0 })
|
|
285
|
-
await once(wss, 'listening')
|
|
286
|
-
t.after(() => wss.close())
|
|
287
|
-
|
|
288
|
-
const { waitForClientSubscription, waitForClientMessage, getWs } = setupMockIccServer(wss)
|
|
289
|
-
const app = createMockApp(wss.address().port)
|
|
290
|
-
const error = new Error('Task execution failed')
|
|
291
|
-
error.code = 'PLT_TASK_FAILED'
|
|
292
|
-
error.statusCode = 500
|
|
293
|
-
app.watt.runtime.runSchedulerJob = async () => { throw error }
|
|
294
|
-
|
|
295
|
-
await schedulerPlugin(app)
|
|
296
|
-
await updatePlugin(app)
|
|
297
|
-
await app.connectToUpdates()
|
|
298
|
-
t.after(() => app.closeUpdates())
|
|
299
|
-
await waitForClientSubscription
|
|
300
|
-
|
|
301
|
-
getWs().send(JSON.stringify({
|
|
302
|
-
command: 'run-scheduled-job',
|
|
303
|
-
requestId: 'request-2',
|
|
304
|
-
params: { name: 'frontend:0', source: 'application' }
|
|
305
|
-
}))
|
|
306
|
-
|
|
307
|
-
assert.deepStrictEqual(await waitForClientMessage(), {
|
|
308
|
-
command: 'command-result',
|
|
309
|
-
requestId: 'request-2',
|
|
310
|
-
success: false,
|
|
311
|
-
error: {
|
|
312
|
-
name: 'Error',
|
|
313
|
-
message: 'Task execution failed',
|
|
314
|
-
code: 'PLT_TASK_FAILED',
|
|
315
|
-
statusCode: 500
|
|
316
|
-
}
|
|
317
|
-
})
|
|
318
|
-
})
|
|
319
|
-
|
|
320
|
-
test('run-scheduled-job: should not report completion without a request ID', async (t) => {
|
|
321
|
-
setUpEnvironment()
|
|
322
|
-
|
|
323
|
-
const wss = new WebSocket.Server({ port: 0 })
|
|
324
|
-
await once(wss, 'listening')
|
|
325
|
-
t.after(() => wss.close())
|
|
326
|
-
|
|
327
|
-
const { waitForClientSubscription, getClientMessages, getWs } = setupMockIccServer(wss)
|
|
328
|
-
const app = createMockApp(wss.address().port)
|
|
329
|
-
let executedResolve
|
|
330
|
-
const executed = new Promise(resolve => { executedResolve = resolve })
|
|
331
|
-
app.watt.runtime.runSchedulerJob = async () => {
|
|
332
|
-
executedResolve()
|
|
333
|
-
return { success: true }
|
|
334
|
-
}
|
|
335
252
|
|
|
336
|
-
|
|
337
|
-
await updatePlugin(app)
|
|
338
|
-
await app.connectToUpdates()
|
|
339
|
-
t.after(() => app.closeUpdates())
|
|
340
|
-
await waitForClientSubscription
|
|
341
|
-
|
|
342
|
-
getWs().send(JSON.stringify({
|
|
343
|
-
command: 'run-scheduled-job',
|
|
344
|
-
params: { name: 'frontend:0', source: 'application' }
|
|
345
|
-
}))
|
|
346
|
-
|
|
347
|
-
await executed
|
|
348
|
-
await immediate()
|
|
349
|
-
assert.deepStrictEqual(getClientMessages(), [])
|
|
253
|
+
assert.deepStrictEqual(receivedRequests, [{ method: 'POST', url: '/cleanup' }])
|
|
350
254
|
})
|
|
351
255
|
|
|
352
256
|
test('run-scheduled-job: should route legacy internal callbacks through the runtime', async (t) => {
|
|
@@ -446,8 +350,63 @@ test('collectSchedulerJobs: should use the runtime scheduler status when availab
|
|
|
446
350
|
assert.deepStrictEqual(jobs[1].tasks, ['db:cleanup'])
|
|
447
351
|
})
|
|
448
352
|
|
|
449
|
-
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) => {
|
|
450
408
|
const app = createMockApp(0)
|
|
409
|
+
app.env.PLT_ICC_URL = undefined
|
|
451
410
|
|
|
452
411
|
let applications = 0
|
|
453
412
|
app.watt.applySchedulerMode = async () => {
|
|
@@ -457,5 +416,5 @@ test('external mode: should apply runtime scheduler ownership', async (t) => {
|
|
|
457
416
|
await schedulerPlugin(app)
|
|
458
417
|
await app.sendSchedulerInfo()
|
|
459
418
|
|
|
460
|
-
assert.strictEqual(applications,
|
|
419
|
+
assert.strictEqual(applications, 0)
|
|
461
420
|
})
|