@platformatic/watt-extra 1.14.3 → 1.15.0-alpha.1
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/cli.js +35 -4
- package/lib/watt.js +48 -2
- package/package.json +7 -7
- package/plugins/metadata.js +12 -1
- package/plugins/scheduler.js +109 -7
- package/plugins/update.js +8 -0
- package/pnpm-workspace.yaml +5 -0
- package/test/cli.test.js +1 -50
- package/test/scheduler-coordination.test.js +362 -0
- package/.claude/settings.local.json +0 -24
- package/lib/start-args.js +0 -47
package/cli.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import commist from 'commist'
|
|
3
|
-
import
|
|
3
|
+
import minimist from 'minimist'
|
|
4
|
+
import { resolve, join, dirname } from 'node:path'
|
|
4
5
|
import { fileURLToPath } from 'node:url'
|
|
5
6
|
import helpMeInit from 'help-me'
|
|
6
7
|
import { readFileSync } from 'node:fs'
|
|
7
8
|
import { start, logger } from './index.js'
|
|
8
9
|
import { getSimpleBanner } from './lib/banner.js'
|
|
9
|
-
import { applyStartArgs } from './lib/start-args.js'
|
|
10
10
|
|
|
11
11
|
const __filename = fileURLToPath(import.meta.url)
|
|
12
12
|
const __dirname = dirname(__filename)
|
|
@@ -35,15 +35,46 @@ async function startCommand (argv) {
|
|
|
35
35
|
logger.info(`WattExtra v${pkg.version}`)
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
const
|
|
38
|
+
const args = minimist(argv, {
|
|
39
|
+
alias: {
|
|
40
|
+
h: 'help',
|
|
41
|
+
l: 'log-level',
|
|
42
|
+
i: 'icc-url',
|
|
43
|
+
a: 'app-name',
|
|
44
|
+
d: 'app-dir'
|
|
45
|
+
},
|
|
46
|
+
boolean: ['help'],
|
|
47
|
+
string: ['log-level', 'icc-url', 'app-name', 'app-dir'],
|
|
48
|
+
default: {
|
|
49
|
+
'log-level': 'info',
|
|
50
|
+
'app-dir': process.cwd()
|
|
51
|
+
}
|
|
52
|
+
})
|
|
39
53
|
|
|
40
54
|
logger.debug({ args, argv }, 'Start command arguments')
|
|
41
55
|
|
|
42
|
-
if (help) {
|
|
56
|
+
if (args.help) {
|
|
43
57
|
helpMe.toStdout('start')
|
|
44
58
|
return true
|
|
45
59
|
}
|
|
46
60
|
|
|
61
|
+
// Set environment variables based on CLI options
|
|
62
|
+
if (args['log-level']) {
|
|
63
|
+
process.env.PLT_LOG_LEVEL = args['log-level']
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (args['icc-url']) {
|
|
67
|
+
process.env.PLT_ICC_URL = args['icc-url']
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (args['app-name']) {
|
|
71
|
+
process.env.PLT_APP_NAME = args['app-name']
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (args['app-dir']) {
|
|
75
|
+
process.env.PLT_APP_DIR = resolve(args['app-dir']) // Ensure the path is absolute
|
|
76
|
+
}
|
|
77
|
+
|
|
47
78
|
await start()
|
|
48
79
|
return true
|
|
49
80
|
}
|
package/lib/watt.js
CHANGED
|
@@ -57,6 +57,7 @@ class Watt {
|
|
|
57
57
|
this.runtime = await this.#createRuntime()
|
|
58
58
|
this.#logger.info('Starting runtime -WATT')
|
|
59
59
|
await this.runtime.start()
|
|
60
|
+
await this.#applySchedulerMode(this.runtime)
|
|
60
61
|
await this.updateSharedContext(this.#sharedContext)
|
|
61
62
|
this.#logger.info('Runtime started')
|
|
62
63
|
} catch (err) {
|
|
@@ -186,6 +187,7 @@ class Watt {
|
|
|
186
187
|
|
|
187
188
|
try {
|
|
188
189
|
await runtime.init()
|
|
190
|
+
await this.#applySchedulerMode(runtime)
|
|
189
191
|
} catch (e) {
|
|
190
192
|
await runtime.close()
|
|
191
193
|
throw e
|
|
@@ -497,8 +499,22 @@ class Watt {
|
|
|
497
499
|
}
|
|
498
500
|
|
|
499
501
|
#configureScheduler (config) {
|
|
500
|
-
|
|
501
|
-
|
|
502
|
+
const { Runtime } = this.#require('@platformatic/runtime')
|
|
503
|
+
if (typeof Runtime?.prototype?.pauseSchedulerJob === 'function') {
|
|
504
|
+
return
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// Disable all watt schedules when ICC coordinates them centrally
|
|
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') {
|
|
515
|
+
return
|
|
516
|
+
}
|
|
517
|
+
|
|
502
518
|
if (config.scheduler) {
|
|
503
519
|
config.scheduler = config.scheduler.map((scheduler) => ({
|
|
504
520
|
...scheduler,
|
|
@@ -507,6 +523,36 @@ class Watt {
|
|
|
507
523
|
}
|
|
508
524
|
}
|
|
509
525
|
|
|
526
|
+
getOriginalSchedulerConfig () {
|
|
527
|
+
return this.#originalConfig?.scheduler ?? []
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
async applySchedulerMode () {
|
|
531
|
+
await this.#applySchedulerMode(this.runtime)
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
async #applySchedulerMode (runtime) {
|
|
535
|
+
if (typeof runtime?.getScheduler !== 'function' ||
|
|
536
|
+
typeof runtime.pauseSchedulerJob !== 'function' ||
|
|
537
|
+
typeof runtime.resumeSchedulerJob !== 'function') {
|
|
538
|
+
return
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const mode = !this.#env.PLT_ICC_URL || this.#instanceConfig?.scheduler?.mode === 'local'
|
|
542
|
+
? 'local'
|
|
543
|
+
: 'external'
|
|
544
|
+
const jobs = await runtime.getScheduler()
|
|
545
|
+
const operation = mode === 'external' ? 'pauseSchedulerJob' : 'resumeSchedulerJob'
|
|
546
|
+
|
|
547
|
+
await Promise.all(
|
|
548
|
+
jobs
|
|
549
|
+
.filter(job => job.paused !== (mode === 'external'))
|
|
550
|
+
.map(job => runtime[operation](job.name))
|
|
551
|
+
)
|
|
552
|
+
|
|
553
|
+
this.#logger.info({ mode, jobs: jobs.map(job => job.name) }, 'Runtime scheduler mode updated')
|
|
554
|
+
}
|
|
555
|
+
|
|
510
556
|
async #configureServices (runtime) {
|
|
511
557
|
if (typeof runtime.setApplicationConfigPatch !== 'function') {
|
|
512
558
|
return
|
package/package.json
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/watt-extra",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.15.0-alpha.1",
|
|
4
4
|
"description": "The Platformatic runtime manager",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"lint": "eslint",
|
|
8
|
-
"test": "pnpm run lint && borp -c 1 --timeout=180000 ./test/*.test.js",
|
|
9
|
-
"start": "node index.js"
|
|
10
|
-
},
|
|
11
6
|
"bin": {
|
|
12
7
|
"watt-extra": "./cli.js"
|
|
13
8
|
},
|
|
@@ -55,5 +50,10 @@
|
|
|
55
50
|
"undici-slicer-interceptor": "^0.4.1",
|
|
56
51
|
"undici-traffic-interceptor": "^0.1.4",
|
|
57
52
|
"ws": "^8.18.3"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"lint": "eslint",
|
|
56
|
+
"test": "pnpm run lint && borp -c 1 --timeout=180000 ./test/*.test.js",
|
|
57
|
+
"start": "node index.js"
|
|
58
58
|
}
|
|
59
|
-
}
|
|
59
|
+
}
|
package/plugins/metadata.js
CHANGED
|
@@ -56,6 +56,16 @@ 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
|
+
|
|
59
69
|
try {
|
|
60
70
|
// There is a better way? We need to set the default headers for the client
|
|
61
71
|
// every time, because the token might be expired
|
|
@@ -64,7 +74,8 @@ async function metadata (app, _opts) {
|
|
|
64
74
|
await controlPlaneClient.saveApplicationInstanceState({
|
|
65
75
|
id: app.instanceId,
|
|
66
76
|
services,
|
|
67
|
-
metadata: runtimeMetadata
|
|
77
|
+
metadata: runtimeMetadata,
|
|
78
|
+
...(scheduler ? { scheduler } : {})
|
|
68
79
|
}, {
|
|
69
80
|
headers: await app.getAuthorizationHeaders()
|
|
70
81
|
})
|
package/plugins/scheduler.js
CHANGED
|
@@ -1,4 +1,80 @@
|
|
|
1
|
+
import { request } from 'undici'
|
|
2
|
+
|
|
1
3
|
async function scheduler (app, _opts) {
|
|
4
|
+
// Collects configured and application-level jobs owned by the runtime.
|
|
5
|
+
async function collectSchedulerJobs () {
|
|
6
|
+
const runtime = app.watt.runtime
|
|
7
|
+
|
|
8
|
+
if (typeof runtime.getScheduler === 'function') {
|
|
9
|
+
return runtime.getScheduler()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Older runtimes: use the original configuration, since the runtime one
|
|
13
|
+
// has been disabled by the config patch (see Watt#configureScheduler)
|
|
14
|
+
const jobs = app.watt.getOriginalSchedulerConfig?.() ?? []
|
|
15
|
+
return jobs
|
|
16
|
+
.filter((job) => job.enabled !== false)
|
|
17
|
+
.map((job) => ({
|
|
18
|
+
name: job.name,
|
|
19
|
+
cron: job.cron,
|
|
20
|
+
callbackUrl: job.callbackUrl,
|
|
21
|
+
method: job.method ?? 'GET',
|
|
22
|
+
headers: job.headers ?? {},
|
|
23
|
+
body: job.body ?? {},
|
|
24
|
+
maxRetries: job.maxRetries ?? 3,
|
|
25
|
+
source: 'config'
|
|
26
|
+
}))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Executes a job triggered centrally by ICC (run-scheduled-job command).
|
|
30
|
+
// The job runs in this pod only: ICC dispatches each tick to a single pod.
|
|
31
|
+
async function runScheduledJob (params = {}) {
|
|
32
|
+
const runtime = app.watt.runtime
|
|
33
|
+
if (!runtime) {
|
|
34
|
+
throw new Error('Runtime not started, cannot run scheduled job')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const { name, callbackUrl, method = 'GET', headers = {}, body, source = 'config' } = params
|
|
38
|
+
|
|
39
|
+
app.log.info({ name, source }, 'Executing scheduled job triggered by ICC')
|
|
40
|
+
|
|
41
|
+
// Newer runtimes own both configured and application-level jobs.
|
|
42
|
+
if (typeof runtime.runSchedulerJob === 'function') {
|
|
43
|
+
return runtime.runSchedulerJob(name)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!callbackUrl) {
|
|
47
|
+
throw new Error(`Cannot execute scheduled job "${name}": no callback URL`)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const url = new URL(callbackUrl)
|
|
51
|
+
const bodyString = body == null || typeof body === 'string' ? body : JSON.stringify(body)
|
|
52
|
+
|
|
53
|
+
let statusCode
|
|
54
|
+
if (url.hostname.endsWith('.plt.local')) {
|
|
55
|
+
// Internal mesh URL: route the request through the runtime
|
|
56
|
+
const applicationId = url.hostname.slice(0, -'.plt.local'.length)
|
|
57
|
+
const res = await runtime.inject(applicationId, {
|
|
58
|
+
method,
|
|
59
|
+
url: `${url.pathname}${url.search}`,
|
|
60
|
+
headers,
|
|
61
|
+
body: bodyString ?? undefined
|
|
62
|
+
})
|
|
63
|
+
statusCode = res.statusCode
|
|
64
|
+
} else {
|
|
65
|
+
const res = await request(callbackUrl, { method, headers, body: bodyString ?? undefined })
|
|
66
|
+
await res.body.dump()
|
|
67
|
+
statusCode = res.statusCode
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (statusCode < 200 || statusCode >= 300) {
|
|
71
|
+
throw new Error(`Scheduled job "${name}" failed with HTTP ${statusCode}`)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
app.log.info({ name, statusCode }, 'Scheduled job executed')
|
|
75
|
+
return { name, statusCode }
|
|
76
|
+
}
|
|
77
|
+
|
|
2
78
|
async function sendSchedulerInfo () {
|
|
3
79
|
// Skip scheduler configuration if ICC is not configured
|
|
4
80
|
if (!app.env.PLT_ICC_URL) {
|
|
@@ -6,10 +82,23 @@ async function scheduler (app, _opts) {
|
|
|
6
82
|
return
|
|
7
83
|
}
|
|
8
84
|
|
|
85
|
+
const mode = app.instanceConfig?.scheduler?.mode
|
|
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
|
|
9
100
|
try {
|
|
10
101
|
const applicationId = app.instanceConfig?.applicationId
|
|
11
|
-
const runtime = app.watt.runtime
|
|
12
|
-
const config = await runtime.getRuntimeConfig()
|
|
13
102
|
const { default: build, setDefaultHeaders } = await import('../clients/cron/cron.mjs')
|
|
14
103
|
|
|
15
104
|
const cronUrl = app.instanceConfig?.iccServices?.cron?.url
|
|
@@ -20,14 +109,24 @@ async function scheduler (app, _opts) {
|
|
|
20
109
|
const cronClient = build(cronUrl)
|
|
21
110
|
setDefaultHeaders(await app.getAuthorizationHeaders())
|
|
22
111
|
|
|
23
|
-
const jobs =
|
|
112
|
+
const jobs = (await collectSchedulerJobs()).filter((job) => job.source === 'config')
|
|
24
113
|
|
|
25
114
|
const saveJobs = []
|
|
26
115
|
for (const job of jobs) {
|
|
27
|
-
const iccJob = {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
116
|
+
const iccJob = {
|
|
117
|
+
name: job.name,
|
|
118
|
+
callbackUrl: job.callbackUrl,
|
|
119
|
+
schedule: job.cron, // unfortunately, the ICC API uses `schedule` instead of `cron`
|
|
120
|
+
method: job.method,
|
|
121
|
+
maxRetries: job.maxRetries,
|
|
122
|
+
applicationId
|
|
123
|
+
}
|
|
124
|
+
if (job.headers && Object.keys(job.headers).length > 0) {
|
|
125
|
+
iccJob.headers = job.headers
|
|
126
|
+
}
|
|
127
|
+
if (job.body && (typeof job.body === 'string' ? job.body.length > 0 : Object.keys(job.body).length > 0)) {
|
|
128
|
+
iccJob.body = job.body
|
|
129
|
+
}
|
|
31
130
|
saveJobs.push(cronClient.putWattJobs(iccJob))
|
|
32
131
|
}
|
|
33
132
|
const result = await Promise.allSettled(saveJobs)
|
|
@@ -42,7 +141,10 @@ async function scheduler (app, _opts) {
|
|
|
42
141
|
app.log.error(error, 'Failed in configuring watt jobs in ICC')
|
|
43
142
|
}
|
|
44
143
|
}
|
|
144
|
+
|
|
45
145
|
app.sendSchedulerInfo = sendSchedulerInfo
|
|
146
|
+
app.collectSchedulerJobs = collectSchedulerJobs
|
|
147
|
+
app.runScheduledJob = runScheduledJob
|
|
46
148
|
}
|
|
47
149
|
|
|
48
150
|
export default scheduler
|
package/plugins/update.js
CHANGED
|
@@ -37,6 +37,14 @@ async function updatePlugin (app) {
|
|
|
37
37
|
return
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
// Handle a centrally coordinated cron job execution: ICC dispatches
|
|
41
|
+
// each tick to exactly one pod of the application, this one.
|
|
42
|
+
if (command === 'run-scheduled-job') {
|
|
43
|
+
app.log.info({ command, job: message.params?.name }, 'Received run-scheduled-job command from ICC')
|
|
44
|
+
await app.runScheduledJob(message.params ?? {})
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
|
|
40
48
|
// Handle updates websocket format: { type: '...', topic: '...', data: {...} }
|
|
41
49
|
if (!topic || !type) {
|
|
42
50
|
app.log.warn({ message }, 'Received invalid message from updates websocket')
|
package/test/cli.test.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import assert from 'node:assert'
|
|
2
2
|
import { test } from 'node:test'
|
|
3
3
|
import { spawn } from 'node:child_process'
|
|
4
|
-
import { join,
|
|
4
|
+
import { join, dirname } from 'node:path'
|
|
5
5
|
import { fileURLToPath } from 'node:url'
|
|
6
|
-
import { applyStartArgs } from '../lib/start-args.js'
|
|
7
6
|
|
|
8
7
|
const __filename = fileURLToPath(import.meta.url)
|
|
9
8
|
const __dirname = dirname(__filename)
|
|
@@ -91,51 +90,3 @@ test('CLI should output JSON log without banner in non-TTY mode (version command
|
|
|
91
90
|
'Non-TTY output should include JSON log with version'
|
|
92
91
|
)
|
|
93
92
|
})
|
|
94
|
-
|
|
95
|
-
test('applyStartArgs: --log-level flag overrides env-provided PLT_LOG_LEVEL', () => {
|
|
96
|
-
const env = { PLT_LOG_LEVEL: 'warn' }
|
|
97
|
-
applyStartArgs(['--log-level', 'debug'], env)
|
|
98
|
-
assert.strictEqual(env.PLT_LOG_LEVEL, 'debug')
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
test('applyStartArgs: -l short flag also overrides env', () => {
|
|
102
|
-
const env = { PLT_LOG_LEVEL: 'warn' }
|
|
103
|
-
applyStartArgs(['-l', 'trace'], env)
|
|
104
|
-
assert.strictEqual(env.PLT_LOG_LEVEL, 'trace')
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
// Regression test: cli.js used to have `default: { 'log-level': 'info' }` in its
|
|
108
|
-
// minimist config, which populated args['log-level'] with 'info' whenever the flag
|
|
109
|
-
// was not passed. The subsequent unconditional write to process.env.PLT_LOG_LEVEL
|
|
110
|
-
// then silently clobbered the value provided by the deployment environment (e.g.
|
|
111
|
-
// PLT_LOG_LEVEL=warn from k8s), forcing the runtime to log at info regardless.
|
|
112
|
-
test('applyStartArgs: preserves env-provided PLT_LOG_LEVEL when --log-level is not passed', () => {
|
|
113
|
-
const env = { PLT_LOG_LEVEL: 'warn' }
|
|
114
|
-
applyStartArgs([], env)
|
|
115
|
-
assert.strictEqual(env.PLT_LOG_LEVEL, 'warn')
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
test('applyStartArgs: defaults PLT_LOG_LEVEL to "info" when neither env nor flag is set', () => {
|
|
119
|
-
const env = {}
|
|
120
|
-
applyStartArgs([], env)
|
|
121
|
-
assert.strictEqual(env.PLT_LOG_LEVEL, 'info')
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
test('applyStartArgs: passes through other CLI options to env', () => {
|
|
125
|
-
const env = {}
|
|
126
|
-
applyStartArgs([
|
|
127
|
-
'--icc-url', 'http://icc.example',
|
|
128
|
-
'--app-name', 'my-app',
|
|
129
|
-
'--app-dir', 'test/fixtures/runtime-service'
|
|
130
|
-
], env)
|
|
131
|
-
assert.strictEqual(env.PLT_ICC_URL, 'http://icc.example')
|
|
132
|
-
assert.strictEqual(env.PLT_APP_NAME, 'my-app')
|
|
133
|
-
assert.strictEqual(env.PLT_APP_DIR, resolve('test/fixtures/runtime-service'))
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
test('applyStartArgs: --help short-circuits without mutating env', () => {
|
|
137
|
-
const env = {}
|
|
138
|
-
const res = applyStartArgs(['--help'], env)
|
|
139
|
-
assert.strictEqual(res.help, true)
|
|
140
|
-
assert.strictEqual(env.PLT_LOG_LEVEL, undefined)
|
|
141
|
-
})
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import assert from 'node:assert'
|
|
2
|
+
import { randomUUID } from 'node:crypto'
|
|
3
|
+
import { once } from 'node:events'
|
|
4
|
+
import { createServer } from 'node:http'
|
|
5
|
+
import { join, dirname } from 'node:path'
|
|
6
|
+
import { test } from 'node:test'
|
|
7
|
+
import { fileURLToPath } from 'node:url'
|
|
8
|
+
import WebSocket from 'ws'
|
|
9
|
+
import { start } from '../index.js'
|
|
10
|
+
import Watt from '../lib/watt.js'
|
|
11
|
+
import schedulerPlugin from '../plugins/scheduler.js'
|
|
12
|
+
import updatePlugin from '../plugins/update.js'
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
setUpEnvironment,
|
|
16
|
+
startICC,
|
|
17
|
+
installDeps
|
|
18
|
+
} from './helper.js'
|
|
19
|
+
|
|
20
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
21
|
+
const __dirname = dirname(__filename)
|
|
22
|
+
|
|
23
|
+
function controlPlaneResponseWithMode (applicationId, applicationName, mode) {
|
|
24
|
+
return () => ({
|
|
25
|
+
applicationId,
|
|
26
|
+
applicationName,
|
|
27
|
+
config: {},
|
|
28
|
+
scheduler: { mode },
|
|
29
|
+
iccServices: {
|
|
30
|
+
cron: { url: 'http://127.0.0.1:3000/cron' },
|
|
31
|
+
compliance: { url: 'http://127.0.0.1:3000/compliance' },
|
|
32
|
+
scaler: { url: 'http://127.0.0.1:3000/scaler' }
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
test('external mode: should disable local jobs, skip legacy registration and report the jobs in the state', async (t) => {
|
|
38
|
+
const applicationName = 'test-app'
|
|
39
|
+
const applicationId = randomUUID()
|
|
40
|
+
const applicationPath = join(__dirname, 'fixtures', 'runtime-scheduler')
|
|
41
|
+
|
|
42
|
+
await installDeps(t, applicationPath)
|
|
43
|
+
|
|
44
|
+
let savedWattJob = null
|
|
45
|
+
let savedState = null
|
|
46
|
+
const icc = await startICC(t, {
|
|
47
|
+
applicationId,
|
|
48
|
+
applicationName,
|
|
49
|
+
controlPlaneResponse: controlPlaneResponseWithMode(applicationId, applicationName, 'external'),
|
|
50
|
+
saveWattJob: (job) => {
|
|
51
|
+
savedWattJob = job
|
|
52
|
+
},
|
|
53
|
+
saveApplicationInstanceState: ({ state }) => {
|
|
54
|
+
savedState = state
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
setUpEnvironment({
|
|
59
|
+
PLT_APP_NAME: applicationName,
|
|
60
|
+
PLT_APP_DIR: applicationPath,
|
|
61
|
+
PLT_ICC_URL: 'http://127.0.0.1:3000'
|
|
62
|
+
})
|
|
63
|
+
const app = await start()
|
|
64
|
+
|
|
65
|
+
t.after(async () => {
|
|
66
|
+
await app.close()
|
|
67
|
+
await icc.close()
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
// The local scheduler triggers are disabled on released runtimes and paused
|
|
71
|
+
// on runtimes exposing scheduler control.
|
|
72
|
+
const config = await app.watt.runtime.getRuntimeConfig()
|
|
73
|
+
if (typeof app.watt.runtime.pauseSchedulerJob === 'function') {
|
|
74
|
+
assert.notStrictEqual(config.scheduler[0].enabled, false)
|
|
75
|
+
assert.strictEqual(app.watt.runtime.getScheduler()[0].paused, true)
|
|
76
|
+
} else {
|
|
77
|
+
assert.strictEqual(config.scheduler[0].enabled, false)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// The legacy per-job registration is skipped: the control-plane
|
|
81
|
+
// registers the jobs centrally from the state report
|
|
82
|
+
assert.strictEqual(savedWattJob, null)
|
|
83
|
+
|
|
84
|
+
// The jobs are reported in the application state
|
|
85
|
+
assert.ok(savedState, 'the state should have been reported')
|
|
86
|
+
assert.ok(Array.isArray(savedState.scheduler), 'the state should include the scheduler jobs')
|
|
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')
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('local mode: should keep the local jobs running and skip any registration', async (t) => {
|
|
95
|
+
const applicationName = 'test-app'
|
|
96
|
+
const applicationId = randomUUID()
|
|
97
|
+
const applicationPath = join(__dirname, 'fixtures', 'runtime-scheduler')
|
|
98
|
+
|
|
99
|
+
await installDeps(t, applicationPath)
|
|
100
|
+
|
|
101
|
+
let savedWattJob = null
|
|
102
|
+
const icc = await startICC(t, {
|
|
103
|
+
applicationId,
|
|
104
|
+
applicationName,
|
|
105
|
+
controlPlaneResponse: controlPlaneResponseWithMode(applicationId, applicationName, 'local'),
|
|
106
|
+
saveWattJob: (job) => {
|
|
107
|
+
savedWattJob = job
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
setUpEnvironment({
|
|
112
|
+
PLT_APP_NAME: applicationName,
|
|
113
|
+
PLT_APP_DIR: applicationPath,
|
|
114
|
+
PLT_ICC_URL: 'http://127.0.0.1:3000'
|
|
115
|
+
})
|
|
116
|
+
const app = await start()
|
|
117
|
+
|
|
118
|
+
t.after(async () => {
|
|
119
|
+
await app.close()
|
|
120
|
+
await icc.close()
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
// ICC coordination is disabled: the local scheduler stays enabled
|
|
124
|
+
const config = await app.watt.runtime.getRuntimeConfig()
|
|
125
|
+
assert.notStrictEqual(config.scheduler[0].enabled, false)
|
|
126
|
+
if (typeof app.watt.runtime.getScheduler === 'function') {
|
|
127
|
+
assert.strictEqual(app.watt.runtime.getScheduler()[0].paused, false)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// No registration happens: the jobs run locally in each pod
|
|
131
|
+
assert.strictEqual(savedWattJob, null)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
function createMockApp (port) {
|
|
135
|
+
return {
|
|
136
|
+
log: { info: () => {}, error: () => {}, warn: () => {}, debug: () => {} },
|
|
137
|
+
instanceConfig: {
|
|
138
|
+
applicationId: 'test-application-id',
|
|
139
|
+
scheduler: { mode: 'external' }
|
|
140
|
+
},
|
|
141
|
+
instanceId: 'test-pod-123',
|
|
142
|
+
getRuntimeId: () => 'test-runtime-id',
|
|
143
|
+
getAuthorizationHeaders: async () => ({ Authorization: 'Bearer test-token' }),
|
|
144
|
+
env: {
|
|
145
|
+
PLT_ICC_URL: `http://localhost:${port}`,
|
|
146
|
+
PLT_UPDATES_RECONNECT_INTERVAL_SEC: 1
|
|
147
|
+
},
|
|
148
|
+
watt: {
|
|
149
|
+
runtime: {},
|
|
150
|
+
applySchedulerMode: async () => {}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
test('runtime scheduler ownership follows the ICC mode', async () => {
|
|
156
|
+
const instanceConfig = { scheduler: { mode: 'external' } }
|
|
157
|
+
const jobs = [
|
|
158
|
+
{ name: 'configured', paused: false },
|
|
159
|
+
{ name: 'frontend:0', paused: false }
|
|
160
|
+
]
|
|
161
|
+
const watt = new Watt({
|
|
162
|
+
env: {
|
|
163
|
+
PLT_APP_DIR: join(__dirname, 'fixtures', 'runtime-scheduler'),
|
|
164
|
+
PLT_ICC_URL: 'http://localhost:3000'
|
|
165
|
+
},
|
|
166
|
+
log: { info: () => {} },
|
|
167
|
+
instanceConfig,
|
|
168
|
+
instanceId: 'test-pod-123'
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
watt.runtime = {
|
|
172
|
+
getScheduler: () => jobs,
|
|
173
|
+
pauseSchedulerJob: async name => {
|
|
174
|
+
jobs.find(job => job.name === name).paused = true
|
|
175
|
+
},
|
|
176
|
+
resumeSchedulerJob: async name => {
|
|
177
|
+
jobs.find(job => job.name === name).paused = false
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
await watt.applySchedulerMode()
|
|
182
|
+
assert.deepStrictEqual(jobs.map(job => job.paused), [true, true])
|
|
183
|
+
|
|
184
|
+
instanceConfig.scheduler.mode = 'local'
|
|
185
|
+
await watt.applySchedulerMode()
|
|
186
|
+
assert.deepStrictEqual(jobs.map(job => job.paused), [false, false])
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
function setupMockIccServer (wss) {
|
|
190
|
+
let ws = null
|
|
191
|
+
|
|
192
|
+
const waitForClientSubscription = once(wss, 'connection').then(([socket]) => {
|
|
193
|
+
ws = socket
|
|
194
|
+
|
|
195
|
+
return new Promise((resolve) => {
|
|
196
|
+
socket.on('message', (data) => {
|
|
197
|
+
const message = JSON.parse(data.toString())
|
|
198
|
+
|
|
199
|
+
if (message.command === 'subscribe' && message.topic === '/config') {
|
|
200
|
+
socket.send(JSON.stringify({ command: 'ack' }))
|
|
201
|
+
resolve()
|
|
202
|
+
}
|
|
203
|
+
})
|
|
204
|
+
})
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
return { waitForClientSubscription, getWs: () => ws }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
test('run-scheduled-job: should execute the job callback when triggered over the websocket', async (t) => {
|
|
211
|
+
setUpEnvironment()
|
|
212
|
+
const port = 14100
|
|
213
|
+
|
|
214
|
+
const wss = new WebSocket.Server({ port })
|
|
215
|
+
t.after(() => wss.close())
|
|
216
|
+
|
|
217
|
+
const { waitForClientSubscription, getWs } = setupMockIccServer(wss)
|
|
218
|
+
|
|
219
|
+
// The target the job callback should hit
|
|
220
|
+
const receivedRequests = []
|
|
221
|
+
let requestResolve
|
|
222
|
+
const requestReceived = new Promise((resolve) => { requestResolve = resolve })
|
|
223
|
+
const target = createServer((req, res) => {
|
|
224
|
+
receivedRequests.push({ method: req.method, url: req.url })
|
|
225
|
+
res.end('{"ok":true}')
|
|
226
|
+
requestResolve()
|
|
227
|
+
})
|
|
228
|
+
await new Promise((resolve) => target.listen(0, resolve))
|
|
229
|
+
t.after(() => target.close())
|
|
230
|
+
|
|
231
|
+
const app = createMockApp(port)
|
|
232
|
+
await schedulerPlugin(app)
|
|
233
|
+
await updatePlugin(app)
|
|
234
|
+
await app.connectToUpdates()
|
|
235
|
+
t.after(() => app.closeUpdates())
|
|
236
|
+
await waitForClientSubscription
|
|
237
|
+
|
|
238
|
+
getWs().send(JSON.stringify({
|
|
239
|
+
command: 'run-scheduled-job',
|
|
240
|
+
params: {
|
|
241
|
+
name: 'cleanup',
|
|
242
|
+
callbackUrl: `http://127.0.0.1:${target.address().port}/cleanup`,
|
|
243
|
+
method: 'POST',
|
|
244
|
+
source: 'config'
|
|
245
|
+
}
|
|
246
|
+
}))
|
|
247
|
+
|
|
248
|
+
await requestReceived
|
|
249
|
+
|
|
250
|
+
assert.deepStrictEqual(receivedRequests, [{ method: 'POST', url: '/cleanup' }])
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
test('run-scheduled-job: should route legacy internal callbacks through the runtime', async (t) => {
|
|
254
|
+
const app = createMockApp(0)
|
|
255
|
+
|
|
256
|
+
const injected = []
|
|
257
|
+
app.watt.runtime.inject = async (applicationId, params) => {
|
|
258
|
+
injected.push({ applicationId, ...params })
|
|
259
|
+
return { statusCode: 200 }
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
await schedulerPlugin(app)
|
|
263
|
+
|
|
264
|
+
const result = await app.runScheduledJob({
|
|
265
|
+
name: 'cleanup',
|
|
266
|
+
callbackUrl: 'http://frontend.plt.local/cleanup',
|
|
267
|
+
method: 'POST',
|
|
268
|
+
source: 'config',
|
|
269
|
+
body: { scheduledTime: 1 }
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
assert.deepStrictEqual(result, { name: 'cleanup', statusCode: 200 })
|
|
273
|
+
assert.strictEqual(injected.length, 1)
|
|
274
|
+
assert.strictEqual(injected[0].applicationId, 'frontend')
|
|
275
|
+
assert.strictEqual(injected[0].method, 'POST')
|
|
276
|
+
assert.strictEqual(injected[0].url, '/cleanup')
|
|
277
|
+
assert.strictEqual(injected[0].body, '{"scheduledTime":1}')
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
test('run-scheduled-job: should use native runtime execution for application jobs', async (t) => {
|
|
281
|
+
const app = createMockApp(0)
|
|
282
|
+
|
|
283
|
+
const executed = []
|
|
284
|
+
app.watt.runtime.runSchedulerJob = async (name) => {
|
|
285
|
+
executed.push(name)
|
|
286
|
+
return { name, success: true }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
await schedulerPlugin(app)
|
|
290
|
+
|
|
291
|
+
const result = await app.runScheduledJob({ name: 'frontend:0', source: 'application' })
|
|
292
|
+
|
|
293
|
+
assert.deepStrictEqual(executed, ['frontend:0'])
|
|
294
|
+
assert.deepStrictEqual(result, { name: 'frontend:0', success: true })
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
test('run-scheduled-job: should fail on non-2xx callback responses', async (t) => {
|
|
298
|
+
const app = createMockApp(0)
|
|
299
|
+
|
|
300
|
+
app.watt.runtime.inject = async () => ({ statusCode: 500 })
|
|
301
|
+
|
|
302
|
+
await schedulerPlugin(app)
|
|
303
|
+
|
|
304
|
+
await assert.rejects(
|
|
305
|
+
app.runScheduledJob({
|
|
306
|
+
name: 'failing',
|
|
307
|
+
callbackUrl: 'http://frontend.plt.local/failing',
|
|
308
|
+
source: 'config'
|
|
309
|
+
}),
|
|
310
|
+
/Scheduled job "failing" failed with HTTP 500/
|
|
311
|
+
)
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
test('collectSchedulerJobs: should use the runtime scheduler status when available', async (t) => {
|
|
315
|
+
const app = createMockApp(0)
|
|
316
|
+
|
|
317
|
+
app.watt.runtime.getScheduler = async () => ([
|
|
318
|
+
{
|
|
319
|
+
name: 'cleanup',
|
|
320
|
+
cron: '0 * * * *',
|
|
321
|
+
callbackUrl: 'http://service.plt.local/cleanup',
|
|
322
|
+
method: 'POST',
|
|
323
|
+
source: 'config',
|
|
324
|
+
paused: false
|
|
325
|
+
},
|
|
326
|
+
{
|
|
327
|
+
name: 'frontend:0',
|
|
328
|
+
cron: '* * * * *',
|
|
329
|
+
source: 'application',
|
|
330
|
+
applicationId: 'frontend',
|
|
331
|
+
scheduleId: '0',
|
|
332
|
+
tasks: ['db:cleanup']
|
|
333
|
+
}
|
|
334
|
+
])
|
|
335
|
+
|
|
336
|
+
await schedulerPlugin(app)
|
|
337
|
+
|
|
338
|
+
const jobs = await app.collectSchedulerJobs()
|
|
339
|
+
|
|
340
|
+
assert.strictEqual(jobs.length, 2)
|
|
341
|
+
assert.strictEqual(jobs[0].name, 'cleanup')
|
|
342
|
+
assert.strictEqual(jobs[0].source, 'config')
|
|
343
|
+
assert.strictEqual(jobs[1].name, 'frontend:0')
|
|
344
|
+
assert.strictEqual(jobs[1].source, 'application')
|
|
345
|
+
assert.strictEqual(jobs[1].applicationId, 'frontend')
|
|
346
|
+
assert.strictEqual(jobs[1].scheduleId, '0')
|
|
347
|
+
assert.deepStrictEqual(jobs[1].tasks, ['db:cleanup'])
|
|
348
|
+
})
|
|
349
|
+
|
|
350
|
+
test('external mode: should apply runtime scheduler ownership', async (t) => {
|
|
351
|
+
const app = createMockApp(0)
|
|
352
|
+
|
|
353
|
+
let applications = 0
|
|
354
|
+
app.watt.applySchedulerMode = async () => {
|
|
355
|
+
applications++
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
await schedulerPlugin(app)
|
|
359
|
+
await app.sendSchedulerInfo()
|
|
360
|
+
|
|
361
|
+
assert.strictEqual(applications, 1)
|
|
362
|
+
})
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"permissions": {
|
|
3
|
-
"allow": [
|
|
4
|
-
"Read(//work/workspaces/workspace-platformatic/platformatic/**)",
|
|
5
|
-
"Bash(npx borp:*)",
|
|
6
|
-
"Bash(timeout 30 npx borp -c 1 --timeout=20000 ./test/trigger-flamegraphs.test.js)",
|
|
7
|
-
"Bash(xargs cat:*)",
|
|
8
|
-
"Bash(pnpm install)",
|
|
9
|
-
"Bash(find:*)",
|
|
10
|
-
"Bash(cat:*)",
|
|
11
|
-
"WebFetch(domain:github.com)",
|
|
12
|
-
"Bash(node --test:*)",
|
|
13
|
-
"Bash(for i in 1 2 3)",
|
|
14
|
-
"Bash(do echo \"Run $i:\")",
|
|
15
|
-
"Bash(done)",
|
|
16
|
-
"Bash(git stash:*)",
|
|
17
|
-
"Bash(echo:*)",
|
|
18
|
-
"Bash(grep:*)",
|
|
19
|
-
"Bash(gh api:*)"
|
|
20
|
-
],
|
|
21
|
-
"deny": [],
|
|
22
|
-
"ask": []
|
|
23
|
-
}
|
|
24
|
-
}
|
package/lib/start-args.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import minimist from 'minimist'
|
|
2
|
-
import { resolve } from 'node:path'
|
|
3
|
-
|
|
4
|
-
// Parse start command argv and apply CLI options to env.
|
|
5
|
-
// Precedence for PLT_LOG_LEVEL: --log-level flag > pre-set env > 'info' default.
|
|
6
|
-
// Do NOT add a minimist default for `log-level`: that would make args['log-level']
|
|
7
|
-
// always truthy and unconditionally clobber an env-provided value.
|
|
8
|
-
export function applyStartArgs (argv, env = process.env) {
|
|
9
|
-
const args = minimist(argv, {
|
|
10
|
-
alias: {
|
|
11
|
-
h: 'help',
|
|
12
|
-
l: 'log-level',
|
|
13
|
-
i: 'icc-url',
|
|
14
|
-
a: 'app-name',
|
|
15
|
-
d: 'app-dir'
|
|
16
|
-
},
|
|
17
|
-
boolean: ['help'],
|
|
18
|
-
string: ['log-level', 'icc-url', 'app-name', 'app-dir'],
|
|
19
|
-
default: {
|
|
20
|
-
'app-dir': process.cwd()
|
|
21
|
-
}
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
if (args.help) {
|
|
25
|
-
return { args, help: true }
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (args['log-level']) {
|
|
29
|
-
env.PLT_LOG_LEVEL = args['log-level']
|
|
30
|
-
} else if (!env.PLT_LOG_LEVEL) {
|
|
31
|
-
env.PLT_LOG_LEVEL = 'info'
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (args['icc-url']) {
|
|
35
|
-
env.PLT_ICC_URL = args['icc-url']
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (args['app-name']) {
|
|
39
|
-
env.PLT_APP_NAME = args['app-name']
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (args['app-dir']) {
|
|
43
|
-
env.PLT_APP_DIR = resolve(args['app-dir']) // Ensure the path is absolute
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return { args, help: false }
|
|
47
|
-
}
|