@platformatic/watt-extra 1.15.0-alpha.2 → 1.15.0
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/.claude/settings.local.json +24 -0
- package/.github/workflows/test.yml +3 -3
- package/cli.js +4 -35
- package/clients/control-plane/control-plane-types.d.ts +1 -0
- package/clients/control-plane/control-plane.openapi.json +7 -1
- package/clients/cron/cron-types.d.ts +3 -0
- package/clients/cron/cron.mjs +10 -2
- package/clients/cron/cron.openapi.json +16 -1
- package/lib/start-args.js +47 -0
- package/lib/watt.js +24 -19
- package/package.json +17 -17
- package/plugins/compliancy.js +1 -1
- package/plugins/flamegraphs.js +21 -5
- package/plugins/init.js +9 -0
- package/plugins/metadata.js +2 -5
- package/plugins/scheduler.js +49 -32
- package/plugins/update.js +17 -35
- package/test/alerts.test.js +4 -4
- package/test/cli.test.js +50 -1
- package/test/init.test.js +90 -0
- package/test/scheduler-coordination.test.js +124 -122
- package/test/trigger-flamegraphs.test.js +133 -64
- package/test/watch.test.js +54 -0
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
}
|
|
@@ -21,7 +21,7 @@ jobs:
|
|
|
21
21
|
|
|
22
22
|
services:
|
|
23
23
|
valkey:
|
|
24
|
-
image: "valkey/valkey:9.
|
|
24
|
+
image: "valkey/valkey:9.1"
|
|
25
25
|
ports:
|
|
26
26
|
- "6379:6379"
|
|
27
27
|
options: >-
|
|
@@ -35,13 +35,13 @@ jobs:
|
|
|
35
35
|
node-version: [22.x, 24.x]
|
|
36
36
|
|
|
37
37
|
steps:
|
|
38
|
-
- uses: actions/checkout@
|
|
38
|
+
- uses: actions/checkout@v7
|
|
39
39
|
- uses: pnpm/action-setup@v4.2.0
|
|
40
40
|
with:
|
|
41
41
|
version: 10
|
|
42
42
|
|
|
43
43
|
- name: Use Node.js ${{ matrix.node-version }}
|
|
44
|
-
uses: actions/setup-node@
|
|
44
|
+
uses: actions/setup-node@v7
|
|
45
45
|
with:
|
|
46
46
|
node-version: ${{ matrix.node-version }}
|
|
47
47
|
# https://github.com/actions/setup-node/blob/main/docs/advanced-usage.md#use-private-packages
|
package/cli.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import commist from 'commist'
|
|
3
|
-
import
|
|
4
|
-
import { resolve, join, dirname } from 'node:path'
|
|
3
|
+
import { join, dirname } from 'node:path'
|
|
5
4
|
import { fileURLToPath } from 'node:url'
|
|
6
5
|
import helpMeInit from 'help-me'
|
|
7
6
|
import { readFileSync } from 'node:fs'
|
|
8
7
|
import { start, logger } from './index.js'
|
|
9
8
|
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,46 +35,15 @@ async function startCommand (argv) {
|
|
|
35
35
|
logger.info(`WattExtra v${pkg.version}`)
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
const args =
|
|
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
|
-
})
|
|
38
|
+
const { args, help } = applyStartArgs(argv)
|
|
53
39
|
|
|
54
40
|
logger.debug({ args, argv }, 'Start command arguments')
|
|
55
41
|
|
|
56
|
-
if (
|
|
42
|
+
if (help) {
|
|
57
43
|
helpMe.toStdout('start')
|
|
58
44
|
return true
|
|
59
45
|
}
|
|
60
46
|
|
|
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
|
-
|
|
78
47
|
await start()
|
|
79
48
|
return true
|
|
80
49
|
}
|
|
@@ -13582,6 +13582,12 @@
|
|
|
13582
13582
|
"items": {
|
|
13583
13583
|
"type": "object"
|
|
13584
13584
|
}
|
|
13585
|
+
},
|
|
13586
|
+
"scheduler": {
|
|
13587
|
+
"type": "array",
|
|
13588
|
+
"items": {
|
|
13589
|
+
"type": "object"
|
|
13590
|
+
}
|
|
13585
13591
|
}
|
|
13586
13592
|
},
|
|
13587
13593
|
"additionalProperties": false
|
|
@@ -13690,4 +13696,4 @@
|
|
|
13690
13696
|
"url": "/"
|
|
13691
13697
|
}
|
|
13692
13698
|
]
|
|
13693
|
-
}
|
|
13699
|
+
}
|
|
@@ -1302,6 +1302,9 @@ export type PutWattJobsRequest = {
|
|
|
1302
1302
|
'body'?: object | null;
|
|
1303
1303
|
'headers'?: object | null;
|
|
1304
1304
|
'applicationId'?: string | null;
|
|
1305
|
+
'source'?: string | null;
|
|
1306
|
+
'scheduleId'?: string | null;
|
|
1307
|
+
'tasks'?: Array<string> | null;
|
|
1305
1308
|
}
|
|
1306
1309
|
|
|
1307
1310
|
export type PutWattJobsResponseOK = unknown
|
package/clients/cron/cron.mjs
CHANGED
|
@@ -573,10 +573,18 @@ 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
|
+
// ICC can return an empty or non-JSON error body. Preserve the response
|
|
579
|
+
// status instead of masking it with a body parsing error.
|
|
580
|
+
body = await response[responseType]()
|
|
581
|
+
} catch {
|
|
582
|
+
body = null
|
|
583
|
+
}
|
|
576
584
|
return {
|
|
577
585
|
statusCode: response.status,
|
|
578
586
|
headers: headersToJSON(response.headers),
|
|
579
|
-
body
|
|
587
|
+
body
|
|
580
588
|
}
|
|
581
589
|
}
|
|
582
590
|
|
|
@@ -869,4 +877,4 @@ export default function build (url, options) {
|
|
|
869
877
|
getMessagesIdCancel: _getMessagesIdCancel.bind(url, ...arguments),
|
|
870
878
|
putWattJobs: _putWattJobs.bind(url, ...arguments)
|
|
871
879
|
}
|
|
872
|
-
}
|
|
880
|
+
}
|
|
@@ -9304,6 +9304,21 @@
|
|
|
9304
9304
|
"applicationId": {
|
|
9305
9305
|
"type": "string",
|
|
9306
9306
|
"nullable": true
|
|
9307
|
+
},
|
|
9308
|
+
"source": {
|
|
9309
|
+
"type": "string",
|
|
9310
|
+
"nullable": true
|
|
9311
|
+
},
|
|
9312
|
+
"scheduleId": {
|
|
9313
|
+
"type": "string",
|
|
9314
|
+
"nullable": true
|
|
9315
|
+
},
|
|
9316
|
+
"tasks": {
|
|
9317
|
+
"type": "array",
|
|
9318
|
+
"items": {
|
|
9319
|
+
"type": "string"
|
|
9320
|
+
},
|
|
9321
|
+
"nullable": true
|
|
9307
9322
|
}
|
|
9308
9323
|
},
|
|
9309
9324
|
"required": [
|
|
@@ -9327,4 +9342,4 @@
|
|
|
9327
9342
|
"url": "/"
|
|
9328
9343
|
}
|
|
9329
9344
|
]
|
|
9330
|
-
}
|
|
9345
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
}
|
package/lib/watt.js
CHANGED
|
@@ -163,8 +163,10 @@ class Watt {
|
|
|
163
163
|
|
|
164
164
|
const runtime = await create(this.#appDir, null, {
|
|
165
165
|
isProduction: true,
|
|
166
|
-
transform: async (config) => {
|
|
167
|
-
|
|
166
|
+
transform: async (config, schema, options) => {
|
|
167
|
+
// Forward schema and options: the runtime's transform reads isProduction
|
|
168
|
+
// from options, and defaults watch to !isProduction when it is missing.
|
|
169
|
+
config = await transform(config, schema, options)
|
|
168
170
|
|
|
169
171
|
this.#config = config
|
|
170
172
|
this.#originalConfig = structuredClone(config)
|
|
@@ -501,20 +503,15 @@ class Watt {
|
|
|
501
503
|
#configureScheduler (config) {
|
|
502
504
|
const { Runtime } = this.#require('@platformatic/runtime')
|
|
503
505
|
if (typeof Runtime?.prototype?.pauseSchedulerJob === 'function') {
|
|
506
|
+
this.#logger.debug?.('Runtime scheduler controls available, preserving scheduler configuration')
|
|
504
507
|
return
|
|
505
508
|
}
|
|
506
509
|
|
|
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') {
|
|
510
|
+
if (!this.#env.PLT_ICC_URL) {
|
|
515
511
|
return
|
|
516
512
|
}
|
|
517
513
|
|
|
514
|
+
this.#logger.debug?.('Runtime scheduler controls unavailable, disabling configured jobs for ICC compatibility')
|
|
518
515
|
if (config.scheduler) {
|
|
519
516
|
config.scheduler = config.scheduler.map((scheduler) => ({
|
|
520
517
|
...scheduler,
|
|
@@ -538,19 +535,27 @@ class Watt {
|
|
|
538
535
|
return
|
|
539
536
|
}
|
|
540
537
|
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
538
|
+
if (!this.#env.PLT_ICC_URL) {
|
|
539
|
+
return
|
|
540
|
+
}
|
|
541
|
+
|
|
544
542
|
const jobs = await runtime.getScheduler()
|
|
545
|
-
const operation = mode === 'external' ? 'pauseSchedulerJob' : 'resumeSchedulerJob'
|
|
546
543
|
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
.map(job => runtime[operation](job.name))
|
|
544
|
+
const activeJobs = jobs.filter(job => !job.paused)
|
|
545
|
+
const results = await Promise.allSettled(
|
|
546
|
+
activeJobs.map(job => runtime.pauseSchedulerJob(job.name))
|
|
551
547
|
)
|
|
552
548
|
|
|
553
|
-
|
|
549
|
+
for (const [index, result] of results.entries()) {
|
|
550
|
+
if (result.status === 'rejected') {
|
|
551
|
+
this.#logger.warn(
|
|
552
|
+
{ err: ensureLoggableError(result.reason), job: activeJobs[index].name },
|
|
553
|
+
'Failed to pause runtime scheduler job for ICC coordination'
|
|
554
|
+
)
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
this.#logger.info({ jobs: jobs.map(job => job.name) }, 'Runtime scheduler jobs paused for ICC coordination')
|
|
554
559
|
}
|
|
555
560
|
|
|
556
561
|
async #configureServices (runtime) {
|
package/package.json
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/watt-extra",
|
|
3
|
-
"version": "1.15.0
|
|
3
|
+
"version": "1.15.0",
|
|
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
|
+
},
|
|
6
11
|
"bin": {
|
|
7
12
|
"watt-extra": "./cli.js"
|
|
8
13
|
},
|
|
@@ -14,27 +19,27 @@
|
|
|
14
19
|
},
|
|
15
20
|
"devDependencies": {
|
|
16
21
|
"@fastify/websocket": "^11.1.0",
|
|
17
|
-
"@platformatic/composer": "^3.
|
|
18
|
-
"@platformatic/next": "^3.
|
|
19
|
-
"@platformatic/node": "^3.
|
|
20
|
-
"@platformatic/service": "^3.
|
|
22
|
+
"@platformatic/composer": "^3.62.2",
|
|
23
|
+
"@platformatic/next": "^3.62.2",
|
|
24
|
+
"@platformatic/node": "^3.62.2",
|
|
25
|
+
"@platformatic/service": "^3.62.2",
|
|
21
26
|
"atomic-sleep": "^1.0.0",
|
|
22
27
|
"borp": "^1.0.0",
|
|
23
28
|
"eslint": "9",
|
|
24
|
-
"fastify": "^5.
|
|
29
|
+
"fastify": "^5.10.0",
|
|
25
30
|
"fastify-plugin": "^5.0.1",
|
|
26
31
|
"neostandard": "^0.12.0",
|
|
27
|
-
"next": "^16.
|
|
28
|
-
"platformatic": "^3.
|
|
32
|
+
"next": "^16.2.10",
|
|
33
|
+
"platformatic": "^3.62.2",
|
|
29
34
|
"pprof-format": "^2.1.0",
|
|
30
35
|
"why-is-node-running": "^2.3.0"
|
|
31
36
|
},
|
|
32
37
|
"dependencies": {
|
|
33
38
|
"@datadog/pprof": "^5.9.0",
|
|
34
39
|
"@fastify/error": "^4.2.0",
|
|
35
|
-
"@platformatic/foundation": "^3.
|
|
36
|
-
"@platformatic/runtime": "^3.
|
|
37
|
-
"@platformatic/wattpm-pprof-capture": "^3.
|
|
40
|
+
"@platformatic/foundation": "^3.62.2",
|
|
41
|
+
"@platformatic/runtime": "^3.62.2",
|
|
42
|
+
"@platformatic/wattpm-pprof-capture": "^3.62.2",
|
|
38
43
|
"avvio": "^9.1.0",
|
|
39
44
|
"chalk": "^4.1.2",
|
|
40
45
|
"commist": "^3.2.0",
|
|
@@ -50,10 +55,5 @@
|
|
|
50
55
|
"undici-slicer-interceptor": "^0.4.1",
|
|
51
56
|
"undici-traffic-interceptor": "^0.1.4",
|
|
52
57
|
"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/compliancy.js
CHANGED
package/plugins/flamegraphs.js
CHANGED
|
@@ -239,14 +239,21 @@ async function flamegraphs (app, _opts) {
|
|
|
239
239
|
async function getServiceFlamegraph (workerId, profileType, attempt = 1) {
|
|
240
240
|
const runtime = app.watt.runtime
|
|
241
241
|
|
|
242
|
+
// The runtime may have been closed while waiting between attempts
|
|
243
|
+
if (!runtime) {
|
|
244
|
+
app.log.warn({ workerId }, 'Runtime not available, cannot get profile')
|
|
245
|
+
return
|
|
246
|
+
}
|
|
247
|
+
|
|
242
248
|
app.log.info({ workerId, attempt, maxAttempts, attemptTimeout }, 'Getting profile from worker')
|
|
243
249
|
|
|
244
250
|
try {
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
251
|
+
const { profile, timestamp, preserved } = await runtime.getApplicationLastProfile(
|
|
252
|
+
workerId,
|
|
253
|
+
{ type: profileType }
|
|
254
|
+
)
|
|
255
|
+
app.log.info({ workerId, profileType, preserved }, 'Got profile from worker')
|
|
256
|
+
return { data: profile, timestamp }
|
|
250
257
|
} catch (err) {
|
|
251
258
|
if (err.code === 'PLT_PPROF_NO_PROFILE_AVAILABLE') {
|
|
252
259
|
app.log.info(
|
|
@@ -259,6 +266,15 @@ async function flamegraphs (app, _opts) {
|
|
|
259
266
|
}
|
|
260
267
|
} else if (err.code === 'PLT_PPROF_NOT_ENOUGH_ELU') {
|
|
261
268
|
app.log.info({ workerId }, 'ELU low, CPU profiling not active')
|
|
269
|
+
} else if (err.code === 'PLT_RUNTIME_LAST_PROFILE_TIMEOUT') {
|
|
270
|
+
// The runtime throws this when the worker event loop is unresponsive
|
|
271
|
+
// (e.g. saturated by high ELU or hard-blocked) and no preserved
|
|
272
|
+
// overload profile has been captured yet
|
|
273
|
+
app.log.warn(
|
|
274
|
+
{ workerId },
|
|
275
|
+
'Worker event loop is not responding, likely saturated (ELU too high), ' +
|
|
276
|
+
'and no preserved overload profile is available yet'
|
|
277
|
+
)
|
|
262
278
|
} else {
|
|
263
279
|
app.log.warn({ err, workerId }, 'Failed to get profile from a worker')
|
|
264
280
|
|
package/plugins/init.js
CHANGED
|
@@ -16,6 +16,15 @@ async function initPlugin (app) {
|
|
|
16
16
|
if (applicationName) {
|
|
17
17
|
request.applicationName = applicationName
|
|
18
18
|
}
|
|
19
|
+
|
|
20
|
+
// The deployment id this image's client assets were BUILT with, for
|
|
21
|
+
// query-string skew protection. Only the pod can report it: the build runs
|
|
22
|
+
// in CI before the image exists, so ICC never sees the build arg. It
|
|
23
|
+
// survives here if the Dockerfile promotes the build ARG to ENV.
|
|
24
|
+
if (process.env.PLT_DEPLOYMENT_ID) {
|
|
25
|
+
request.buildDeploymentId = process.env.PLT_DEPLOYMENT_ID
|
|
26
|
+
}
|
|
27
|
+
|
|
19
28
|
return controlPlaneClient.initApplicationInstance(request)
|
|
20
29
|
}
|
|
21
30
|
|
package/plugins/metadata.js
CHANGED
|
@@ -56,14 +56,11 @@ async function metadata (app, _opts) {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
|
|
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
|
|
59
|
+
let scheduler
|
|
63
60
|
try {
|
|
64
61
|
scheduler = await app.collectSchedulerJobs()
|
|
65
62
|
} catch (error) {
|
|
66
|
-
app.log.warn(error, 'Failed to collect
|
|
63
|
+
app.log.warn(error, 'Failed to collect scheduler jobs, not reporting them')
|
|
67
64
|
}
|
|
68
65
|
|
|
69
66
|
try {
|
package/plugins/scheduler.js
CHANGED
|
@@ -9,7 +9,7 @@ async function scheduler (app, _opts) {
|
|
|
9
9
|
return runtime.getScheduler()
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
//
|
|
12
|
+
// Legacy compatibility path: use the original configuration, since the runtime one
|
|
13
13
|
// has been disabled by the config patch (see Watt#configureScheduler)
|
|
14
14
|
const jobs = app.watt.getOriginalSchedulerConfig?.() ?? []
|
|
15
15
|
return jobs
|
|
@@ -38,11 +38,16 @@ async function scheduler (app, _opts) {
|
|
|
38
38
|
|
|
39
39
|
app.log.info({ name, source }, 'Executing scheduled job triggered by ICC')
|
|
40
40
|
|
|
41
|
-
//
|
|
41
|
+
// Runtime-managed jobs, including application-level tasks, do not need a
|
|
42
|
+
// callback URL. Legacy config jobs fall back to their callback below.
|
|
42
43
|
if (typeof runtime.runSchedulerJob === 'function') {
|
|
43
44
|
return runtime.runSchedulerJob(name)
|
|
44
45
|
}
|
|
45
46
|
|
|
47
|
+
if (source === 'application') {
|
|
48
|
+
throw new Error(`Cannot execute application scheduled job "${name}": runtime scheduler controls are unavailable`)
|
|
49
|
+
}
|
|
50
|
+
|
|
46
51
|
if (!callbackUrl) {
|
|
47
52
|
throw new Error(`Cannot execute scheduled job "${name}": no callback URL`)
|
|
48
53
|
}
|
|
@@ -82,21 +87,8 @@ async function scheduler (app, _opts) {
|
|
|
82
87
|
return
|
|
83
88
|
}
|
|
84
89
|
|
|
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
|
|
90
|
+
// Legacy ICC integration path: the current API exposes one PUT endpoint
|
|
91
|
+
// per job. Newer ICC versions register application jobs from state.
|
|
100
92
|
try {
|
|
101
93
|
const applicationId = app.instanceConfig?.applicationId
|
|
102
94
|
const { default: build, setDefaultHeaders } = await import('../clients/cron/cron.mjs')
|
|
@@ -109,36 +101,61 @@ async function scheduler (app, _opts) {
|
|
|
109
101
|
const cronClient = build(cronUrl)
|
|
110
102
|
setDefaultHeaders(await app.getAuthorizationHeaders())
|
|
111
103
|
|
|
112
|
-
const jobs =
|
|
104
|
+
const jobs = await collectSchedulerJobs()
|
|
105
|
+
const configJobs = jobs.filter((job) => job.source === 'config')
|
|
106
|
+
const applicationJobs = jobs.filter((job) => job.source === 'application')
|
|
113
107
|
|
|
114
|
-
|
|
115
|
-
for (const job of jobs) {
|
|
108
|
+
async function saveJob (job) {
|
|
116
109
|
const iccJob = {
|
|
117
110
|
name: job.name,
|
|
118
|
-
|
|
119
|
-
schedule: job.cron, // unfortunately, the ICC API uses `schedule` instead of `cron`
|
|
111
|
+
schedule: job.cron,
|
|
120
112
|
method: job.method,
|
|
121
113
|
maxRetries: job.maxRetries,
|
|
122
114
|
applicationId
|
|
123
115
|
}
|
|
124
|
-
|
|
125
|
-
|
|
116
|
+
|
|
117
|
+
if (job.source === 'application') {
|
|
118
|
+
iccJob.source = job.source
|
|
119
|
+
iccJob.scheduleId = job.scheduleId
|
|
120
|
+
iccJob.tasks = job.tasks
|
|
121
|
+
} else {
|
|
122
|
+
iccJob.callbackUrl = job.callbackUrl
|
|
123
|
+
if (job.headers && Object.keys(job.headers).length > 0) {
|
|
124
|
+
iccJob.headers = job.headers
|
|
125
|
+
}
|
|
126
|
+
if (job.body && (typeof job.body === 'string' ? job.body.length > 0 : Object.keys(job.body).length > 0)) {
|
|
127
|
+
iccJob.body = job.body
|
|
128
|
+
}
|
|
126
129
|
}
|
|
127
|
-
|
|
128
|
-
|
|
130
|
+
|
|
131
|
+
const result = await cronClient.putWattJobs(iccJob)
|
|
132
|
+
if (result.statusCode >= 400) {
|
|
133
|
+
const error = new Error(`ICC returned HTTP ${result.statusCode} while saving job "${job.name}"`)
|
|
134
|
+
error.statusCode = result.statusCode
|
|
135
|
+
throw error
|
|
129
136
|
}
|
|
130
|
-
saveJobs.push(cronClient.putWattJobs(iccJob))
|
|
131
137
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
138
|
+
|
|
139
|
+
for (const job of configJobs) {
|
|
140
|
+
await saveJob(job)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
for (const [index, job] of applicationJobs.entries()) {
|
|
144
|
+
try {
|
|
145
|
+
await saveJob(job)
|
|
146
|
+
} catch (error) {
|
|
147
|
+
if (index === 0 && error.statusCode === 400) {
|
|
148
|
+
app.log.warn('ICC does not support application scheduler jobs, skipping application registrations')
|
|
149
|
+
break
|
|
150
|
+
}
|
|
151
|
+
throw error
|
|
152
|
+
}
|
|
137
153
|
}
|
|
138
154
|
|
|
139
155
|
app.log.info('Scheduler configured')
|
|
140
156
|
} catch (error) {
|
|
141
157
|
app.log.error(error, 'Failed in configuring watt jobs in ICC')
|
|
158
|
+
throw error
|
|
142
159
|
}
|
|
143
160
|
}
|
|
144
161
|
|