@vox-ai-app/scheduler 1.0.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/LICENSE +21 -0
- package/package.json +30 -0
- package/src/cron.js +87 -0
- package/src/index.js +10 -0
- package/src/store.js +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Arnav Gupta
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vox-ai-app/scheduler",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "Cron-based job scheduler for Vox — schedule recurring agent runs, heartbeats, and timed tasks",
|
|
7
|
+
"main": "./src/index.js",
|
|
8
|
+
"private": false,
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./src/index.js",
|
|
11
|
+
"./cron": "./src/cron.js",
|
|
12
|
+
"./store": "./src/store.js"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public",
|
|
16
|
+
"registry": "https://registry.npmjs.org"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/vox-ai-app/vox"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"src",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"croner": "^9.0.0"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/cron.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Cron } from 'croner'
|
|
2
|
+
|
|
3
|
+
const jobs = new Map()
|
|
4
|
+
|
|
5
|
+
function resolveTimezone(tz) {
|
|
6
|
+
if (tz && typeof tz === 'string' && tz.trim()) return tz.trim()
|
|
7
|
+
try { return Intl.DateTimeFormat().resolvedOptions().timeZone } catch { return 'UTC' }
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function scheduleJob(id, config, handler) {
|
|
11
|
+
if (jobs.has(id)) cancelJob(id)
|
|
12
|
+
|
|
13
|
+
const { expr, tz, runImmediately } = config
|
|
14
|
+
const timezone = resolveTimezone(tz)
|
|
15
|
+
|
|
16
|
+
const cron = new Cron(expr, {
|
|
17
|
+
timezone,
|
|
18
|
+
catch: (err) => {
|
|
19
|
+
if (config.onError) config.onError(err)
|
|
20
|
+
}
|
|
21
|
+
}, () => {
|
|
22
|
+
handler(id, { scheduledAt: Date.now(), expr, timezone })
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
const job = {
|
|
26
|
+
id,
|
|
27
|
+
cron,
|
|
28
|
+
expr,
|
|
29
|
+
timezone,
|
|
30
|
+
createdAt: Date.now(),
|
|
31
|
+
handler
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
jobs.set(id, job)
|
|
35
|
+
|
|
36
|
+
if (runImmediately) {
|
|
37
|
+
handler(id, { scheduledAt: Date.now(), expr, timezone, immediate: true })
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return job
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function cancelJob(id) {
|
|
44
|
+
const job = jobs.get(id)
|
|
45
|
+
if (!job) return false
|
|
46
|
+
job.cron.stop()
|
|
47
|
+
jobs.delete(id)
|
|
48
|
+
return true
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function cancelAllJobs() {
|
|
52
|
+
for (const [id, job] of jobs) {
|
|
53
|
+
job.cron.stop()
|
|
54
|
+
}
|
|
55
|
+
jobs.clear()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function getJob(id) {
|
|
59
|
+
const job = jobs.get(id)
|
|
60
|
+
if (!job) return null
|
|
61
|
+
return {
|
|
62
|
+
id: job.id,
|
|
63
|
+
expr: job.expr,
|
|
64
|
+
timezone: job.timezone,
|
|
65
|
+
createdAt: job.createdAt,
|
|
66
|
+
nextRun: job.cron.nextRun()?.getTime() || null,
|
|
67
|
+
running: job.cron.isBusy()
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function listJobs() {
|
|
72
|
+
return Array.from(jobs.values()).map(job => ({
|
|
73
|
+
id: job.id,
|
|
74
|
+
expr: job.expr,
|
|
75
|
+
timezone: job.timezone,
|
|
76
|
+
createdAt: job.createdAt,
|
|
77
|
+
nextRun: job.cron.nextRun()?.getTime() || null,
|
|
78
|
+
running: job.cron.isBusy()
|
|
79
|
+
}))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function computeNextRun(expr, tz) {
|
|
83
|
+
const timezone = resolveTimezone(tz)
|
|
84
|
+
const cron = new Cron(expr, { timezone })
|
|
85
|
+
const next = cron.nextRun()
|
|
86
|
+
return next ? next.getTime() : null
|
|
87
|
+
}
|
package/src/index.js
ADDED
package/src/store.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
const SCHEDULES_FILE = 'schedules.json'
|
|
5
|
+
|
|
6
|
+
export function createStore(dataDir) {
|
|
7
|
+
const filePath = path.join(dataDir, SCHEDULES_FILE)
|
|
8
|
+
|
|
9
|
+
function readAll() {
|
|
10
|
+
try {
|
|
11
|
+
const raw = fs.readFileSync(filePath, 'utf8')
|
|
12
|
+
return JSON.parse(raw)
|
|
13
|
+
} catch {
|
|
14
|
+
return []
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function writeAll(schedules) {
|
|
19
|
+
fs.mkdirSync(dataDir, { recursive: true })
|
|
20
|
+
fs.writeFileSync(filePath, JSON.stringify(schedules, null, 2), 'utf8')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function save(schedule) {
|
|
24
|
+
const all = readAll()
|
|
25
|
+
const idx = all.findIndex(s => s.id === schedule.id)
|
|
26
|
+
if (idx >= 0) {
|
|
27
|
+
all[idx] = { ...all[idx], ...schedule, updatedAt: Date.now() }
|
|
28
|
+
} else {
|
|
29
|
+
all.push({ ...schedule, createdAt: Date.now() })
|
|
30
|
+
}
|
|
31
|
+
writeAll(all)
|
|
32
|
+
return schedule
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function remove(id) {
|
|
36
|
+
const all = readAll()
|
|
37
|
+
const filtered = all.filter(s => s.id !== id)
|
|
38
|
+
if (filtered.length === all.length) return false
|
|
39
|
+
writeAll(filtered)
|
|
40
|
+
return true
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function get(id) {
|
|
44
|
+
return readAll().find(s => s.id === id) || null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function list() {
|
|
48
|
+
return readAll()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return { save, remove, get, list }
|
|
52
|
+
}
|