@platformatic/watt-extra 1.15.0-alpha.1 → 1.15.0-alpha.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.
- package/package.json +1 -1
- package/plugins/update.js +40 -4
- package/test/scheduler-coordination.test.js +101 -2
package/package.json
CHANGED
package/plugins/update.js
CHANGED
|
@@ -13,12 +13,28 @@ 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
|
+
|
|
16
32
|
async function updatePlugin (app) {
|
|
17
33
|
const reconnectInterval = app.env.PLT_UPDATES_RECONNECT_INTERVAL_SEC * 1000
|
|
18
34
|
|
|
19
35
|
let socket = null
|
|
20
36
|
|
|
21
|
-
async function processMessage (data) {
|
|
37
|
+
async function processMessage (connection, data) {
|
|
22
38
|
try {
|
|
23
39
|
const message = JSON.parse(data)
|
|
24
40
|
const { topic, type, command } = message
|
|
@@ -41,7 +57,27 @@ async function updatePlugin (app) {
|
|
|
41
57
|
// each tick to exactly one pod of the application, this one.
|
|
42
58
|
if (command === 'run-scheduled-job') {
|
|
43
59
|
app.log.info({ command, job: message.params?.name }, 'Received run-scheduled-job command from ICC')
|
|
44
|
-
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
const result = await app.runScheduledJob(message.params ?? {})
|
|
63
|
+
if (typeof message.requestId === 'string') {
|
|
64
|
+
await sendCommandResult(connection, message.requestId, { success: true, result })
|
|
65
|
+
}
|
|
66
|
+
} catch (err) {
|
|
67
|
+
if (typeof message.requestId !== 'string') {
|
|
68
|
+
throw err
|
|
69
|
+
}
|
|
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
|
+
})
|
|
80
|
+
}
|
|
45
81
|
return
|
|
46
82
|
}
|
|
47
83
|
|
|
@@ -82,7 +118,7 @@ async function updatePlugin (app) {
|
|
|
82
118
|
try {
|
|
83
119
|
const headers = await app.getAuthorizationHeaders()
|
|
84
120
|
|
|
85
|
-
socket = new WebSocket(wsUrl, { headers })
|
|
121
|
+
const socket = new WebSocket(wsUrl, { headers })
|
|
86
122
|
await once(socket, 'open')
|
|
87
123
|
|
|
88
124
|
app.log.info('Connected to updates websocket')
|
|
@@ -99,7 +135,7 @@ async function updatePlugin (app) {
|
|
|
99
135
|
app.log.info('Received subscription acknowledgment from updates websocket')
|
|
100
136
|
|
|
101
137
|
// listen for subsequent messages
|
|
102
|
-
socket.on('message', processMessage)
|
|
138
|
+
socket.on('message', data => processMessage(socket, data))
|
|
103
139
|
|
|
104
140
|
socket.on('error', (err) => {
|
|
105
141
|
app.log.error(err, 'Error in updates websocket connection')
|
|
@@ -4,6 +4,7 @@ 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'
|
|
7
8
|
import { fileURLToPath } from 'node:url'
|
|
8
9
|
import WebSocket from 'ws'
|
|
9
10
|
import { start } from '../index.js'
|
|
@@ -188,6 +189,8 @@ test('runtime scheduler ownership follows the ICC mode', async () => {
|
|
|
188
189
|
|
|
189
190
|
function setupMockIccServer (wss) {
|
|
190
191
|
let ws = null
|
|
192
|
+
const clientMessages = []
|
|
193
|
+
const messageWaiters = []
|
|
191
194
|
|
|
192
195
|
const waitForClientSubscription = once(wss, 'connection').then(([socket]) => {
|
|
193
196
|
ws = socket
|
|
@@ -199,12 +202,29 @@ function setupMockIccServer (wss) {
|
|
|
199
202
|
if (message.command === 'subscribe' && message.topic === '/config') {
|
|
200
203
|
socket.send(JSON.stringify({ command: 'ack' }))
|
|
201
204
|
resolve()
|
|
205
|
+
return
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const waiter = messageWaiters.shift()
|
|
209
|
+
if (waiter) {
|
|
210
|
+
waiter(message)
|
|
211
|
+
} else {
|
|
212
|
+
clientMessages.push(message)
|
|
202
213
|
}
|
|
203
214
|
})
|
|
204
215
|
})
|
|
205
216
|
})
|
|
206
217
|
|
|
207
|
-
|
|
218
|
+
function waitForClientMessage () {
|
|
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 }
|
|
208
228
|
}
|
|
209
229
|
|
|
210
230
|
test('run-scheduled-job: should execute the job callback when triggered over the websocket', async (t) => {
|
|
@@ -214,7 +234,7 @@ test('run-scheduled-job: should execute the job callback when triggered over the
|
|
|
214
234
|
const wss = new WebSocket.Server({ port })
|
|
215
235
|
t.after(() => wss.close())
|
|
216
236
|
|
|
217
|
-
const { waitForClientSubscription, getWs } = setupMockIccServer(wss)
|
|
237
|
+
const { waitForClientSubscription, waitForClientMessage, getWs } = setupMockIccServer(wss)
|
|
218
238
|
|
|
219
239
|
// The target the job callback should hit
|
|
220
240
|
const receivedRequests = []
|
|
@@ -237,6 +257,7 @@ test('run-scheduled-job: should execute the job callback when triggered over the
|
|
|
237
257
|
|
|
238
258
|
getWs().send(JSON.stringify({
|
|
239
259
|
command: 'run-scheduled-job',
|
|
260
|
+
requestId: 'request-1',
|
|
240
261
|
params: {
|
|
241
262
|
name: 'cleanup',
|
|
242
263
|
callbackUrl: `http://127.0.0.1:${target.address().port}/cleanup`,
|
|
@@ -246,8 +267,86 @@ test('run-scheduled-job: should execute the job callback when triggered over the
|
|
|
246
267
|
}))
|
|
247
268
|
|
|
248
269
|
await requestReceived
|
|
270
|
+
const completion = await waitForClientMessage()
|
|
249
271
|
|
|
250
272
|
assert.deepStrictEqual(receivedRequests, [{ method: 'POST', url: '/cleanup' }])
|
|
273
|
+
assert.deepStrictEqual(completion, {
|
|
274
|
+
command: 'command-result',
|
|
275
|
+
requestId: 'request-1',
|
|
276
|
+
success: true,
|
|
277
|
+
result: { name: 'cleanup', statusCode: 200 }
|
|
278
|
+
})
|
|
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
|
+
|
|
336
|
+
await schedulerPlugin(app)
|
|
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(), [])
|
|
251
350
|
})
|
|
252
351
|
|
|
253
352
|
test('run-scheduled-job: should route legacy internal callbacks through the runtime', async (t) => {
|