@platformatic/watt-extra 1.13.0 → 1.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
+ }
package/cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import commist from 'commist'
3
3
  import minimist from 'minimist'
4
4
  import { resolve, join, dirname } from 'node:path'
5
- import { fileURLToPath } from 'node:url'
5
+ import { fileURLToPath, pathToFileURL } from 'node:url'
6
6
  import helpMeInit from 'help-me'
7
7
  import { readFileSync } from 'node:fs'
8
8
  import { start, logger } from './index.js'
@@ -27,14 +27,11 @@ function version () {
27
27
  }
28
28
  }
29
29
 
30
- // Handle start command
31
- async function startCommand (argv) {
32
- if (process.stdout.isTTY) {
33
- console.log(getSimpleBanner(pkg.version))
34
- } else {
35
- logger.info(`WattExtra v${pkg.version}`)
36
- }
37
-
30
+ // Parse start command argv and apply CLI options to env.
31
+ // Precedence for PLT_LOG_LEVEL: --log-level flag > pre-set env > 'info' default.
32
+ // Do NOT add a minimist default for `log-level`: that would make args['log-level']
33
+ // always truthy and unconditionally clobber an env-provided value.
34
+ export function applyStartArgs (argv, env = process.env) {
38
35
  const args = minimist(argv, {
39
36
  alias: {
40
37
  h: 'help',
@@ -46,33 +43,50 @@ async function startCommand (argv) {
46
43
  boolean: ['help'],
47
44
  string: ['log-level', 'icc-url', 'app-name', 'app-dir'],
48
45
  default: {
49
- 'log-level': 'info',
50
46
  'app-dir': process.cwd()
51
47
  }
52
48
  })
53
49
 
54
- logger.debug({ args, argv }, 'Start command arguments')
55
-
56
50
  if (args.help) {
57
- helpMe.toStdout('start')
58
- return true
51
+ return { args, help: true }
59
52
  }
60
53
 
61
- // Set environment variables based on CLI options
62
54
  if (args['log-level']) {
63
- process.env.PLT_LOG_LEVEL = args['log-level']
55
+ env.PLT_LOG_LEVEL = args['log-level']
56
+ } else if (!env.PLT_LOG_LEVEL) {
57
+ env.PLT_LOG_LEVEL = 'info'
64
58
  }
65
59
 
66
60
  if (args['icc-url']) {
67
- process.env.PLT_ICC_URL = args['icc-url']
61
+ env.PLT_ICC_URL = args['icc-url']
68
62
  }
69
63
 
70
64
  if (args['app-name']) {
71
- process.env.PLT_APP_NAME = args['app-name']
65
+ env.PLT_APP_NAME = args['app-name']
72
66
  }
73
67
 
74
68
  if (args['app-dir']) {
75
- process.env.PLT_APP_DIR = resolve(args['app-dir']) // Ensure the path is absolute
69
+ env.PLT_APP_DIR = resolve(args['app-dir']) // Ensure the path is absolute
70
+ }
71
+
72
+ return { args, help: false }
73
+ }
74
+
75
+ // Handle start command
76
+ async function startCommand (argv) {
77
+ if (process.stdout.isTTY) {
78
+ console.log(getSimpleBanner(pkg.version))
79
+ } else {
80
+ logger.info(`WattExtra v${pkg.version}`)
81
+ }
82
+
83
+ const { args, help } = applyStartArgs(argv)
84
+
85
+ logger.debug({ args, argv }, 'Start command arguments')
86
+
87
+ if (help) {
88
+ helpMe.toStdout('start')
89
+ return true
76
90
  }
77
91
 
78
92
  await start()
@@ -144,4 +158,6 @@ async function run () {
144
158
  }
145
159
  }
146
160
 
147
- run()
161
+ if (import.meta.url === pathToFileURL(process.argv[1]).href) {
162
+ run()
163
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/watt-extra",
3
- "version": "1.13.0",
3
+ "version": "1.14.2",
4
4
  "description": "The Platformatic runtime manager",
5
5
  "type": "module",
6
6
  "scripts": {
package/plugins/init.js CHANGED
@@ -52,6 +52,15 @@ async function initPlugin (app) {
52
52
  app.instanceConfig = instanceConfig
53
53
  app.instanceId = instanceId
54
54
 
55
+ // workerMissingVersion is true only when the runtime
56
+ // already spawned without this value (ICC was unreachable at boot).
57
+ const newDeploymentVersion = instanceConfig?.deploymentVersion
58
+ const workerMissingVersion = !!newDeploymentVersion &&
59
+ process.env.PLT_DEPLOYMENT_VERSION !== newDeploymentVersion
60
+ if (newDeploymentVersion) {
61
+ process.env.PLT_DEPLOYMENT_VERSION = newDeploymentVersion
62
+ }
63
+
55
64
  // If runtime already started (ICC recovery after startup), update its instance config
56
65
  if (app.watt?.runtime) {
57
66
  await app.watt.updateInstanceConfig(instanceConfig)
@@ -60,6 +69,14 @@ async function initPlugin (app) {
60
69
  await app.setupAlerts?.()
61
70
  await app.setupHealthSignals?.()
62
71
  await app.setupFlamegraphs?.()
72
+
73
+ // The app already spawned (ICC was unreachable at boot), so its worker's env is
74
+ // frozen without the version. Push it via the shared context: the runtime
75
+ // delivers it live to the running worker (no restart).
76
+ if (workerMissingVersion) {
77
+ app.log.info({ deploymentVersion: newDeploymentVersion }, 'Publishing deployment version to the shared context after startup')
78
+ await app.watt.updateSharedContext({ deploymentVersion: newDeploymentVersion })
79
+ }
63
80
  }
64
81
  }
65
82
  try {
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 '../cli.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
@@ -374,6 +374,58 @@ test('init plugin calls updateInstanceConfig on watt when ICC becomes available
374
374
  equal(setupFlamegraphsCalled, true)
375
375
  })
376
376
 
377
+ test('init plugin sets PLT_DEPLOYMENT_VERSION and pushes it to the shared context on ICC recovery', async (t) => {
378
+ const applicationName = 'test-app-version-recovery'
379
+ const applicationId = randomUUID()
380
+
381
+ const icc = await startICC(t, {
382
+ applicationId,
383
+ controlPlaneResponse: {
384
+ applicationId,
385
+ applicationName,
386
+ deploymentVersion: 'v9',
387
+ config: {}
388
+ }
389
+ })
390
+ t.after(async () => { await icc.close() })
391
+
392
+ const originalVersion = process.env.PLT_DEPLOYMENT_VERSION
393
+ delete process.env.PLT_DEPLOYMENT_VERSION
394
+ t.after(() => {
395
+ if (originalVersion) process.env.PLT_DEPLOYMENT_VERSION = originalVersion
396
+ else delete process.env.PLT_DEPLOYMENT_VERSION
397
+ })
398
+
399
+ // Boot with ICC unavailable -> the app starts without a version.
400
+ const app = createMockApp({
401
+ PLT_APP_NAME: applicationName,
402
+ PLT_APP_DIR: '/test/dir',
403
+ PLT_ICC_URL: 'http://127.0.0.1:9999',
404
+ PLT_CONTROL_PLANE_URL: 'http://127.0.0.1:9999/control-plane'
405
+ })
406
+ await initPlugin(app)
407
+ equal(app.instanceConfig, null)
408
+ equal(process.env.PLT_DEPLOYMENT_VERSION, undefined)
409
+
410
+ // Runtime already spawned (env frozen without the version); capture pushes.
411
+ let pushedContext = null
412
+ app.watt.runtime = {}
413
+ app.watt.updateInstanceConfig = async () => {}
414
+ app.watt.updateSharedContext = async (ctx) => { pushedContext = ctx }
415
+ app.setupAlerts = async () => {}
416
+ app.setupHealthSignals = async () => {}
417
+ app.setupFlamegraphs = async () => {}
418
+
419
+ // ICC becomes available.
420
+ app.env.PLT_ICC_URL = `http://127.0.0.1:${icc.server.address().port}`
421
+ app.env.PLT_CONTROL_PLANE_URL = `http://127.0.0.1:${icc.server.address().port}/control-plane`
422
+ await app.initApplication()
423
+
424
+ // Applied to the env (for future spawns) and pushed live to the running worker.
425
+ equal(process.env.PLT_DEPLOYMENT_VERSION, 'v9')
426
+ equal(pushedContext?.deploymentVersion, 'v9')
427
+ })
428
+
377
429
  test('updateInstanceConfig calls runtime.updateMetricsConfig with merged config', async (t) => {
378
430
  const applicationName = 'test-app-metrics'
379
431
  const applicationId = randomUUID()