@pikku/schedule 0.12.7 → 0.12.8
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/CHANGELOG.md +28 -0
- package/package.json +10 -6
- package/run-tests.sh +21 -6
- package/src/in-memory-scheduler-service.test.ts +0 -94
- package/src/in-memory-scheduler-service.ts +0 -166
- package/src/index.ts +0 -6
- package/tsconfig.json +0 -18
- package/tsconfig.tsbuildinfo +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
## 0.12.8
|
|
2
|
+
|
|
3
|
+
### Patch Changes
|
|
4
|
+
|
|
5
|
+
- 4c7a1b5: Run the monorepo's own scripts through bun instead of yarn. What moves is the
|
|
6
|
+
package manager each package's `prepublishOnly` and build scripts invoke, plus
|
|
7
|
+
the two manifest fixes bun needs to resolve the tree: `uWebSockets.js` is
|
|
8
|
+
declared with an explicit `github:` specifier, and `@pikku/uws-handler` marks
|
|
9
|
+
its `uWebSockets.js` peer optional so a bun install of a consumer that brings
|
|
10
|
+
its own uWS app does not try to fetch it from the registry.
|
|
11
|
+
|
|
12
|
+
Three published behaviours change, all of them cases where an isolated
|
|
13
|
+
`node_modules` or bun as the runtime had been papered over by yarn's hoisting:
|
|
14
|
+
|
|
15
|
+
- `@pikku/migrator-sql` turns foreign keys on when it opens a sqlite database
|
|
16
|
+
through bun. `node:sqlite` enforces them by default and `bun:sqlite` does not,
|
|
17
|
+
which silently turned every `ON DELETE CASCADE` into a no-op under
|
|
18
|
+
`bunx --bun pikku`.
|
|
19
|
+
- `@pikku/cli` resolves a deploy provider against the project being deployed
|
|
20
|
+
rather than against wherever the CLI itself is installed, which is what its
|
|
21
|
+
own "is not installed" error asks the user to arrange.
|
|
22
|
+
- `@pikku/cli` treats a specifier a runtime hands straight back — bun does this
|
|
23
|
+
for the modules it implements itself — as not resolved from the project, so
|
|
24
|
+
it falls back rather than loading the runtime's own copy.
|
|
25
|
+
|
|
26
|
+
- Updated dependencies [4c7a1b5]
|
|
27
|
+
- @pikku/core@0.12.108
|
|
28
|
+
|
|
1
29
|
## 0.12.7
|
|
2
30
|
|
|
3
31
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pikku/schedule",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.8",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/pikkujs/pikku.git",
|
|
7
|
+
"directory": "packages/schedule"
|
|
8
|
+
},
|
|
4
9
|
"description": "In-memory scheduler service for Pikku cron wirings",
|
|
5
10
|
"author": "yasser.fadl@gmail.com",
|
|
6
11
|
"license": "MIT",
|
|
@@ -16,14 +21,13 @@
|
|
|
16
21
|
"test": "bash run-tests.sh",
|
|
17
22
|
"test:watch": "bash run-tests.sh --watch",
|
|
18
23
|
"test:coverage": "bash run-tests.sh --coverage",
|
|
19
|
-
"prepublishOnly": "
|
|
24
|
+
"prepublishOnly": "bun run build"
|
|
20
25
|
},
|
|
21
26
|
"peerDependencies": {
|
|
22
|
-
"@pikku/core": "^0.12.
|
|
27
|
+
"@pikku/core": "^0.12.108"
|
|
23
28
|
},
|
|
24
29
|
"devDependencies": {
|
|
25
|
-
"@types/node": "^24.13.3"
|
|
26
|
-
"typescript": "^6.0.3"
|
|
30
|
+
"@types/node": "^24.13.3"
|
|
27
31
|
},
|
|
28
32
|
"engines": {
|
|
29
33
|
"node": ">=24"
|
|
@@ -39,4 +43,4 @@
|
|
|
39
43
|
"default": "./dist/index.js"
|
|
40
44
|
}
|
|
41
45
|
}
|
|
42
|
-
}
|
|
46
|
+
}
|
package/run-tests.sh
CHANGED
|
@@ -34,20 +34,35 @@ files=($(find src -type f -name "*.test.ts"))
|
|
|
34
34
|
# Check if any files matched the pattern
|
|
35
35
|
if [ ${#files[@]} -eq 0 ]; then
|
|
36
36
|
echo "No test files found matching pattern: $pattern"
|
|
37
|
+
if [ "${STRICT_TEST_DISCOVERY}" = "1" ]; then
|
|
38
|
+
exit 1
|
|
39
|
+
fi
|
|
37
40
|
exit 0
|
|
38
41
|
fi
|
|
39
42
|
|
|
40
|
-
# Construct the
|
|
41
|
-
|
|
43
|
+
# Construct the bun command
|
|
44
|
+
#
|
|
45
|
+
# --parallel gives every test file its own worker process, which is what node's
|
|
46
|
+
# `--test` did. Several suites keep module-level caches (a TypeScript program, a
|
|
47
|
+
# temp fixture directory) that a shared process would leak between files.
|
|
48
|
+
#
|
|
49
|
+
# --timeout because bun caps a test at 5s by default where node had no cap, and
|
|
50
|
+
# the suites that build real TypeScript programs run well past that.
|
|
51
|
+
bun_cmd=(bun test --parallel --timeout 120000)
|
|
42
52
|
|
|
43
53
|
# Append options based on flags
|
|
44
54
|
if [ "$watch_mode" = true ]; then
|
|
45
|
-
|
|
55
|
+
# --watch reruns in one process; parallel workers have nothing to watch.
|
|
56
|
+
bun_cmd=(bun test --watch --timeout 120000)
|
|
46
57
|
fi
|
|
47
58
|
|
|
48
59
|
if [ "$coverage_mode" = true ]; then
|
|
49
|
-
|
|
60
|
+
# --coverage-dir=. keeps lcov.info at the package root: CI merges every
|
|
61
|
+
# package's report by prefixing its SF paths with the file's own directory,
|
|
62
|
+
# so a nested coverage/ directory would re-root them one level too deep.
|
|
63
|
+
bun_cmd+=(--coverage --coverage-reporter=lcov --coverage-reporter=text --coverage-dir=.)
|
|
64
|
+
export PIKKU_TEST_COVERAGE=1
|
|
50
65
|
fi
|
|
51
66
|
|
|
52
|
-
# Execute the
|
|
53
|
-
"${
|
|
67
|
+
# Execute the bun command with the expanded list of files
|
|
68
|
+
"${bun_cmd[@]}" "${files[@]}"
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { describe, test } from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { pikkuState } from '@pikku/core/state'
|
|
4
|
-
import { addFunction } from '@pikku/core/function'
|
|
5
|
-
|
|
6
|
-
import { InMemorySchedulerService } from './in-memory-scheduler-service.js'
|
|
7
|
-
|
|
8
|
-
const waitFor = async (predicate: () => boolean, withinMs = 2000) => {
|
|
9
|
-
const deadline = Date.now() + withinMs
|
|
10
|
-
while (Date.now() < deadline) {
|
|
11
|
-
if (predicate()) return true
|
|
12
|
-
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
13
|
-
}
|
|
14
|
-
return false
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
describe('InMemorySchedulerService scheduleRPC', () => {
|
|
18
|
-
test('schedules and executes delayed RPC', async () => {
|
|
19
|
-
const logger = {
|
|
20
|
-
info: () => {},
|
|
21
|
-
warn: () => {},
|
|
22
|
-
debug: () => {},
|
|
23
|
-
error: () => {},
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
pikkuState(null, 'package', 'singletonServices', {
|
|
27
|
-
logger,
|
|
28
|
-
} as any)
|
|
29
|
-
|
|
30
|
-
const scheduler = new InMemorySchedulerService()
|
|
31
|
-
try {
|
|
32
|
-
const taskId = await scheduler.scheduleRPC(50, 'testRpc', { ok: true }, {
|
|
33
|
-
userId: 'user-1',
|
|
34
|
-
} as any)
|
|
35
|
-
assert.ok(taskId)
|
|
36
|
-
|
|
37
|
-
const task = await scheduler.getTask(taskId)
|
|
38
|
-
assert.ok(task)
|
|
39
|
-
assert.equal(task.rpcName, 'testRpc')
|
|
40
|
-
} finally {
|
|
41
|
-
await scheduler.close()
|
|
42
|
-
}
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* The delay actually has to invoke the RPC, with its data.
|
|
47
|
-
*
|
|
48
|
-
* The test above only proved the task was registered, which is why the
|
|
49
|
-
* scheduler could dispatch through `runScheduledTask` — a lookup against the
|
|
50
|
-
* *cron task* registry that no scheduled RPC is ever in — and still pass.
|
|
51
|
-
* Every internally scheduled RPC failed with ScheduledTaskNotFoundError,
|
|
52
|
-
* `pikkuWorkflowSleeper` among them, so an async workflow never woke from
|
|
53
|
-
* `workflow.sleep`.
|
|
54
|
-
*/
|
|
55
|
-
test('invokes the named RPC with its data once the delay elapses', async () => {
|
|
56
|
-
const errors: string[] = []
|
|
57
|
-
const logger = {
|
|
58
|
-
info: () => {},
|
|
59
|
-
warn: () => {},
|
|
60
|
-
debug: () => {},
|
|
61
|
-
error: (message: string) => errors.push(message),
|
|
62
|
-
}
|
|
63
|
-
pikkuState(null, 'package', 'singletonServices', { logger } as any)
|
|
64
|
-
|
|
65
|
-
const calls: unknown[] = []
|
|
66
|
-
addFunction(
|
|
67
|
-
'delayedTestRpc',
|
|
68
|
-
{
|
|
69
|
-
func: async (_services: any, data: unknown) => calls.push(data),
|
|
70
|
-
} as any,
|
|
71
|
-
null
|
|
72
|
-
)
|
|
73
|
-
pikkuState(null, 'function', 'meta')['delayedTestRpc'] = {
|
|
74
|
-
name: 'delayedTestRpc',
|
|
75
|
-
sessionless: true,
|
|
76
|
-
permissions: [],
|
|
77
|
-
} as any
|
|
78
|
-
|
|
79
|
-
const scheduler = new InMemorySchedulerService()
|
|
80
|
-
try {
|
|
81
|
-
await scheduler.scheduleRPC(20, 'delayedTestRpc', {
|
|
82
|
-
runId: 'run-1',
|
|
83
|
-
stepId: 'step-1',
|
|
84
|
-
})
|
|
85
|
-
assert.ok(
|
|
86
|
-
await waitFor(() => calls.length > 0),
|
|
87
|
-
`RPC was never invoked; scheduler errors: ${JSON.stringify(errors)}`
|
|
88
|
-
)
|
|
89
|
-
assert.deepEqual(calls[0], { runId: 'run-1', stepId: 'step-1' })
|
|
90
|
-
} finally {
|
|
91
|
-
await scheduler.close()
|
|
92
|
-
}
|
|
93
|
-
})
|
|
94
|
-
})
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import { CronJob } from 'cron'
|
|
2
|
-
import type { CoreUserSession } from '@pikku/core/types'
|
|
3
|
-
import type {
|
|
4
|
-
ScheduledTaskInfo,
|
|
5
|
-
ScheduledTaskSummary,
|
|
6
|
-
} from '@pikku/core/services'
|
|
7
|
-
import { SchedulerService } from '@pikku/core/services'
|
|
8
|
-
import { parseDurationString } from '@pikku/core/utils'
|
|
9
|
-
import { pikkuState, getSingletonServices } from '@pikku/core/state'
|
|
10
|
-
import { runScheduledTask } from '@pikku/core/scheduler'
|
|
11
|
-
import { getScheduledTasks } from '@pikku/core/scheduler'
|
|
12
|
-
import { rpcService } from '@pikku/core/rpc'
|
|
13
|
-
|
|
14
|
-
interface DelayedTask {
|
|
15
|
-
taskId: string
|
|
16
|
-
rpcName: string
|
|
17
|
-
data?: any
|
|
18
|
-
session?: CoreUserSession
|
|
19
|
-
scheduledFor: Date
|
|
20
|
-
timer: ReturnType<typeof setTimeout>
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const getLogger = () => {
|
|
24
|
-
return pikkuState(null, 'package', 'singletonServices')!.logger
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* In-memory SchedulerService implementation.
|
|
29
|
-
* Uses CronJob for recurring tasks and setTimeout for delayed RPCs.
|
|
30
|
-
*/
|
|
31
|
-
export class InMemorySchedulerService extends SchedulerService {
|
|
32
|
-
private cronJobs = new Map<string, CronJob>()
|
|
33
|
-
private delayedTasks = new Map<string, DelayedTask>()
|
|
34
|
-
private idCounter = 0
|
|
35
|
-
|
|
36
|
-
async init(): Promise<void> {}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Schedule a one-off delayed RPC call via setTimeout
|
|
40
|
-
*/
|
|
41
|
-
async scheduleRPC(
|
|
42
|
-
delay: number | string,
|
|
43
|
-
rpcName: string,
|
|
44
|
-
data?: any,
|
|
45
|
-
session?: CoreUserSession
|
|
46
|
-
): Promise<string> {
|
|
47
|
-
const delayMs =
|
|
48
|
-
typeof delay === 'string' ? parseDurationString(delay) : delay
|
|
49
|
-
const taskId = `inmem-${++this.idCounter}-${Date.now()}`
|
|
50
|
-
const scheduledFor = new Date(Date.now() + delayMs)
|
|
51
|
-
|
|
52
|
-
const timer = setTimeout(async () => {
|
|
53
|
-
this.delayedTasks.delete(taskId)
|
|
54
|
-
try {
|
|
55
|
-
// An RPC, not a cron task. `runScheduledTask` looks the name up in the
|
|
56
|
-
// scheduler's task registry, which only ever holds wired cron tasks —
|
|
57
|
-
// so every internally-scheduled RPC died here as
|
|
58
|
-
// ScheduledTaskNotFoundError. The one that matters most is
|
|
59
|
-
// `pikkuWorkflowSleeper`: it is registered by the workflow service as a
|
|
60
|
-
// function, and it is what wakes a run from `workflow.sleep`. With the
|
|
61
|
-
// wake-up lost to a swallowed log line, an async workflow run stopped
|
|
62
|
-
// at its first sleep and stayed `running` forever.
|
|
63
|
-
//
|
|
64
|
-
// `data` has to be forwarded for the same reason. The sleeper is called
|
|
65
|
-
// with `{ runId, stepId }` and cannot identify the sleeping step
|
|
66
|
-
// without it — it was captured in `delayedTasks` but never passed on.
|
|
67
|
-
// This matches what the BullMQ and pg-boss schedulers already do:
|
|
68
|
-
// enqueue `{ rpcName, data, session }` and invoke the RPC with it.
|
|
69
|
-
const services = getSingletonServices()
|
|
70
|
-
const rpc = rpcService.getContextRPCService(
|
|
71
|
-
services as any,
|
|
72
|
-
{},
|
|
73
|
-
false
|
|
74
|
-
) as { invoke: (name: string, data: unknown) => Promise<unknown> }
|
|
75
|
-
await rpc.invoke(rpcName, data)
|
|
76
|
-
} catch (err: unknown) {
|
|
77
|
-
getLogger().error(`Failed to execute delayed RPC '${rpcName}': ${err}`)
|
|
78
|
-
}
|
|
79
|
-
}, delayMs)
|
|
80
|
-
|
|
81
|
-
this.delayedTasks.set(taskId, {
|
|
82
|
-
taskId,
|
|
83
|
-
rpcName,
|
|
84
|
-
data,
|
|
85
|
-
session,
|
|
86
|
-
scheduledFor,
|
|
87
|
-
timer,
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
return taskId
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async unschedule(taskId: string): Promise<boolean> {
|
|
94
|
-
const task = this.delayedTasks.get(taskId)
|
|
95
|
-
if (task) {
|
|
96
|
-
clearTimeout(task.timer)
|
|
97
|
-
this.delayedTasks.delete(taskId)
|
|
98
|
-
return true
|
|
99
|
-
}
|
|
100
|
-
return false
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async getTask(taskId: string): Promise<ScheduledTaskInfo | null> {
|
|
104
|
-
const task = this.delayedTasks.get(taskId)
|
|
105
|
-
if (!task) return null
|
|
106
|
-
return {
|
|
107
|
-
taskId: task.taskId,
|
|
108
|
-
rpcName: task.rpcName,
|
|
109
|
-
scheduledFor: task.scheduledFor,
|
|
110
|
-
data: task.data,
|
|
111
|
-
session: task.session,
|
|
112
|
-
status: 'scheduled',
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
async getAllTasks(): Promise<ScheduledTaskSummary[]> {
|
|
117
|
-
return Array.from(this.delayedTasks.values()).map((t) => ({
|
|
118
|
-
taskId: t.taskId,
|
|
119
|
-
rpcName: t.rpcName,
|
|
120
|
-
scheduledFor: t.scheduledFor,
|
|
121
|
-
}))
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
async close(): Promise<void> {
|
|
125
|
-
await this.stop()
|
|
126
|
-
// Clear delayed tasks
|
|
127
|
-
for (const [, task] of this.delayedTasks) {
|
|
128
|
-
clearTimeout(task.timer)
|
|
129
|
-
}
|
|
130
|
-
this.delayedTasks.clear()
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Start recurring scheduled tasks.
|
|
135
|
-
*/
|
|
136
|
-
async start(): Promise<void> {
|
|
137
|
-
const scheduledTasks = getScheduledTasks()
|
|
138
|
-
for (const [, task] of scheduledTasks) {
|
|
139
|
-
this.startCronJob(task.name, task.schedule)
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Stop all recurring CronJobs.
|
|
145
|
-
*/
|
|
146
|
-
async stop(): Promise<void> {
|
|
147
|
-
for (const [, job] of this.cronJobs) {
|
|
148
|
-
job.stop()
|
|
149
|
-
}
|
|
150
|
-
this.cronJobs.clear()
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
private startCronJob(name: string, schedule: string) {
|
|
154
|
-
const job = new CronJob(
|
|
155
|
-
schedule,
|
|
156
|
-
async () => {
|
|
157
|
-
getLogger().info(`Running scheduled task: ${name}`)
|
|
158
|
-
await runScheduledTask({ name })
|
|
159
|
-
getLogger().debug(`Completed scheduled task: ${name}`)
|
|
160
|
-
},
|
|
161
|
-
null,
|
|
162
|
-
true
|
|
163
|
-
)
|
|
164
|
-
this.cronJobs.set(name, job)
|
|
165
|
-
}
|
|
166
|
-
}
|
package/src/index.ts
DELETED
package/tsconfig.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"rootDir": "./src",
|
|
5
|
-
"module": "Node18",
|
|
6
|
-
"outDir": "dist",
|
|
7
|
-
"target": "esnext",
|
|
8
|
-
"declaration": true,
|
|
9
|
-
"resolveJsonModule": true
|
|
10
|
-
},
|
|
11
|
-
"include": ["bin/**/*.ts", "src/**/*.ts"],
|
|
12
|
-
"exclude": ["**/*.test.ts", "node_modules", "bin/dist"],
|
|
13
|
-
"references": [
|
|
14
|
-
{
|
|
15
|
-
"path": "../core/tsconfig.json"
|
|
16
|
-
}
|
|
17
|
-
]
|
|
18
|
-
}
|
package/tsconfig.tsbuildinfo
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"root":["./src/in-memory-scheduler-service.ts","./src/index.ts"],"version":"6.0.3"}
|