@pikku/schedule 0.7.0 → 0.8.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/CHANGELOG.md CHANGED
@@ -1,9 +1,12 @@
1
1
  # @pikku/schedule
2
2
 
3
- ## 0.7.0
3
+ ## 0.8.0
4
4
 
5
5
  - Updating to match remaining packages
6
6
 
7
+ ## 0.7.0
8
+
9
+ - Updating to match remaining packages
7
10
 
8
11
  ## 0.6.2
9
12
 
@@ -1,6 +1,5 @@
1
1
  import { CronJob } from 'cron';
2
- import { pikkuState, } from '@pikku/core';
3
- import { runScheduledTask } from '@pikku/core/scheduler';
2
+ import { runScheduledTask, getScheduledTasks, } from '@pikku/core/scheduler';
4
3
  export class PikkuTaskScheduler {
5
4
  singletonServices;
6
5
  createSessionServices;
@@ -10,7 +9,7 @@ export class PikkuTaskScheduler {
10
9
  this.createSessionServices = createSessionServices;
11
10
  }
12
11
  startAll() {
13
- const scheduledTasks = pikkuState('scheduler', 'tasks');
12
+ const scheduledTasks = getScheduledTasks();
14
13
  scheduledTasks.forEach((task) => this.startJobSchedule(task));
15
14
  }
16
15
  stopAll() {
@@ -18,7 +17,7 @@ export class PikkuTaskScheduler {
18
17
  this.jobs.clear();
19
18
  }
20
19
  start(names) {
21
- const scheduledTasks = pikkuState('scheduler', 'tasks');
20
+ const scheduledTasks = getScheduledTasks();
22
21
  for (const name of names) {
23
22
  const task = scheduledTasks.get(name);
24
23
  if (task) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikku/schedule",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "author": "yasser.fadl@gmail.com",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",
@@ -17,7 +17,7 @@
17
17
  "test:coverage": "bash run-tests.sh --coverage"
18
18
  },
19
19
  "peerDependencies": {
20
- "@pikku/core": "^0.7.0"
20
+ "@pikku/core": "^0.8.0"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@types/node": "^22.7.8",
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { PikkuTaskScheduler } from './pikku-task-scheduler.js'
@@ -0,0 +1,80 @@
1
+ import { CronJob } from 'cron'
2
+ import {
3
+ CoreAPIFunctionSessionless,
4
+ CoreServices,
5
+ CoreSingletonServices,
6
+ CoreUserSession,
7
+ CreateSessionServices,
8
+ } from '@pikku/core'
9
+ import {
10
+ runScheduledTask,
11
+ CoreScheduledTask,
12
+ getScheduledTasks,
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<CoreAPIFunctionSessionless<void, void>>
59
+ ) {
60
+ const job = new CronJob(
61
+ task.schedule,
62
+ async () => {
63
+ this.singletonServices.logger.info(
64
+ `Running scheduled task: ${task.name}`
65
+ )
66
+ await runScheduledTask({
67
+ singletonServices: this.singletonServices,
68
+ createSessionServices: this.createSessionServices as any,
69
+ name: task.name,
70
+ })
71
+ this.singletonServices.logger.debug(
72
+ `Completed scheduled task: ${task.name}`
73
+ )
74
+ },
75
+ null,
76
+ true
77
+ )
78
+ this.jobs.set(task.name, job)
79
+ }
80
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
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
+ }
@@ -0,0 +1 @@
1
+ {"root":["./src/index.ts","./src/pikku-task-scheduler.ts"],"version":"5.8.3"}