@dnax/core 0.2.5 → 0.2.6

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/define/index.ts CHANGED
@@ -4,6 +4,8 @@ import type {
4
4
  Endpoint,
5
5
  Service,
6
6
  Socket,
7
+ cronConfig,
8
+ cronCtx,
7
9
  middlewareCtx,
8
10
  } from "../types";
9
11
  import { Cfg } from "../config/";
@@ -31,10 +33,13 @@ function WebSocket(config: Socket) {
31
33
  }
32
34
 
33
35
  function Middleware(ctx: middlewareCtx) {
34
- console.log(ctx);
35
36
  return ctx;
36
37
  }
37
38
 
39
+ function Task(config: cronConfig) {
40
+ return config;
41
+ }
42
+
38
43
  const define = {
39
44
  Service,
40
45
  Config,
@@ -43,6 +48,8 @@ const define = {
43
48
  App: Config,
44
49
  WebSocket,
45
50
  Middleware,
51
+ Task,
52
+ Cron: Task,
46
53
  };
47
54
 
48
55
  export default define;
@@ -0,0 +1,49 @@
1
+ import { Cron } from "croner";
2
+ import { Glob } from "bun";
3
+ import { Cfg } from "../../config";
4
+ import path from "path";
5
+ import type { cronConfig } from "../../types";
6
+ import cleanDeep from "clean-deep";
7
+ import { useRest } from "../../driver/mongo";
8
+ async function initCron() {
9
+ let cronTasks = [];
10
+ if (Cfg?.tenants?.length) {
11
+ for await (let t of Cfg.tenants) {
12
+ let tenantPath = `${t.dir}/tasks/**/**.cron.{ts,js}`;
13
+ const glob = new Glob(tenantPath);
14
+ for await (let file of glob.scan({
15
+ cwd: Cfg.cwd,
16
+ })) {
17
+ let fullPathFile = path.join(Cfg.cwd || "", file);
18
+ await import(fullPathFile)
19
+ .then((inject) => {
20
+ let cronOpt = inject.default as cronConfig;
21
+ if (cronOpt?.enabled) {
22
+ let cronTask = new Cron(cronOpt.pattern, {
23
+ ...cleanDeep(cronOpt.options),
24
+ });
25
+ cronOpt.handler({
26
+ io: Cfg.io,
27
+ rest: new useRest({
28
+ tenant_id: t.id,
29
+ }),
30
+ task: cronTask,
31
+ });
32
+ cronTasks.push({
33
+ tenant_id: t.id,
34
+ name: cronOpt.name,
35
+ cron: cronOpt,
36
+ });
37
+ }
38
+ })
39
+ .catch((err) => {
40
+ console.error(err);
41
+ });
42
+ }
43
+ }
44
+ }
45
+
46
+ //Cfg.endpoints = endpoints;
47
+ }
48
+
49
+ export { initCron };
package/lib/index.ts CHANGED
@@ -2,6 +2,7 @@ import { loadAllCollections, syncCollectionDatabase } from "./collection";
2
2
  import { connectTenantsDatabase } from "../driver";
3
3
  import { loadEndpoints } from "./endpoint";
4
4
  import { loadServices } from "./service";
5
+ import { initCron } from "./cron";
5
6
  import { loadSocket } from "./socket";
6
7
  // load all ressource
7
8
  async function init(cf = { app: null }) {
@@ -21,6 +22,8 @@ async function init(cf = { app: null }) {
21
22
 
22
23
  // load all loadEndpoints
23
24
  await loadEndpoints();
25
+
26
+ await initCron();
24
27
  }
25
28
 
26
29
  export { init };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
@@ -20,12 +20,13 @@
20
20
  "@lukeed/ms": "^2.0.2",
21
21
  "@types/jsonwebtoken": "^9.0.6",
22
22
  "boxen": "^7.1.1",
23
+ "bree": "^9.2.4",
23
24
  "chokidar": "^3.6.0",
24
25
  "clean-deep": "^3.4.0",
25
26
  "collect.js": "^4.36.1",
26
27
  "consola": "^3.2.3",
27
28
  "cookie": "^0.6.0",
28
- "croner": "^8.0.2",
29
+ "croner": "^8.1.1",
29
30
  "find-open-port": "^2.0.3",
30
31
  "generate-unique-id": "^2.0.3",
31
32
  "hono": "^4.4.3",
package/types/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { AnySchema } from "joi";
2
+ import { Cron } from "croner";
2
3
  import { updateParams } from "./../driver/mongo/@types";
3
4
  import * as v from "valibot";
4
5
  import type { Db, MongoClient } from "mongodb";
@@ -119,6 +120,25 @@ export type Field = {
119
120
  relationTo?: string;
120
121
  };
121
122
 
123
+ export type cronCtx = {
124
+ rest: InstanceType<typeof useRest>;
125
+ io: Io;
126
+ task: InstanceType<typeof Cron>;
127
+ };
128
+
129
+ export type cronConfig = {
130
+ name: string;
131
+ enabled?: boolean;
132
+ pattern: string;
133
+ options?: {
134
+ maxRuns?: Number;
135
+ startAt?: Date;
136
+ stopAt?: Date;
137
+ paused?: Boolean;
138
+ };
139
+ handler: (ctx: cronCtx) => void;
140
+ };
141
+
122
142
  export type middlewareCtx = (ctx: {
123
143
  token?: string;
124
144
  action?: Actions;