@platformatic/watt-extra 1.14.4 → 1.15.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli.js CHANGED
@@ -1,12 +1,12 @@
1
1
  #!/usr/bin/env node
2
2
  import commist from 'commist'
3
- import { join, dirname } from 'node:path'
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 { args, help } = applyStartArgs(argv)
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) {
@@ -162,10 +163,8 @@ class Watt {
162
163
 
163
164
  const runtime = await create(this.#appDir, null, {
164
165
  isProduction: true,
165
- transform: async (config, schema, options) => {
166
- // Forward schema and options: the runtime's transform reads isProduction
167
- // from options, and defaults watch to !isProduction when it is missing.
168
- config = await transform(config, schema, options)
166
+ transform: async (config) => {
167
+ config = await transform(config)
169
168
 
170
169
  this.#config = config
171
170
  this.#originalConfig = structuredClone(config)
@@ -188,6 +187,7 @@ class Watt {
188
187
 
189
188
  try {
190
189
  await runtime.init()
190
+ await this.#applySchedulerMode(runtime)
191
191
  } catch (e) {
192
192
  await runtime.close()
193
193
  throw e
@@ -499,8 +499,22 @@ class Watt {
499
499
  }
500
500
 
501
501
  #configureScheduler (config) {
502
- // Disable all watt schedules. We do that because
503
- // we will create/update them in ICC, not on watt in memory
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
+
504
518
  if (config.scheduler) {
505
519
  config.scheduler = config.scheduler.map((scheduler) => ({
506
520
  ...scheduler,
@@ -509,6 +523,36 @@ class Watt {
509
523
  }
510
524
  }
511
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
+
512
556
  async #configureServices (runtime) {
513
557
  if (typeof runtime.setApplicationConfigPatch !== 'function') {
514
558
  return
package/package.json CHANGED
@@ -1,13 +1,8 @@
1
1
  {
2
2
  "name": "@platformatic/watt-extra",
3
- "version": "1.14.4",
3
+ "version": "1.15.0-alpha.2",
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
+ }
@@ -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
  })
@@ -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 = config.scheduler || []
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 = { ...job, applicationId }
28
- iccJob.schedule = iccJob.cron // unfortunately, the ICC API uses `schedule` instead of `cron`
29
- delete iccJob.cron
30
- delete iccJob.enabled
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
@@ -13,12 +13,28 @@ function createWebSocketUrl (httpUrl, path, queryParams = {}) {
13
13
  return url.toString()
14
14
  }
15
15
 
16
+ async function sendCommandResult (connection, requestId, result) {
17
+ if (connection.readyState !== WebSocket.OPEN) {
18
+ throw new Error(`Cannot send command result for request "${requestId}": WebSocket is not open`)
19
+ }
20
+
21
+ await new Promise((resolve, reject) => {
22
+ connection.send(JSON.stringify({ command: 'command-result', requestId, ...result }), err => {
23
+ if (err) {
24
+ reject(err)
25
+ } else {
26
+ resolve()
27
+ }
28
+ })
29
+ })
30
+ }
31
+
16
32
  async function updatePlugin (app) {
17
33
  const reconnectInterval = app.env.PLT_UPDATES_RECONNECT_INTERVAL_SEC * 1000
18
34
 
19
35
  let socket = null
20
36
 
21
- async function processMessage (data) {
37
+ async function processMessage (connection, data) {
22
38
  try {
23
39
  const message = JSON.parse(data)
24
40
  const { topic, type, command } = message
@@ -37,6 +53,34 @@ async function updatePlugin (app) {
37
53
  return
38
54
  }
39
55
 
56
+ // Handle a centrally coordinated cron job execution: ICC dispatches
57
+ // each tick to exactly one pod of the application, this one.
58
+ if (command === 'run-scheduled-job') {
59
+ app.log.info({ command, job: message.params?.name }, 'Received run-scheduled-job command from ICC')
60
+
61
+ try {
62
+ const result = await app.runScheduledJob(message.params ?? {})
63
+ if (typeof message.requestId === 'string') {
64
+ await sendCommandResult(connection, message.requestId, { success: true, result })
65
+ }
66
+ } catch (err) {
67
+ if (typeof message.requestId !== 'string') {
68
+ throw err
69
+ }
70
+
71
+ await sendCommandResult(connection, message.requestId, {
72
+ success: false,
73
+ error: {
74
+ name: err.name,
75
+ message: err.message,
76
+ code: err.code,
77
+ statusCode: err.statusCode
78
+ }
79
+ })
80
+ }
81
+ return
82
+ }
83
+
40
84
  // Handle updates websocket format: { type: '...', topic: '...', data: {...} }
41
85
  if (!topic || !type) {
42
86
  app.log.warn({ message }, 'Received invalid message from updates websocket')
@@ -74,7 +118,7 @@ async function updatePlugin (app) {
74
118
  try {
75
119
  const headers = await app.getAuthorizationHeaders()
76
120
 
77
- socket = new WebSocket(wsUrl, { headers })
121
+ const socket = new WebSocket(wsUrl, { headers })
78
122
  await once(socket, 'open')
79
123
 
80
124
  app.log.info('Connected to updates websocket')
@@ -91,7 +135,7 @@ async function updatePlugin (app) {
91
135
  app.log.info('Received subscription acknowledgment from updates websocket')
92
136
 
93
137
  // listen for subsequent messages
94
- socket.on('message', processMessage)
138
+ socket.on('message', data => processMessage(socket, data))
95
139
 
96
140
  socket.on('error', (err) => {
97
141
  app.log.error(err, 'Error in updates websocket connection')
@@ -0,0 +1,5 @@
1
+ allowBuilds:
2
+ msgpackr-extract: true
3
+ protobufjs: true
4
+ sharp: true
5
+ unrs-resolver: true
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, resolve, dirname } from 'node:path'
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,461 @@
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 { setImmediate as immediate } from 'node:timers/promises'
8
+ import { fileURLToPath } from 'node:url'
9
+ import WebSocket from 'ws'
10
+ import { start } from '../index.js'
11
+ import Watt from '../lib/watt.js'
12
+ import schedulerPlugin from '../plugins/scheduler.js'
13
+ import updatePlugin from '../plugins/update.js'
14
+
15
+ import {
16
+ setUpEnvironment,
17
+ startICC,
18
+ installDeps
19
+ } from './helper.js'
20
+
21
+ const __filename = fileURLToPath(import.meta.url)
22
+ const __dirname = dirname(__filename)
23
+
24
+ function controlPlaneResponseWithMode (applicationId, applicationName, mode) {
25
+ return () => ({
26
+ applicationId,
27
+ applicationName,
28
+ config: {},
29
+ scheduler: { mode },
30
+ iccServices: {
31
+ cron: { url: 'http://127.0.0.1:3000/cron' },
32
+ compliance: { url: 'http://127.0.0.1:3000/compliance' },
33
+ scaler: { url: 'http://127.0.0.1:3000/scaler' }
34
+ }
35
+ })
36
+ }
37
+
38
+ test('external mode: should disable local jobs, skip legacy registration and report the jobs in the state', async (t) => {
39
+ const applicationName = 'test-app'
40
+ const applicationId = randomUUID()
41
+ const applicationPath = join(__dirname, 'fixtures', 'runtime-scheduler')
42
+
43
+ await installDeps(t, applicationPath)
44
+
45
+ let savedWattJob = null
46
+ let savedState = null
47
+ const icc = await startICC(t, {
48
+ applicationId,
49
+ applicationName,
50
+ controlPlaneResponse: controlPlaneResponseWithMode(applicationId, applicationName, 'external'),
51
+ saveWattJob: (job) => {
52
+ savedWattJob = job
53
+ },
54
+ saveApplicationInstanceState: ({ state }) => {
55
+ savedState = state
56
+ }
57
+ })
58
+
59
+ setUpEnvironment({
60
+ PLT_APP_NAME: applicationName,
61
+ PLT_APP_DIR: applicationPath,
62
+ PLT_ICC_URL: 'http://127.0.0.1:3000'
63
+ })
64
+ const app = await start()
65
+
66
+ t.after(async () => {
67
+ await app.close()
68
+ await icc.close()
69
+ })
70
+
71
+ // The local scheduler triggers are disabled on released runtimes and paused
72
+ // on runtimes exposing scheduler control.
73
+ const config = await app.watt.runtime.getRuntimeConfig()
74
+ if (typeof app.watt.runtime.pauseSchedulerJob === 'function') {
75
+ assert.notStrictEqual(config.scheduler[0].enabled, false)
76
+ assert.strictEqual(app.watt.runtime.getScheduler()[0].paused, true)
77
+ } else {
78
+ assert.strictEqual(config.scheduler[0].enabled, false)
79
+ }
80
+
81
+ // The legacy per-job registration is skipped: the control-plane
82
+ // registers the jobs centrally from the state report
83
+ assert.strictEqual(savedWattJob, null)
84
+
85
+ // The jobs are reported in the application state
86
+ assert.ok(savedState, 'the state should have been reported')
87
+ assert.ok(Array.isArray(savedState.scheduler), 'the state should include the scheduler jobs')
88
+ assert.strictEqual(savedState.scheduler.length, 1)
89
+ assert.strictEqual(savedState.scheduler[0].name, 'test')
90
+ assert.strictEqual(savedState.scheduler[0].cron, '*/5 * * * *')
91
+ assert.strictEqual(savedState.scheduler[0].callbackUrl, 'http://localhost:3000')
92
+ assert.strictEqual(savedState.scheduler[0].source, 'config')
93
+ })
94
+
95
+ test('local mode: should keep the local jobs running and skip any registration', async (t) => {
96
+ const applicationName = 'test-app'
97
+ const applicationId = randomUUID()
98
+ const applicationPath = join(__dirname, 'fixtures', 'runtime-scheduler')
99
+
100
+ await installDeps(t, applicationPath)
101
+
102
+ let savedWattJob = null
103
+ const icc = await startICC(t, {
104
+ applicationId,
105
+ applicationName,
106
+ controlPlaneResponse: controlPlaneResponseWithMode(applicationId, applicationName, 'local'),
107
+ saveWattJob: (job) => {
108
+ savedWattJob = job
109
+ }
110
+ })
111
+
112
+ setUpEnvironment({
113
+ PLT_APP_NAME: applicationName,
114
+ PLT_APP_DIR: applicationPath,
115
+ PLT_ICC_URL: 'http://127.0.0.1:3000'
116
+ })
117
+ const app = await start()
118
+
119
+ t.after(async () => {
120
+ await app.close()
121
+ await icc.close()
122
+ })
123
+
124
+ // ICC coordination is disabled: the local scheduler stays enabled
125
+ const config = await app.watt.runtime.getRuntimeConfig()
126
+ assert.notStrictEqual(config.scheduler[0].enabled, false)
127
+ if (typeof app.watt.runtime.getScheduler === 'function') {
128
+ assert.strictEqual(app.watt.runtime.getScheduler()[0].paused, false)
129
+ }
130
+
131
+ // No registration happens: the jobs run locally in each pod
132
+ assert.strictEqual(savedWattJob, null)
133
+ })
134
+
135
+ function createMockApp (port) {
136
+ return {
137
+ log: { info: () => {}, error: () => {}, warn: () => {}, debug: () => {} },
138
+ instanceConfig: {
139
+ applicationId: 'test-application-id',
140
+ scheduler: { mode: 'external' }
141
+ },
142
+ instanceId: 'test-pod-123',
143
+ getRuntimeId: () => 'test-runtime-id',
144
+ getAuthorizationHeaders: async () => ({ Authorization: 'Bearer test-token' }),
145
+ env: {
146
+ PLT_ICC_URL: `http://localhost:${port}`,
147
+ PLT_UPDATES_RECONNECT_INTERVAL_SEC: 1
148
+ },
149
+ watt: {
150
+ runtime: {},
151
+ applySchedulerMode: async () => {}
152
+ }
153
+ }
154
+ }
155
+
156
+ test('runtime scheduler ownership follows the ICC mode', async () => {
157
+ const instanceConfig = { scheduler: { mode: 'external' } }
158
+ const jobs = [
159
+ { name: 'configured', paused: false },
160
+ { name: 'frontend:0', paused: false }
161
+ ]
162
+ const watt = new Watt({
163
+ env: {
164
+ PLT_APP_DIR: join(__dirname, 'fixtures', 'runtime-scheduler'),
165
+ PLT_ICC_URL: 'http://localhost:3000'
166
+ },
167
+ log: { info: () => {} },
168
+ instanceConfig,
169
+ instanceId: 'test-pod-123'
170
+ })
171
+
172
+ watt.runtime = {
173
+ getScheduler: () => jobs,
174
+ pauseSchedulerJob: async name => {
175
+ jobs.find(job => job.name === name).paused = true
176
+ },
177
+ resumeSchedulerJob: async name => {
178
+ jobs.find(job => job.name === name).paused = false
179
+ }
180
+ }
181
+
182
+ await watt.applySchedulerMode()
183
+ assert.deepStrictEqual(jobs.map(job => job.paused), [true, true])
184
+
185
+ instanceConfig.scheduler.mode = 'local'
186
+ await watt.applySchedulerMode()
187
+ assert.deepStrictEqual(jobs.map(job => job.paused), [false, false])
188
+ })
189
+
190
+ function setupMockIccServer (wss) {
191
+ let ws = null
192
+ const clientMessages = []
193
+ const messageWaiters = []
194
+
195
+ const waitForClientSubscription = once(wss, 'connection').then(([socket]) => {
196
+ ws = socket
197
+
198
+ return new Promise((resolve) => {
199
+ socket.on('message', (data) => {
200
+ const message = JSON.parse(data.toString())
201
+
202
+ if (message.command === 'subscribe' && message.topic === '/config') {
203
+ socket.send(JSON.stringify({ command: 'ack' }))
204
+ resolve()
205
+ return
206
+ }
207
+
208
+ const waiter = messageWaiters.shift()
209
+ if (waiter) {
210
+ waiter(message)
211
+ } else {
212
+ clientMessages.push(message)
213
+ }
214
+ })
215
+ })
216
+ })
217
+
218
+ function waitForClientMessage () {
219
+ const message = clientMessages.shift()
220
+ if (message) {
221
+ return Promise.resolve(message)
222
+ }
223
+
224
+ return new Promise(resolve => messageWaiters.push(resolve))
225
+ }
226
+
227
+ return { waitForClientSubscription, waitForClientMessage, getClientMessages: () => clientMessages, getWs: () => ws }
228
+ }
229
+
230
+ test('run-scheduled-job: should execute the job callback when triggered over the websocket', async (t) => {
231
+ setUpEnvironment()
232
+ const port = 14100
233
+
234
+ const wss = new WebSocket.Server({ port })
235
+ t.after(() => wss.close())
236
+
237
+ const { waitForClientSubscription, waitForClientMessage, getWs } = setupMockIccServer(wss)
238
+
239
+ // The target the job callback should hit
240
+ const receivedRequests = []
241
+ let requestResolve
242
+ const requestReceived = new Promise((resolve) => { requestResolve = resolve })
243
+ const target = createServer((req, res) => {
244
+ receivedRequests.push({ method: req.method, url: req.url })
245
+ res.end('{"ok":true}')
246
+ requestResolve()
247
+ })
248
+ await new Promise((resolve) => target.listen(0, resolve))
249
+ t.after(() => target.close())
250
+
251
+ const app = createMockApp(port)
252
+ await schedulerPlugin(app)
253
+ await updatePlugin(app)
254
+ await app.connectToUpdates()
255
+ t.after(() => app.closeUpdates())
256
+ await waitForClientSubscription
257
+
258
+ getWs().send(JSON.stringify({
259
+ command: 'run-scheduled-job',
260
+ requestId: 'request-1',
261
+ params: {
262
+ name: 'cleanup',
263
+ callbackUrl: `http://127.0.0.1:${target.address().port}/cleanup`,
264
+ method: 'POST',
265
+ source: 'config'
266
+ }
267
+ }))
268
+
269
+ await requestReceived
270
+ const completion = await waitForClientMessage()
271
+
272
+ assert.deepStrictEqual(receivedRequests, [{ method: 'POST', url: '/cleanup' }])
273
+ assert.deepStrictEqual(completion, {
274
+ command: 'command-result',
275
+ requestId: 'request-1',
276
+ success: true,
277
+ result: { name: 'cleanup', statusCode: 200 }
278
+ })
279
+ })
280
+
281
+ test('run-scheduled-job: should report runtime failures over the websocket', async (t) => {
282
+ setUpEnvironment()
283
+
284
+ const wss = new WebSocket.Server({ port: 0 })
285
+ await once(wss, 'listening')
286
+ t.after(() => wss.close())
287
+
288
+ const { waitForClientSubscription, waitForClientMessage, getWs } = setupMockIccServer(wss)
289
+ const app = createMockApp(wss.address().port)
290
+ const error = new Error('Task execution failed')
291
+ error.code = 'PLT_TASK_FAILED'
292
+ error.statusCode = 500
293
+ app.watt.runtime.runSchedulerJob = async () => { throw error }
294
+
295
+ await schedulerPlugin(app)
296
+ await updatePlugin(app)
297
+ await app.connectToUpdates()
298
+ t.after(() => app.closeUpdates())
299
+ await waitForClientSubscription
300
+
301
+ getWs().send(JSON.stringify({
302
+ command: 'run-scheduled-job',
303
+ requestId: 'request-2',
304
+ params: { name: 'frontend:0', source: 'application' }
305
+ }))
306
+
307
+ assert.deepStrictEqual(await waitForClientMessage(), {
308
+ command: 'command-result',
309
+ requestId: 'request-2',
310
+ success: false,
311
+ error: {
312
+ name: 'Error',
313
+ message: 'Task execution failed',
314
+ code: 'PLT_TASK_FAILED',
315
+ statusCode: 500
316
+ }
317
+ })
318
+ })
319
+
320
+ test('run-scheduled-job: should not report completion without a request ID', async (t) => {
321
+ setUpEnvironment()
322
+
323
+ const wss = new WebSocket.Server({ port: 0 })
324
+ await once(wss, 'listening')
325
+ t.after(() => wss.close())
326
+
327
+ const { waitForClientSubscription, getClientMessages, getWs } = setupMockIccServer(wss)
328
+ const app = createMockApp(wss.address().port)
329
+ let executedResolve
330
+ const executed = new Promise(resolve => { executedResolve = resolve })
331
+ app.watt.runtime.runSchedulerJob = async () => {
332
+ executedResolve()
333
+ return { success: true }
334
+ }
335
+
336
+ await schedulerPlugin(app)
337
+ await updatePlugin(app)
338
+ await app.connectToUpdates()
339
+ t.after(() => app.closeUpdates())
340
+ await waitForClientSubscription
341
+
342
+ getWs().send(JSON.stringify({
343
+ command: 'run-scheduled-job',
344
+ params: { name: 'frontend:0', source: 'application' }
345
+ }))
346
+
347
+ await executed
348
+ await immediate()
349
+ assert.deepStrictEqual(getClientMessages(), [])
350
+ })
351
+
352
+ test('run-scheduled-job: should route legacy internal callbacks through the runtime', async (t) => {
353
+ const app = createMockApp(0)
354
+
355
+ const injected = []
356
+ app.watt.runtime.inject = async (applicationId, params) => {
357
+ injected.push({ applicationId, ...params })
358
+ return { statusCode: 200 }
359
+ }
360
+
361
+ await schedulerPlugin(app)
362
+
363
+ const result = await app.runScheduledJob({
364
+ name: 'cleanup',
365
+ callbackUrl: 'http://frontend.plt.local/cleanup',
366
+ method: 'POST',
367
+ source: 'config',
368
+ body: { scheduledTime: 1 }
369
+ })
370
+
371
+ assert.deepStrictEqual(result, { name: 'cleanup', statusCode: 200 })
372
+ assert.strictEqual(injected.length, 1)
373
+ assert.strictEqual(injected[0].applicationId, 'frontend')
374
+ assert.strictEqual(injected[0].method, 'POST')
375
+ assert.strictEqual(injected[0].url, '/cleanup')
376
+ assert.strictEqual(injected[0].body, '{"scheduledTime":1}')
377
+ })
378
+
379
+ test('run-scheduled-job: should use native runtime execution for application jobs', async (t) => {
380
+ const app = createMockApp(0)
381
+
382
+ const executed = []
383
+ app.watt.runtime.runSchedulerJob = async (name) => {
384
+ executed.push(name)
385
+ return { name, success: true }
386
+ }
387
+
388
+ await schedulerPlugin(app)
389
+
390
+ const result = await app.runScheduledJob({ name: 'frontend:0', source: 'application' })
391
+
392
+ assert.deepStrictEqual(executed, ['frontend:0'])
393
+ assert.deepStrictEqual(result, { name: 'frontend:0', success: true })
394
+ })
395
+
396
+ test('run-scheduled-job: should fail on non-2xx callback responses', async (t) => {
397
+ const app = createMockApp(0)
398
+
399
+ app.watt.runtime.inject = async () => ({ statusCode: 500 })
400
+
401
+ await schedulerPlugin(app)
402
+
403
+ await assert.rejects(
404
+ app.runScheduledJob({
405
+ name: 'failing',
406
+ callbackUrl: 'http://frontend.plt.local/failing',
407
+ source: 'config'
408
+ }),
409
+ /Scheduled job "failing" failed with HTTP 500/
410
+ )
411
+ })
412
+
413
+ test('collectSchedulerJobs: should use the runtime scheduler status when available', async (t) => {
414
+ const app = createMockApp(0)
415
+
416
+ app.watt.runtime.getScheduler = async () => ([
417
+ {
418
+ name: 'cleanup',
419
+ cron: '0 * * * *',
420
+ callbackUrl: 'http://service.plt.local/cleanup',
421
+ method: 'POST',
422
+ source: 'config',
423
+ paused: false
424
+ },
425
+ {
426
+ name: 'frontend:0',
427
+ cron: '* * * * *',
428
+ source: 'application',
429
+ applicationId: 'frontend',
430
+ scheduleId: '0',
431
+ tasks: ['db:cleanup']
432
+ }
433
+ ])
434
+
435
+ await schedulerPlugin(app)
436
+
437
+ const jobs = await app.collectSchedulerJobs()
438
+
439
+ assert.strictEqual(jobs.length, 2)
440
+ assert.strictEqual(jobs[0].name, 'cleanup')
441
+ assert.strictEqual(jobs[0].source, 'config')
442
+ assert.strictEqual(jobs[1].name, 'frontend:0')
443
+ assert.strictEqual(jobs[1].source, 'application')
444
+ assert.strictEqual(jobs[1].applicationId, 'frontend')
445
+ assert.strictEqual(jobs[1].scheduleId, '0')
446
+ assert.deepStrictEqual(jobs[1].tasks, ['db:cleanup'])
447
+ })
448
+
449
+ test('external mode: should apply runtime scheduler ownership', async (t) => {
450
+ const app = createMockApp(0)
451
+
452
+ let applications = 0
453
+ app.watt.applySchedulerMode = async () => {
454
+ applications++
455
+ }
456
+
457
+ await schedulerPlugin(app)
458
+ await app.sendSchedulerInfo()
459
+
460
+ assert.strictEqual(applications, 1)
461
+ })
@@ -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
- }
@@ -1,54 +0,0 @@
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
- })