@pikku/schedule 0.6.1

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 ADDED
@@ -0,0 +1,65 @@
1
+ # @pikku/schedule
2
+
3
+ ## 0.6.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 0a92fa7: refactor: pulling schema into seperate package since ajv doesnt work on cloudflare (also keeps bundle size small!)
8
+ - Updated dependencies [0a92fa7]
9
+ - @pikku/core@0.6.7
10
+
11
+ ## 0.6
12
+
13
+ Marking a major release to include channels and scheduled tasks
14
+
15
+ ## 0.5.5
16
+
17
+ ### Patch Changes
18
+
19
+ - 662a6cf: feat: adding scheduled tasks names
20
+ - Updated dependencies [662a6cf]
21
+ - Updated dependencies [c8578ea]
22
+ - Updated dependencies [d2f8edf]
23
+ - @pikku/core@0.5.29
24
+
25
+ ## 0.5.4
26
+
27
+ ### Patch Changes
28
+
29
+ - 886a2fb: refactor: moving singletons (like routes and channels) to global to avoid nodemodule overrides
30
+ - Updated dependencies [a768bad]
31
+ - Updated dependencies [886a2fb]
32
+ - Updated dependencies [886a2fb]
33
+ - @pikku/core@0.5.28
34
+
35
+ ## 0.5.3
36
+
37
+ ### Patch Changes
38
+
39
+ - 0f96787: refactor: dropping cjs support
40
+ - c23524a: refactor: bump to versions to ensure correct package usage
41
+ - Updated dependencies [0f96787]
42
+ - Updated dependencies [64e4a1e]
43
+ - Updated dependencies [c23524a]
44
+ - @pikku/core@0.5.25
45
+
46
+ ## 0.5.2
47
+
48
+ ### Patch Changes
49
+
50
+ - bba25cc: chore: updating all packages to reflect major changes
51
+ - Updated dependencies [bba25cc]
52
+ - Updated dependencies [9deb482]
53
+ - Updated dependencies [ee0c6ea]
54
+ - @pikku/core@0.5.24
55
+
56
+ ## 0.5.1
57
+
58
+ ### Patch Changes
59
+
60
+ - 7fa64a0: feat: making schedule session services optional
61
+ - 539937e: refactor: use a map instead for scheduled tasks
62
+ - Updated dependencies [7fa64a0]
63
+ - Updated dependencies [539937e]
64
+ - Updated dependencies [e9a9968]
65
+ - @pikku/core@0.5.23
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @pikku/scheduler
2
+
3
+ This package is part of @pikku/mono and is responsible for scheduling tasks in pikku.
@@ -0,0 +1 @@
1
+ export { PikkuTaskScheduler } from './pikku-task-scheduler.js';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { PikkuTaskScheduler } from './pikku-task-scheduler.js';
@@ -0,0 +1,12 @@
1
+ import { CoreServices, CoreSingletonServices, CoreUserSession, CreateSessionServices } from '@pikku/core';
2
+ export declare class PikkuTaskScheduler<TaskName extends string> {
3
+ private singletonServices;
4
+ private createSessionServices?;
5
+ private jobs;
6
+ constructor(singletonServices: CoreSingletonServices, createSessionServices?: CreateSessionServices<CoreSingletonServices, CoreServices, CoreUserSession> | undefined);
7
+ startAll(): void;
8
+ stopAll(): void;
9
+ start(names: TaskName[]): void;
10
+ stop(names: TaskName[]): void;
11
+ private startJobSchedule;
12
+ }
@@ -0,0 +1,49 @@
1
+ import { CronJob } from 'cron';
2
+ import { getScheduledTasks, runScheduledTask, } from '@pikku/core/scheduler';
3
+ export class PikkuTaskScheduler {
4
+ singletonServices;
5
+ createSessionServices;
6
+ jobs = new Map();
7
+ constructor(singletonServices, createSessionServices) {
8
+ this.singletonServices = singletonServices;
9
+ this.createSessionServices = createSessionServices;
10
+ }
11
+ startAll() {
12
+ const { scheduledTasks } = getScheduledTasks();
13
+ scheduledTasks.forEach((task) => this.startJobSchedule(task));
14
+ }
15
+ stopAll() {
16
+ this.jobs.forEach((job) => job.stop());
17
+ this.jobs.clear();
18
+ }
19
+ start(names) {
20
+ const { scheduledTasks } = getScheduledTasks();
21
+ for (const name of names) {
22
+ const task = scheduledTasks.get(name);
23
+ if (task) {
24
+ this.startJobSchedule(task);
25
+ }
26
+ }
27
+ }
28
+ stop(names) {
29
+ for (const name of names) {
30
+ const job = this.jobs.get(name);
31
+ if (job) {
32
+ job.stop();
33
+ this.jobs.delete(name);
34
+ }
35
+ }
36
+ }
37
+ startJobSchedule(task) {
38
+ const job = new CronJob(task.schedule, async () => {
39
+ this.singletonServices.logger.info(`Running scheduled task: ${task.name}`);
40
+ await runScheduledTask({
41
+ singletonServices: this.singletonServices,
42
+ createSessionServices: this.createSessionServices,
43
+ name: task.name,
44
+ });
45
+ this.singletonServices.logger.debug(`Completed scheduled task: ${task.name}`);
46
+ }, null, true);
47
+ this.jobs.set(task.name, job);
48
+ }
49
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@pikku/schedule",
3
+ "version": "0.6.1",
4
+ "author": "yasser.fadl@gmail.com",
5
+ "license": "MIT",
6
+ "module": "dist/index.js",
7
+ "main": "dist/index.js",
8
+ "type": "module",
9
+ "scripts": {
10
+ "tsc": "tsc",
11
+ "build:esm": "tsc -b",
12
+ "build": "yarn build:esm",
13
+ "ncu": "ncu",
14
+ "release": "npm run build && npm test",
15
+ "test": "bash run-tests.sh",
16
+ "test:watch": "bash run-tests.sh --watch",
17
+ "test:coverage": "bash run-tests.sh --coverage"
18
+ },
19
+ "peerDependencies": {
20
+ "@pikku/core": "^0.6.7"
21
+ },
22
+ "devDependencies": {
23
+ "@types/node": "^22.7.8",
24
+ "typescript": "^5.6"
25
+ },
26
+ "engines": {
27
+ "node": ">=18"
28
+ },
29
+ "dependencies": {
30
+ "cron": "^3.2.1",
31
+ "cron-schedule": "^5.0.4"
32
+ }
33
+ }
package/run-tests.sh ADDED
@@ -0,0 +1,53 @@
1
+ #!/bin/bash
2
+
3
+ # Enable nullglob to handle cases where no files match the pattern
4
+ shopt -s nullglob
5
+
6
+ # Initialize variables for options
7
+ watch_mode=false
8
+ coverage_mode=false
9
+
10
+ # Parse command-line options
11
+ while [[ $# -gt 0 ]]; do
12
+ case $1 in
13
+ --watch)
14
+ watch_mode=true
15
+ shift
16
+ ;;
17
+ --coverage)
18
+ coverage_mode=true
19
+ shift
20
+ ;;
21
+ *)
22
+ echo "Unknown option: $1"
23
+ exit 1
24
+ ;;
25
+ esac
26
+ done
27
+
28
+ # Define the pattern to match your test files
29
+ pattern="src/*.test.ts"
30
+
31
+ # Expand the pattern into an array of files
32
+ files=($(find src -type f -name "*.test.ts"))
33
+
34
+ # Check if any files matched the pattern
35
+ if [ ${#files[@]} -eq 0 ]; then
36
+ echo "No test files found matching pattern: $pattern"
37
+ exit 0
38
+ fi
39
+
40
+ # Construct the node command
41
+ node_cmd="node --import tsx --test"
42
+
43
+ # Append options based on flags
44
+ if [ "$watch_mode" = true ]; then
45
+ node_cmd="$node_cmd --watch"
46
+ fi
47
+
48
+ if [ "$coverage_mode" = true ]; then
49
+ node_cmd="$node_cmd --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info"
50
+ fi
51
+
52
+ # Execute the node command with the expanded list of files
53
+ $node_cmd "${files[@]}"
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { PikkuTaskScheduler } from './pikku-task-scheduler.js'
@@ -0,0 +1,83 @@
1
+ import { CronJob } from 'cron'
2
+ import {
3
+ CoreAPIFunctionSessionless,
4
+ CoreServices,
5
+ CoreSingletonServices,
6
+ CoreUserSession,
7
+ CreateSessionServices,
8
+ } from '@pikku/core'
9
+ import {
10
+ getScheduledTasks,
11
+ runScheduledTask,
12
+ CoreScheduledTask,
13
+ } from '@pikku/core/scheduler'
14
+
15
+ export class PikkuTaskScheduler<TaskName extends string> {
16
+ private jobs = new Map<string, CronJob>()
17
+
18
+ constructor(
19
+ private singletonServices: CoreSingletonServices,
20
+ private createSessionServices?: CreateSessionServices<
21
+ CoreSingletonServices,
22
+ CoreServices,
23
+ CoreUserSession
24
+ >
25
+ ) {}
26
+
27
+ public startAll() {
28
+ const { scheduledTasks } = getScheduledTasks()
29
+ scheduledTasks.forEach((task) => this.startJobSchedule(task))
30
+ }
31
+
32
+ public stopAll() {
33
+ this.jobs.forEach((job) => job.stop())
34
+ this.jobs.clear()
35
+ }
36
+
37
+ public start(names: TaskName[]) {
38
+ const { scheduledTasks } = getScheduledTasks()
39
+ for (const name of names) {
40
+ const task = scheduledTasks.get(name)
41
+ if (task) {
42
+ this.startJobSchedule(task)
43
+ }
44
+ }
45
+ }
46
+
47
+ public stop(names: TaskName[]) {
48
+ for (const name of names) {
49
+ const job = this.jobs.get(name)
50
+ if (job) {
51
+ job.stop()
52
+ this.jobs.delete(name)
53
+ }
54
+ }
55
+ }
56
+
57
+ private startJobSchedule(
58
+ task: CoreScheduledTask<
59
+ CoreAPIFunctionSessionless<void, void>,
60
+ CoreUserSession
61
+ >
62
+ ) {
63
+ const job = new CronJob(
64
+ task.schedule,
65
+ async () => {
66
+ this.singletonServices.logger.info(
67
+ `Running scheduled task: ${task.name}`
68
+ )
69
+ await runScheduledTask({
70
+ singletonServices: this.singletonServices,
71
+ createSessionServices: this.createSessionServices as any,
72
+ name: task.name,
73
+ })
74
+ this.singletonServices.logger.debug(
75
+ `Completed scheduled task: ${task.name}`
76
+ )
77
+ },
78
+ null,
79
+ true
80
+ )
81
+ this.jobs.set(task.name, job)
82
+ }
83
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "extends": "../tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "module": "Node16",
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
+ }
@@ -0,0 +1 @@
1
+ {"root":["./src/index.ts","./src/pikku-task-scheduler.ts"],"version":"5.7.3"}