@platformatic/nitro 3.63.0 → 3.64.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/lib/capability.js +91 -0
- package/lib/scheduled-tasks.js +50 -0
- package/lib/scheduler/module.d.ts +6 -0
- package/lib/scheduler/module.js +38 -0
- package/lib/scheduler/runtime/runtime-nitro.mjs +55 -0
- package/lib/scheduler/runtime/runtime.mjs +52 -0
- package/package.json +20 -7
- package/schema.json +1 -1
package/lib/capability.js
CHANGED
|
@@ -18,6 +18,7 @@ import { readFile } from 'node:fs/promises'
|
|
|
18
18
|
import { dirname, resolve } from 'node:path'
|
|
19
19
|
import { satisfies } from 'semver'
|
|
20
20
|
import { version } from './schema.js'
|
|
21
|
+
import { readSchedulerManifest } from './scheduled-tasks.js'
|
|
21
22
|
|
|
22
23
|
export const supportedVersions = {
|
|
23
24
|
nitro: '>=3.0.0-alpha.0 <4.0.0',
|
|
@@ -93,6 +94,9 @@ export class NitroCapability extends BaseCapability {
|
|
|
93
94
|
#basePath
|
|
94
95
|
#server
|
|
95
96
|
#dispatcher
|
|
97
|
+
#scheduledTasks
|
|
98
|
+
#schedulerManifest
|
|
99
|
+
#scheduledTasksRunner
|
|
96
100
|
|
|
97
101
|
constructor (root, config, context) {
|
|
98
102
|
super('nitro', version, root, config, context)
|
|
@@ -124,6 +128,48 @@ export class NitroCapability extends BaseCapability {
|
|
|
124
128
|
this.registerGlobals({ basePath: this.#basePath })
|
|
125
129
|
}
|
|
126
130
|
|
|
131
|
+
async getScheduledTasks () {
|
|
132
|
+
if (!this.isProduction) {
|
|
133
|
+
return this.#scheduledTasks ?? []
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
this.#schedulerManifest ??= readSchedulerManifest(
|
|
137
|
+
resolve(this.root, this.config.nitro?.outputDirectory ?? this.config.application.outputDirectory ?? '.output')
|
|
138
|
+
)
|
|
139
|
+
this.#scheduledTasks ??= await this.#schedulerManifest
|
|
140
|
+
return this.#scheduledTasks
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async runScheduledTasks (scheduleId, scheduledTime) {
|
|
144
|
+
if (this.childManager) {
|
|
145
|
+
if (!this.clientWs) {
|
|
146
|
+
throw new Error('The application has not started yet')
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return this.childManager.send(this.clientWs, 'platformatic:nitro:run-scheduled-tasks', {
|
|
150
|
+
scheduleId,
|
|
151
|
+
scheduledTime
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (!this.#scheduledTasksRunner) {
|
|
156
|
+
throw new Error('The application does not use the @platformatic/nitro/scheduler module')
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return this.#scheduledTasksRunner({ scheduleId, scheduledTime })
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
setScheduledTasksRunner (runner) {
|
|
163
|
+
this.#scheduledTasksRunner = runner
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
setupChildManagerEventsForwarding (childManager) {
|
|
167
|
+
super.setupChildManagerEventsForwarding(childManager)
|
|
168
|
+
childManager.on('platformatic:nitro:scheduled-tasks', scheduledTasks => {
|
|
169
|
+
this.#scheduledTasks = scheduledTasks
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
|
|
127
173
|
async start ({ listen }) {
|
|
128
174
|
/* c8 ignore next 3 */
|
|
129
175
|
if (this.url) {
|
|
@@ -370,6 +416,9 @@ export class NitroViteCapability extends ViteCapability {
|
|
|
370
416
|
#basePath
|
|
371
417
|
#server
|
|
372
418
|
#dispatcher
|
|
419
|
+
#scheduledTasks
|
|
420
|
+
#schedulerManifest
|
|
421
|
+
#scheduledTasksRunner
|
|
373
422
|
|
|
374
423
|
constructor (root, config, context) {
|
|
375
424
|
super(root, config, context)
|
|
@@ -472,6 +521,48 @@ export class NitroViteCapability extends ViteCapability {
|
|
|
472
521
|
}
|
|
473
522
|
}
|
|
474
523
|
|
|
524
|
+
async getScheduledTasks () {
|
|
525
|
+
if (!this.isProduction) {
|
|
526
|
+
return this.#scheduledTasks ?? []
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
this.#schedulerManifest ??= readSchedulerManifest(
|
|
530
|
+
resolve(this.root, this.config.nitro?.outputDirectory ?? this.config.application.outputDirectory ?? '.output')
|
|
531
|
+
)
|
|
532
|
+
this.#scheduledTasks ??= await this.#schedulerManifest
|
|
533
|
+
return this.#scheduledTasks
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
async runScheduledTasks (scheduleId, scheduledTime) {
|
|
537
|
+
if (this.childManager) {
|
|
538
|
+
if (!this.clientWs) {
|
|
539
|
+
throw new Error('The application has not started yet')
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
return this.childManager.send(this.clientWs, 'platformatic:nitro:run-scheduled-tasks', {
|
|
543
|
+
scheduleId,
|
|
544
|
+
scheduledTime
|
|
545
|
+
})
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
if (!this.#scheduledTasksRunner) {
|
|
549
|
+
throw new Error('The application does not use the @platformatic/nitro/scheduler module')
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
return this.#scheduledTasksRunner({ scheduleId, scheduledTime })
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
setScheduledTasksRunner (runner) {
|
|
556
|
+
this.#scheduledTasksRunner = runner
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
setupChildManagerEventsForwarding (childManager) {
|
|
560
|
+
super.setupChildManagerEventsForwarding(childManager)
|
|
561
|
+
childManager.on('platformatic:nitro:scheduled-tasks', scheduledTasks => {
|
|
562
|
+
this.#scheduledTasks = scheduledTasks
|
|
563
|
+
})
|
|
564
|
+
}
|
|
565
|
+
|
|
475
566
|
async _startProduction () {
|
|
476
567
|
const command = this.config.application.commands.production
|
|
477
568
|
if (command) {
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
export const SCHEDULER_MANIFEST_FILENAME = 'platformatic-scheduler.json'
|
|
5
|
+
export const SCHEDULER_MANIFEST_VERSION = 1
|
|
6
|
+
|
|
7
|
+
export class UnsupportedSchedulerManifestVersionError extends Error {
|
|
8
|
+
constructor (version) {
|
|
9
|
+
super(`Unsupported Nitro scheduler manifest version "${version}"`)
|
|
10
|
+
this.code = 'PLT_NITRO_UNSUPPORTED_SCHEDULER_MANIFEST_VERSION'
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function normalizeScheduledTasks (scheduledTasks) {
|
|
15
|
+
let schedules = []
|
|
16
|
+
|
|
17
|
+
if (Array.isArray(scheduledTasks)) {
|
|
18
|
+
schedules = scheduledTasks
|
|
19
|
+
} else if (scheduledTasks && typeof scheduledTasks === 'object') {
|
|
20
|
+
schedules = Object.entries(scheduledTasks).map(([cron, tasks]) => ({ cron, tasks }))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return schedules.map(({ cron, tasks }, index) => ({
|
|
24
|
+
id: String(index),
|
|
25
|
+
cron,
|
|
26
|
+
tasks: Array.isArray(tasks) ? tasks : [tasks]
|
|
27
|
+
}))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function readSchedulerManifest (outputDirectory) {
|
|
31
|
+
let manifest
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
manifest = JSON.parse(
|
|
35
|
+
await readFile(join(outputDirectory, 'server', SCHEDULER_MANIFEST_FILENAME), 'utf8')
|
|
36
|
+
)
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (error.code === 'ENOENT') {
|
|
39
|
+
return []
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
throw error
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (manifest.version !== SCHEDULER_MANIFEST_VERSION || !Array.isArray(manifest.scheduledTasks)) {
|
|
46
|
+
throw new UnsupportedSchedulerManifestVersionError(manifest.version)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return manifest.scheduledTasks
|
|
50
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { writeFile } from 'node:fs/promises'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { fileURLToPath } from 'node:url'
|
|
4
|
+
import {
|
|
5
|
+
normalizeScheduledTasks,
|
|
6
|
+
SCHEDULER_MANIFEST_FILENAME,
|
|
7
|
+
SCHEDULER_MANIFEST_VERSION
|
|
8
|
+
} from '../scheduled-tasks.js'
|
|
9
|
+
|
|
10
|
+
const runtimeDirectory = fileURLToPath(new URL('./runtime', import.meta.url))
|
|
11
|
+
|
|
12
|
+
export { SCHEDULER_MANIFEST_FILENAME }
|
|
13
|
+
|
|
14
|
+
export default function schedulerModule (nitro) {
|
|
15
|
+
const scheduledTasks = normalizeScheduledTasks(nitro.options.scheduledTasks)
|
|
16
|
+
|
|
17
|
+
nitro.options.scheduledTasks = {}
|
|
18
|
+
nitro.options.experimental ??= {}
|
|
19
|
+
nitro.options.experimental.tasks = true
|
|
20
|
+
|
|
21
|
+
nitro.options.runtimeConfig ??= {}
|
|
22
|
+
nitro.options.runtimeConfig.platformaticScheduler = { scheduledTasks }
|
|
23
|
+
|
|
24
|
+
nitro.options.plugins ??= []
|
|
25
|
+
const runtimePlugin = nitro.meta?.majorVersion >= 3 ? 'runtime-nitro.mjs' : 'runtime.mjs'
|
|
26
|
+
nitro.options.plugins.push(join(runtimeDirectory, runtimePlugin).replaceAll('\\', '/'))
|
|
27
|
+
|
|
28
|
+
nitro.hooks.hook('compiled', async () => {
|
|
29
|
+
if (nitro.options.dev || !nitro.options.output?.serverDir) {
|
|
30
|
+
return
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
await writeFile(
|
|
34
|
+
join(nitro.options.output.serverDir, SCHEDULER_MANIFEST_FILENAME),
|
|
35
|
+
JSON.stringify({ version: SCHEDULER_MANIFEST_VERSION, scheduledTasks }, null, 2)
|
|
36
|
+
)
|
|
37
|
+
})
|
|
38
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { getCapability, getITC } from '@platformatic/globals'
|
|
2
|
+
import { definePlugin } from 'nitro'
|
|
3
|
+
import { useNitroApp } from 'nitro/app'
|
|
4
|
+
import { runTask } from 'nitro/task'
|
|
5
|
+
import { useRuntimeConfig } from 'nitro/runtime-config'
|
|
6
|
+
|
|
7
|
+
const RUN_HANDLER = 'platformatic:nitro:run-scheduled-tasks'
|
|
8
|
+
const METADATA_NOTIFICATION = 'platformatic:nitro:scheduled-tasks'
|
|
9
|
+
|
|
10
|
+
export default definePlugin(() => {
|
|
11
|
+
const scheduledTasks = useRuntimeConfig().platformaticScheduler?.scheduledTasks ?? []
|
|
12
|
+
|
|
13
|
+
async function runScheduledTasks ({ scheduleId, scheduledTime }) {
|
|
14
|
+
const schedule = scheduledTasks.find(schedule => schedule.id === scheduleId)
|
|
15
|
+
|
|
16
|
+
if (!schedule) {
|
|
17
|
+
throw new Error(`Scheduled task group "${scheduleId}" not found`)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const results = await Promise.allSettled(
|
|
21
|
+
schedule.tasks.map(async name => {
|
|
22
|
+
const { result } = await runTask(name, {
|
|
23
|
+
payload: { scheduledTime },
|
|
24
|
+
context: {}
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
return { name, result: result ?? null }
|
|
28
|
+
})
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
const errors = results.filter(result => result.status === 'rejected').map(result => result.reason)
|
|
32
|
+
if (errors.length > 0) {
|
|
33
|
+
throw new AggregateError(errors, `Scheduled task group "${scheduleId}" failed`)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return results.map(result => result.value)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const itc = getITC({ throwOnMissing: false })
|
|
40
|
+
|
|
41
|
+
if (itc && process.env.PLT_MANAGER_ID) {
|
|
42
|
+
itc.handle(RUN_HANDLER, runScheduledTasks)
|
|
43
|
+
itc.notify(METADATA_NOTIFICATION, scheduledTasks)
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const capability = getCapability({ throwOnMissing: false })
|
|
48
|
+
|
|
49
|
+
if (!capability) {
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
capability.setScheduledTasksRunner(runScheduledTasks)
|
|
54
|
+
useNitroApp().hooks.hook('close', () => capability.setScheduledTasksRunner(null))
|
|
55
|
+
})
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { getCapability, getITC } from '@platformatic/globals'
|
|
2
|
+
import { defineNitroPlugin, runTask, useRuntimeConfig } from 'nitropack/runtime'
|
|
3
|
+
|
|
4
|
+
const RUN_HANDLER = 'platformatic:nitro:run-scheduled-tasks'
|
|
5
|
+
const METADATA_NOTIFICATION = 'platformatic:nitro:scheduled-tasks'
|
|
6
|
+
|
|
7
|
+
export default defineNitroPlugin(nitroApp => {
|
|
8
|
+
const scheduledTasks = useRuntimeConfig().platformaticScheduler?.scheduledTasks ?? []
|
|
9
|
+
|
|
10
|
+
async function runScheduledTasks ({ scheduleId, scheduledTime }) {
|
|
11
|
+
const schedule = scheduledTasks.find(schedule => schedule.id === scheduleId)
|
|
12
|
+
|
|
13
|
+
if (!schedule) {
|
|
14
|
+
throw new Error(`Scheduled task group "${scheduleId}" not found`)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const results = await Promise.allSettled(
|
|
18
|
+
schedule.tasks.map(async name => {
|
|
19
|
+
const { result } = await runTask(name, {
|
|
20
|
+
payload: { scheduledTime },
|
|
21
|
+
context: {}
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
return { name, result: result ?? null }
|
|
25
|
+
})
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
const errors = results.filter(result => result.status === 'rejected').map(result => result.reason)
|
|
29
|
+
if (errors.length > 0) {
|
|
30
|
+
throw new AggregateError(errors, `Scheduled task group "${scheduleId}" failed`)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return results.map(result => result.value)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const itc = getITC({ throwOnMissing: false })
|
|
37
|
+
|
|
38
|
+
if (itc && process.env.PLT_MANAGER_ID) {
|
|
39
|
+
itc.handle(RUN_HANDLER, runScheduledTasks)
|
|
40
|
+
itc.notify(METADATA_NOTIFICATION, scheduledTasks)
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const capability = getCapability({ throwOnMissing: false })
|
|
45
|
+
|
|
46
|
+
if (!capability) {
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
capability.setScheduledTasksRunner(runScheduledTasks)
|
|
51
|
+
nitroApp.hooks.hook('close', () => capability.setScheduledTasksRunner(null))
|
|
52
|
+
})
|
package/package.json
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/nitro",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.64.0",
|
|
4
4
|
"description": "Platformatic Nitro Capability",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
7
|
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"import": "./index.js",
|
|
12
|
+
"default": "./index.js"
|
|
13
|
+
},
|
|
14
|
+
"./scheduler": {
|
|
15
|
+
"types": "./lib/scheduler/module.d.ts",
|
|
16
|
+
"import": "./lib/scheduler/module.js",
|
|
17
|
+
"default": "./lib/scheduler/module.js"
|
|
18
|
+
},
|
|
19
|
+
"./*": "./*"
|
|
20
|
+
},
|
|
8
21
|
"repository": {
|
|
9
22
|
"type": "git",
|
|
10
23
|
"url": "git+https://github.com/platformatic/platformatic.git"
|
|
@@ -18,10 +31,10 @@
|
|
|
18
31
|
"dependencies": {
|
|
19
32
|
"light-my-request": "^6.6.0",
|
|
20
33
|
"semver": "^7.6.3",
|
|
21
|
-
"@platformatic/basic": "3.
|
|
22
|
-
"@platformatic/foundation": "3.
|
|
23
|
-
"@platformatic/
|
|
24
|
-
"@platformatic/
|
|
34
|
+
"@platformatic/basic": "3.64.0",
|
|
35
|
+
"@platformatic/foundation": "3.64.0",
|
|
36
|
+
"@platformatic/vite": "3.64.0",
|
|
37
|
+
"@platformatic/globals": "3.64.0"
|
|
25
38
|
},
|
|
26
39
|
"devDependencies": {
|
|
27
40
|
"cleaner-spec-reporter": "^0.5.0",
|
|
@@ -34,8 +47,8 @@
|
|
|
34
47
|
"typescript": "^5.9.3",
|
|
35
48
|
"vite": "^7.2.4",
|
|
36
49
|
"ws": "^8.18.0",
|
|
37
|
-
"@platformatic/gateway": "3.
|
|
38
|
-
"@platformatic/service": "3.
|
|
50
|
+
"@platformatic/gateway": "3.64.0",
|
|
51
|
+
"@platformatic/service": "3.64.0"
|
|
39
52
|
},
|
|
40
53
|
"engines": {
|
|
41
54
|
"node": ">=22.19.0"
|
package/schema.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"$id": "https://schemas.platformatic.dev/@platformatic/nitro/3.
|
|
2
|
+
"$id": "https://schemas.platformatic.dev/@platformatic/nitro/3.64.0.json",
|
|
3
3
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
4
4
|
"title": "Platformatic Nitro Config",
|
|
5
5
|
"type": "object",
|