@platformatic/watt-extra 1.15.0-alpha.3 → 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.
@@ -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.0"
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@v6
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@v6
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 minimist from 'minimist'
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 = 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
- })
38
+ const { args, help } = applyStartArgs(argv)
53
39
 
54
40
  logger.debug({ args, argv }, 'Start command arguments')
55
41
 
56
- if (args.help) {
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
  }
@@ -2042,6 +2042,7 @@ export type SaveApplicationInstanceStateRequest = {
2042
2042
  'id': string;
2043
2043
  'metadata': { 'platformaticVersion': string };
2044
2044
  'services': Array<object>;
2045
+ 'scheduler'?: Array<object>;
2045
2046
  }
2046
2047
 
2047
2048
  /**
@@ -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
@@ -575,6 +575,8 @@ async function _getIccJobsName (url, request) {
575
575
  const responseType = response.headers.get('content-type')?.startsWith('application/json') ? 'json' : 'text'
576
576
  let body
577
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.
578
580
  body = await response[responseType]()
579
581
  } catch {
580
582
  body = null
@@ -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
- config = await transform(config)
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,6 +503,7 @@ 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
 
@@ -508,6 +511,7 @@ class Watt {
508
511
  return
509
512
  }
510
513
 
514
+ this.#logger.debug?.('Runtime scheduler controls unavailable, disabling configured jobs for ICC compatibility')
511
515
  if (config.scheduler) {
512
516
  config.scheduler = config.scheduler.map((scheduler) => ({
513
517
  ...scheduler,
@@ -537,12 +541,20 @@ class Watt {
537
541
 
538
542
  const jobs = await runtime.getScheduler()
539
543
 
540
- await Promise.all(
541
- jobs
542
- .filter(job => !job.paused)
543
- .map(job => runtime.pauseSchedulerJob(job.name))
544
+ const activeJobs = jobs.filter(job => !job.paused)
545
+ const results = await Promise.allSettled(
546
+ activeJobs.map(job => runtime.pauseSchedulerJob(job.name))
544
547
  )
545
548
 
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
+
546
558
  this.#logger.info({ jobs: jobs.map(job => job.name) }, 'Runtime scheduler jobs paused for ICC coordination')
547
559
  }
548
560
 
package/package.json CHANGED
@@ -1,8 +1,13 @@
1
1
  {
2
2
  "name": "@platformatic/watt-extra",
3
- "version": "1.15.0-alpha.3",
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.25.0",
18
- "@platformatic/next": "^3.25.0",
19
- "@platformatic/node": "^3.25.0",
20
- "@platformatic/service": "^3.25.0",
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.4.0",
29
+ "fastify": "^5.10.0",
25
30
  "fastify-plugin": "^5.0.1",
26
31
  "neostandard": "^0.12.0",
27
- "next": "^16.0.0",
28
- "platformatic": "^3.25.0",
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.25.0",
36
- "@platformatic/runtime": "^3.25.0",
37
- "@platformatic/wattpm-pprof-capture": "^3.25.0",
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
+ }
@@ -60,7 +60,7 @@ async function compliancy (app, _opts) {
60
60
  const { compliant, report } = JSON.parse(res.body)
61
61
 
62
62
  if (!compliant) {
63
- app.log.warn(report, 'Compliancy check failed')
63
+ app.log.info(report, 'Compliancy check failed')
64
64
  }
65
65
  }
66
66
  }
@@ -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 [state, profile] = await Promise.all([
246
- runtime.sendCommandToApplication(workerId, 'getProfilingState', { type: profileType }),
247
- runtime.sendCommandToApplication(workerId, 'getLastProfile', { type: profileType })
248
- ])
249
- return { data: profile, timestamp: state.latestProfileTimestamp }
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
 
@@ -56,6 +56,13 @@ async function metadata (app, _opts) {
56
56
  }
57
57
  }
58
58
 
59
+ let scheduler
60
+ try {
61
+ scheduler = await app.collectSchedulerJobs()
62
+ } catch (error) {
63
+ app.log.warn(error, 'Failed to collect scheduler jobs, not reporting them')
64
+ }
65
+
59
66
  try {
60
67
  // There is a better way? We need to set the default headers for the client
61
68
  // every time, because the token might be expired
@@ -65,6 +72,7 @@ async function metadata (app, _opts) {
65
72
  id: app.instanceId,
66
73
  services,
67
74
  metadata: runtimeMetadata,
75
+ ...(scheduler ? { scheduler } : {})
68
76
  }, {
69
77
  headers: await app.getAuthorizationHeaders()
70
78
  })
@@ -9,7 +9,7 @@ async function scheduler (app, _opts) {
9
9
  return runtime.getScheduler()
10
10
  }
11
11
 
12
- // Older runtimes: use the original configuration, since the runtime one
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
- // Newer runtimes own both configured and application-level jobs.
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,8 +87,8 @@ async function scheduler (app, _opts) {
82
87
  return
83
88
  }
84
89
 
85
- // Register jobs directly on the cron service. Config jobs retain their
86
- // callbacks; application jobs are mapped by new ICC versions to Main.
90
+ // Legacy ICC integration path: the current API exposes one PUT endpoint
91
+ // per job. Newer ICC versions register application jobs from state.
87
92
  try {
88
93
  const applicationId = app.instanceConfig?.applicationId
89
94
  const { default: build, setDefaultHeaders } = await import('../clients/cron/cron.mjs')
@@ -799,8 +799,8 @@ test('should attach one flamegraph to multiple alerts', async (t) => {
799
799
  await icc.close()
800
800
  })
801
801
 
802
- // Wait for the first flamegraph to be generated
803
- await sleep(5000)
802
+ // Wait for the first profiling window (3s grace period + 5s interval) to complete
803
+ await sleep(8500)
804
804
 
805
805
  // Manually trigger health event with unhealthy state
806
806
  const healthInfo = {
@@ -902,8 +902,8 @@ test('should send flamegraphs if attaching fails', async (t) => {
902
902
  await icc.close()
903
903
  })
904
904
 
905
- // Wait for the first flamegraph to be generated
906
- await sleep(5000)
905
+ // Wait for the first profiling window (3s grace period + 5s interval) to complete
906
+ await sleep(8500)
907
907
 
908
908
  // Manually trigger health event with unhealthy state
909
909
  const healthInfo = {
package/test/cli.test.js CHANGED
@@ -1,8 +1,9 @@
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, dirname } from 'node:path'
4
+ import { join, resolve, dirname } from 'node:path'
5
5
  import { fileURLToPath } from 'node:url'
6
+ import { applyStartArgs } from '../lib/start-args.js'
6
7
 
7
8
  const __filename = fileURLToPath(import.meta.url)
8
9
  const __dirname = dirname(__filename)
@@ -90,3 +91,51 @@ test('CLI should output JSON log without banner in non-TTY mode (version command
90
91
  'Non-TTY output should include JSON log with version'
91
92
  )
92
93
  })
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
+ })
package/test/init.test.js CHANGED
@@ -617,3 +617,93 @@ test('updateInstanceConfig preserves existing applicationLabel when ICC does not
617
617
  equal(receivedMetricsConfig.applicationLabel, 'serviceId')
618
618
  equal(receivedMetricsConfig.labels.applicationId, applicationId)
619
619
  })
620
+
621
+ test('init plugin reports the baked deployment id when PLT_DEPLOYMENT_ID is set', async (t) => {
622
+ const applicationName = 'test-app-baked'
623
+ const applicationId = randomUUID()
624
+
625
+ let capturedRequest = null
626
+
627
+ const icc = await startICC(t, {
628
+ applicationId,
629
+ controlPlaneResponse: (req) => {
630
+ capturedRequest = { body: req.body }
631
+ return {
632
+ applicationId,
633
+ applicationName,
634
+ iccServices: {
635
+ riskEngine: { url: 'http://127.0.0.1:3000/risk-service' },
636
+ trafficInspector: { url: 'http://127.0.0.1:3000/traffic-inspector' },
637
+ compliance: { url: 'http://127.0.0.1:3000/compliance' },
638
+ cron: { url: 'http://127.0.0.1:3000/cron' },
639
+ scaler: { url: 'http://127.0.0.1:3000/scaler' }
640
+ },
641
+ config: {}
642
+ }
643
+ }
644
+ })
645
+
646
+ const previous = process.env.PLT_DEPLOYMENT_ID
647
+ process.env.PLT_DEPLOYMENT_ID = 'a1b2c3d4e5f6'
648
+
649
+ t.after(async () => {
650
+ if (previous === undefined) delete process.env.PLT_DEPLOYMENT_ID
651
+ else process.env.PLT_DEPLOYMENT_ID = previous
652
+ await icc.close()
653
+ })
654
+
655
+ const app = createMockApp({
656
+ PLT_APP_NAME: applicationName,
657
+ PLT_APP_DIR: '/test/dir'
658
+ })
659
+
660
+ await initPlugin(app)
661
+
662
+ // The value the image was built with, so ICC can tell whether this version's
663
+ // assets carry `?dpl=<its own version>` and may be query-pinned.
664
+ equal(capturedRequest.body.buildDeploymentId, 'a1b2c3d4e5f6')
665
+ })
666
+
667
+ test('init plugin omits the deployment id when PLT_DEPLOYMENT_ID is unset', async (t) => {
668
+ const applicationName = 'test-app-unbaked'
669
+ const applicationId = randomUUID()
670
+
671
+ let capturedRequest = null
672
+
673
+ const icc = await startICC(t, {
674
+ applicationId,
675
+ controlPlaneResponse: (req) => {
676
+ capturedRequest = { body: req.body }
677
+ return {
678
+ applicationId,
679
+ applicationName,
680
+ iccServices: {
681
+ riskEngine: { url: 'http://127.0.0.1:3000/risk-service' },
682
+ trafficInspector: { url: 'http://127.0.0.1:3000/traffic-inspector' },
683
+ compliance: { url: 'http://127.0.0.1:3000/compliance' },
684
+ cron: { url: 'http://127.0.0.1:3000/cron' },
685
+ scaler: { url: 'http://127.0.0.1:3000/scaler' }
686
+ },
687
+ config: {}
688
+ }
689
+ }
690
+ })
691
+
692
+ const previous = process.env.PLT_DEPLOYMENT_ID
693
+ delete process.env.PLT_DEPLOYMENT_ID
694
+
695
+ t.after(async () => {
696
+ if (previous !== undefined) process.env.PLT_DEPLOYMENT_ID = previous
697
+ await icc.close()
698
+ })
699
+
700
+ const app = createMockApp({
701
+ PLT_APP_NAME: applicationName,
702
+ PLT_APP_DIR: '/test/dir'
703
+ })
704
+
705
+ await initPlugin(app)
706
+
707
+ // A build that was never given an id must not claim one.
708
+ equal(capturedRequest.body.buildDeploymentId, undefined)
709
+ })
@@ -88,7 +88,9 @@ test('ICC coordination: should disable local jobs and register config jobs direc
88
88
 
89
89
  // The jobs are reported in the application state
90
90
  assert.ok(savedState, 'the state should have been reported')
91
- assert.strictEqual(savedState.scheduler, undefined)
91
+ assert.ok(Array.isArray(savedState.scheduler))
92
+ assert.strictEqual(savedState.scheduler[0].name, 'test')
93
+ assert.strictEqual(savedState.scheduler[0].source, 'config')
92
94
  })
93
95
 
94
96
  test('ICC coordination: should disable local jobs regardless of scheduler mode', async (t) => {
@@ -159,7 +161,7 @@ test('runtime scheduler ownership always pauses jobs when ICC is configured', as
159
161
  PLT_APP_DIR: join(__dirname, 'fixtures', 'runtime-scheduler'),
160
162
  PLT_ICC_URL: 'http://localhost:3000'
161
163
  },
162
- log: { info: () => {} },
164
+ log: { info: () => {}, debug: () => {}, warn: () => {} },
163
165
  instanceConfig,
164
166
  instanceId: 'test-pod-123'
165
167
  })
@@ -181,6 +183,36 @@ test('runtime scheduler ownership always pauses jobs when ICC is configured', as
181
183
  assert.deepStrictEqual(jobs.map(job => job.paused), [true, true])
182
184
  })
183
185
 
186
+ test('runtime scheduler ownership continues when pausing one job fails', async () => {
187
+ const jobs = [
188
+ { name: 'failed', paused: false },
189
+ { name: 'paused', paused: false }
190
+ ]
191
+ const watt = new Watt({
192
+ env: {
193
+ PLT_APP_DIR: join(__dirname, 'fixtures', 'runtime-scheduler'),
194
+ PLT_ICC_URL: 'http://localhost:3000'
195
+ },
196
+ log: { info: () => {}, debug: () => {}, warn: () => {} },
197
+ instanceConfig: {},
198
+ instanceId: 'test-pod-123'
199
+ })
200
+
201
+ watt.runtime = {
202
+ getScheduler: () => jobs,
203
+ pauseSchedulerJob: async name => {
204
+ if (name === 'failed') {
205
+ throw new Error('pause failed')
206
+ }
207
+ jobs[1].paused = true
208
+ },
209
+ resumeSchedulerJob: async () => {}
210
+ }
211
+
212
+ await watt.applySchedulerMode()
213
+ assert.deepStrictEqual(jobs.map(job => job.paused), [false, true])
214
+ })
215
+
184
216
  function setupMockIccServer (wss) {
185
217
  let ws = null
186
218
 
@@ -280,7 +312,7 @@ test('run-scheduled-job: should route legacy internal callbacks through the runt
280
312
  assert.strictEqual(injected[0].body, '{"scheduledTime":1}')
281
313
  })
282
314
 
283
- test('run-scheduled-job: should use native runtime execution for application jobs', async (t) => {
315
+ test('run-scheduled-job: should use native runtime execution for application tasks without callbacks', async (t) => {
284
316
  const app = createMockApp(0)
285
317
 
286
318
  const executed = []
@@ -297,6 +329,17 @@ test('run-scheduled-job: should use native runtime execution for application job
297
329
  assert.deepStrictEqual(result, { name: 'frontend:0', success: true })
298
330
  })
299
331
 
332
+ test('run-scheduled-job: should reject application tasks without runtime support', async (t) => {
333
+ const app = createMockApp(0)
334
+
335
+ await schedulerPlugin(app)
336
+
337
+ await assert.rejects(
338
+ app.runScheduledJob({ name: 'frontend:0', source: 'application' }),
339
+ /runtime scheduler controls are unavailable/
340
+ )
341
+ })
342
+
300
343
  test('run-scheduled-job: should fail on non-2xx callback responses', async (t) => {
301
344
  const app = createMockApp(0)
302
345
 
@@ -275,11 +275,8 @@ test('sendFlamegraphs should upload flamegraphs from all services', async (t) =>
275
275
 
276
276
  const mockProfile = new Uint8Array([1, 2, 3, 4, 5])
277
277
 
278
- app.watt.runtime.sendCommandToApplication = async (serviceId, command) => {
279
- if (command === 'getLastProfile') {
280
- return mockProfile
281
- }
282
- return { success: false }
278
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
279
+ return { profile: mockProfile, timestamp: Date.now(), preserved: false }
283
280
  }
284
281
 
285
282
  // Mock HTTP server to receive flamegraphs
@@ -328,12 +325,9 @@ test('sendFlamegraphs should handle missing profile data', async (t) => {
328
325
  errors.push(obj)
329
326
  }
330
327
 
331
- app.watt.runtime.sendCommandToApplication = async (serviceId, command) => {
332
- if (command === 'getLastProfile') {
333
- // Return invalid data (not Uint8Array)
334
- return null
335
- }
336
- return { success: false }
328
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
329
+ // Return invalid data (not Uint8Array)
330
+ return { profile: null, timestamp: null, preserved: false }
337
331
  }
338
332
 
339
333
  await flamegraphsPlugin(app)
@@ -348,12 +342,9 @@ test('sendFlamegraphs should filter by workerIds when provided', async (t) => {
348
342
  const app = createMockApp(port + 12)
349
343
  const getProfileCalls = []
350
344
 
351
- app.watt.runtime.sendCommandToApplication = async (workerId, command) => {
352
- if (command === 'getLastProfile') {
353
- getProfileCalls.push(workerId)
354
- return new Uint8Array([1, 2, 3])
355
- }
356
- return { success: false }
345
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
346
+ getProfileCalls.push(workerId)
347
+ return { profile: new Uint8Array([1, 2, 3]), timestamp: Date.now(), preserved: false }
357
348
  }
358
349
 
359
350
  // Mock HTTP server
@@ -383,15 +374,12 @@ test('sendFlamegraphs should try to get the profile from a service if worker is
383
374
  const app = createMockApp(port + 12)
384
375
  const getProfileCalls = []
385
376
 
386
- app.watt.runtime.sendCommandToApplication = async (workerId, command) => {
387
- if (command === 'getLastProfile') {
388
- getProfileCalls.push(workerId)
389
- if (workerId === 'service-1:2') {
390
- throw new Error('Worker not available')
391
- }
392
- return new Uint8Array([1, 2, 3])
377
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
378
+ getProfileCalls.push(workerId)
379
+ if (workerId === 'service-1:2') {
380
+ throw new Error('Worker not available')
393
381
  }
394
- return { success: false }
382
+ return { profile: new Uint8Array([1, 2, 3]), timestamp: Date.now(), preserved: false }
395
383
  }
396
384
 
397
385
  // Mock HTTP server
@@ -424,12 +412,9 @@ test('sendFlamegraphs should skip when PLT_DISABLE_FLAMEGRAPHS is set', async (t
424
412
 
425
413
  const getProfileCalls = []
426
414
 
427
- app.watt.runtime.sendCommandToApplication = async (workerId, command) => {
428
- if (command === 'getLastProfile') {
429
- getProfileCalls.push(workerId)
430
- return new Uint8Array([1, 2, 3])
431
- }
432
- return { success: false }
415
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
416
+ getProfileCalls.push(workerId)
417
+ return { profile: new Uint8Array([1, 2, 3]), timestamp: Date.now(), preserved: false }
433
418
  }
434
419
 
435
420
  await flamegraphsPlugin(app)
@@ -484,16 +469,17 @@ test('should handle trigger-flamegraph command and upload flamegraphs from servi
484
469
  if (command === 'startProfiling') {
485
470
  return { success: true }
486
471
  }
487
- if (command === 'getLastProfile') {
488
- getFlamegraphReqs.push({ serviceId })
489
- if (getFlamegraphReqs.length === 2) {
490
- uploadResolve()
491
- }
492
- return new Uint8Array([1, 2, 3, 4, 5])
493
- }
494
472
  return { success: false }
495
473
  }
496
474
 
475
+ app.watt.runtime.getApplicationLastProfile = async (serviceId, options) => {
476
+ getFlamegraphReqs.push({ serviceId })
477
+ if (getFlamegraphReqs.length === 2) {
478
+ uploadResolve()
479
+ }
480
+ return { profile: new Uint8Array([1, 2, 3, 4, 5]), timestamp: Date.now(), preserved: false }
481
+ }
482
+
497
483
  await updatePlugin(app)
498
484
  await flamegraphsPlugin(app)
499
485
 
@@ -593,6 +579,10 @@ test('should handle trigger-flamegraph when flamegraph upload fails', async (t)
593
579
  return { success: false }
594
580
  }
595
581
 
582
+ app.watt.runtime.getApplicationLastProfile = async (serviceId, options) => {
583
+ return { profile: new Uint8Array([1, 2, 3, 4, 5]), timestamp: Date.now(), preserved: false }
584
+ }
585
+
596
586
  await updatePlugin(app)
597
587
  await flamegraphsPlugin(app)
598
588
 
@@ -645,16 +635,18 @@ test('should handle trigger-heapprofile command and upload heap profiles from se
645
635
  if (command === 'startProfiling') {
646
636
  return { success: true }
647
637
  }
648
- if (command === 'getLastProfile') {
649
- getHeapProfileReqs.push({ serviceId })
650
- if (getHeapProfileReqs.length === 2) {
651
- uploadResolve()
652
- }
653
- return new Uint8Array([1, 2, 3, 4, 5])
654
- }
655
638
  return { success: false }
656
639
  }
657
640
 
641
+ app.watt.runtime.getApplicationLastProfile = async (serviceId, options) => {
642
+ equal(options.type, 'heap')
643
+ getHeapProfileReqs.push({ serviceId })
644
+ if (getHeapProfileReqs.length === 2) {
645
+ uploadResolve()
646
+ }
647
+ return { profile: new Uint8Array([1, 2, 3, 4, 5]), timestamp: Date.now(), preserved: false }
648
+ }
649
+
658
650
  await updatePlugin(app)
659
651
  await flamegraphsPlugin(app)
660
652
 
@@ -728,18 +720,19 @@ test('should handle PLT_PPROF_NO_PROFILE_AVAILABLE error with info log', async (
728
720
  if (command === 'startProfiling') {
729
721
  return { success: true }
730
722
  }
731
- if (command === 'getLastProfile') {
732
- const now = Date.now()
733
- if (now < profileGenerationDate) {
734
- const error = new Error('No profile available - wait for profiling to complete or trigger manual capture')
735
- error.code = 'PLT_PPROF_NO_PROFILE_AVAILABLE'
736
- throw error
737
- }
738
- return mockProfile
739
- }
740
723
  return { success: false }
741
724
  }
742
725
 
726
+ app.watt.runtime.getApplicationLastProfile = async (serviceId, options) => {
727
+ const now = Date.now()
728
+ if (now < profileGenerationDate) {
729
+ const error = new Error('No profile available - wait for profiling to complete or trigger manual capture')
730
+ error.code = 'PLT_PPROF_NO_PROFILE_AVAILABLE'
731
+ throw error
732
+ }
733
+ return { profile: mockProfile, timestamp: now, preserved: false }
734
+ }
735
+
743
736
  await updatePlugin(app)
744
737
  await flamegraphsPlugin(app)
745
738
 
@@ -844,14 +837,15 @@ test('should handle PLT_PPROF_NOT_ENOUGH_ELU error with info log', async (t) =>
844
837
  if (command === 'startProfiling') {
845
838
  return { success: true }
846
839
  }
847
- if (command === 'getLastProfile') {
848
- const error = new Error('No profile available - event loop utilization has been below threshold for too long')
849
- error.code = 'PLT_PPROF_NOT_ENOUGH_ELU'
850
- throw error
851
- }
852
840
  return { success: false }
853
841
  }
854
842
 
843
+ app.watt.runtime.getApplicationLastProfile = async (serviceId, options) => {
844
+ const error = new Error('No profile available - event loop utilization has been below threshold for too long')
845
+ error.code = 'PLT_PPROF_NOT_ENOUGH_ELU'
846
+ throw error
847
+ }
848
+
855
849
  await updatePlugin(app)
856
850
  await flamegraphsPlugin(app)
857
851
 
@@ -1016,11 +1010,8 @@ test('sendFlamegraphs should include alertId in query params when provided', asy
1016
1010
 
1017
1011
  const app = createMockApp(port + 18)
1018
1012
 
1019
- app.watt.runtime.sendCommandToApplication = async (serviceId, command) => {
1020
- if (command === 'getLastProfile') {
1021
- return new Uint8Array([1, 2, 3])
1022
- }
1023
- return { success: false }
1013
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
1014
+ return { profile: new Uint8Array([1, 2, 3]), timestamp: Date.now(), preserved: false }
1024
1015
  }
1025
1016
 
1026
1017
  // Mock HTTP server to capture requests
@@ -1050,3 +1041,81 @@ test('sendFlamegraphs should include alertId in query params when provided', asy
1050
1041
  ok(req.url.includes('alertId=test-alert-123'), 'URL should include alertId query param')
1051
1042
  }
1052
1043
  })
1044
+
1045
+ test('sendFlamegraphs should log warn when worker is blocked and no preserved profile exists', async (t) => {
1046
+ setUpEnvironment()
1047
+
1048
+ const app = createMockApp(port + 19)
1049
+ const warnLogs = []
1050
+ const getProfileCalls = []
1051
+
1052
+ const originalWarn = app.log.warn
1053
+ app.log.warn = (...args) => {
1054
+ originalWarn(...args)
1055
+ warnLogs.push(args)
1056
+ }
1057
+
1058
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
1059
+ getProfileCalls.push(workerId)
1060
+ const error = new Error('Timed out while waiting for the last profile')
1061
+ error.code = 'PLT_RUNTIME_LAST_PROFILE_TIMEOUT'
1062
+ throw error
1063
+ }
1064
+
1065
+ await flamegraphsPlugin(app)
1066
+ await app.sendFlamegraphs({ workerIds: ['service-1:0'] })
1067
+
1068
+ equal(getProfileCalls.length, 1, 'Should not retry on profile timeout')
1069
+
1070
+ const timeoutLog = warnLogs.find(
1071
+ ([, message]) => typeof message === 'string' && message.includes('Worker event loop is not responding')
1072
+ )
1073
+ ok(timeoutLog, 'Should log warn about the profile timeout')
1074
+ })
1075
+
1076
+ test('sendFlamegraphs should upload preserved profiles and log the preserved flag', async (t) => {
1077
+ setUpEnvironment()
1078
+
1079
+ const uploadedFlamegraphs = []
1080
+ const httpPort = port + 20
1081
+ const app = createMockApp(httpPort)
1082
+ const infoLogs = []
1083
+
1084
+ const originalInfo = app.log.info
1085
+ app.log.info = (...args) => {
1086
+ originalInfo(...args)
1087
+ infoLogs.push(args)
1088
+ }
1089
+
1090
+ const mockProfile = new Uint8Array([9, 8, 7])
1091
+
1092
+ app.watt.runtime.getApplicationLastProfile = async (workerId, options) => {
1093
+ return { profile: mockProfile, timestamp: Date.now(), preserved: true }
1094
+ }
1095
+
1096
+ // Mock HTTP server to receive flamegraphs
1097
+ const { createServer } = await import('node:http')
1098
+ const server = createServer((req, res) => {
1099
+ const body = []
1100
+ req.on('data', chunk => body.push(chunk))
1101
+ req.on('end', () => {
1102
+ uploadedFlamegraphs.push({ url: req.url, body: Buffer.concat(body) })
1103
+ res.writeHead(200)
1104
+ res.end()
1105
+ })
1106
+ })
1107
+
1108
+ await new Promise(resolve => server.listen(httpPort, resolve))
1109
+ t.after(() => server.close())
1110
+
1111
+ await flamegraphsPlugin(app)
1112
+ await app.sendFlamegraphs()
1113
+
1114
+ equal(uploadedFlamegraphs.length, 2, 'Should upload preserved profiles for both services')
1115
+ deepEqual(uploadedFlamegraphs[0].body, Buffer.from(mockProfile))
1116
+
1117
+ const preservedLog = infoLogs.find(
1118
+ ([options, message]) => message === 'Got profile from worker' && options.preserved === true
1119
+ )
1120
+ ok(preservedLog, 'Should log that the profile is a preserved one')
1121
+ })
@@ -0,0 +1,54 @@
1
+ import assert from 'node:assert'
2
+ import { test } from 'node:test'
3
+ import { join, dirname } from 'node:path'
4
+ import { fileURLToPath } from 'node:url'
5
+
6
+ import { randomUUID } from 'node:crypto'
7
+ import { setUpEnvironment, startICC, installDeps } from './helper.js'
8
+ import { start } from '../index.js'
9
+
10
+ const __filename = fileURLToPath(import.meta.url)
11
+ const __dirname = dirname(__filename)
12
+
13
+ // watt-extra always starts the runtime with isProduction: true, so the runtime
14
+ // must resolve watch to false. The runtime defaults watch to !isProduction, and
15
+ // reads isProduction from the options passed to its transform, so a transform
16
+ // that does not forward them silently enables file watching in production.
17
+ // This needs a @platformatic/runtime app: a single-application config instead
18
+ // goes through wrapInRuntimeConfig, which never calls the custom transform.
19
+ test('should disable file watching in production', async (t) => {
20
+ const appName = 'test-app'
21
+ const applicationId = randomUUID()
22
+ const applicationPath = join(__dirname, 'fixtures', 'runtime-service')
23
+
24
+ await installDeps(t, applicationPath)
25
+
26
+ const icc = await startICC(t, {
27
+ applicationId,
28
+ port: 3011
29
+ })
30
+
31
+ setUpEnvironment({
32
+ PLT_APP_NAME: appName,
33
+ PLT_APP_DIR: applicationPath,
34
+ PLT_ICC_URL: icc.iccUrl,
35
+ PLT_CONTROL_PLANE_URL: 'http://127.0.0.1:3012',
36
+ PLT_APP_PORT: 3053,
37
+ PLT_METRICS_PORT: 9102
38
+ })
39
+
40
+ const app = await start()
41
+
42
+ t.after(async () => {
43
+ await app.close()
44
+ await icc.close()
45
+ })
46
+
47
+ const mainConfig = app.watt.runtime.getRuntimeConfig(true)
48
+
49
+ assert.strictEqual(mainConfig.watch, false)
50
+
51
+ for (const application of mainConfig.applications ?? []) {
52
+ assert.strictEqual(application.watch, false, `application ${application.id} must not watch files`)
53
+ }
54
+ })