pwi-plata-type 0.4.200 → 0.4.201

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.
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.PlataFs = exports.PlataSql = exports.PlataAxios = exports.PlataSwagger = exports.PlataModels = exports.PlataTools = exports.PlataRoutes = exports.PlataCluster = void 0;
36
+ exports.PlataJob = exports.PlataFs = exports.PlataSql = exports.PlataAxios = exports.PlataSwagger = exports.PlataModels = exports.PlataTools = exports.PlataRoutes = exports.PlataCluster = void 0;
37
37
  const PlataCluster = __importStar(require("./libs/cluster"));
38
38
  exports.PlataCluster = PlataCluster;
39
39
  const PlataRoutes = __importStar(require("./libs/router"));
@@ -46,6 +46,8 @@ const PlataSwagger = __importStar(require("./libs/swagger"));
46
46
  exports.PlataSwagger = PlataSwagger;
47
47
  const PlataSql = __importStar(require("./libs/sql"));
48
48
  exports.PlataSql = PlataSql;
49
+ const PlataJob = __importStar(require("./libs/job"));
50
+ exports.PlataJob = PlataJob;
49
51
  const fs_1 = require("./libs/fs");
50
52
  Object.defineProperty(exports, "PlataFs", { enumerable: true, get: function () { return fs_1.PlataFs; } });
51
53
  const axios_1 = require("./libs/axios");
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6DAA8C;AAU1C,oCAAY;AAThB,2DAA4C;AAUxC,kCAAW;AATf,yDAA0C;AAUtC,gCAAU;AATd,2DAA4C;AAUxC,kCAAW;AATf,6DAA8C;AAU1C,oCAAY;AAThB,qDAAsC;AAWlC,4BAAQ;AAVZ,kCAAmC;AAW/B,wFAXK,YAAO,OAWL;AAVX,wCAAyC;AAQrC,2FARK,kBAAU,OAQL"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6DAA8C;AAW1C,oCAAY;AAVhB,2DAA4C;AAWxC,kCAAW;AAVf,yDAA0C;AAWtC,gCAAU;AAVd,2DAA4C;AAWxC,kCAAW;AAVf,6DAA8C;AAW1C,oCAAY;AAVhB,qDAAsC;AAYlC,4BAAQ;AAXZ,qDAAsC;AAalC,4BAAQ;AAZZ,kCAAmC;AAW/B,wFAXK,YAAO,OAWL;AAVX,wCAAyC;AAQrC,2FARK,kBAAU,OAQL"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.schedule = schedule;
4
+ async function schedule(args) {
5
+ const nodeSchedule = require('node-schedule');
6
+ let lock = false;
7
+ let runAfter = false;
8
+ const errorCallback = args.onError ?? ((err) => console.error(err));
9
+ const call = async (fireDate, skipLock) => {
10
+ if (lock === true && skipLock !== true) {
11
+ runAfter = true;
12
+ return;
13
+ }
14
+ lock = true;
15
+ await args.job(fireDate).catch(errorCallback);
16
+ if (runAfter === true) {
17
+ return setImmediate(() => call(new Date(), true));
18
+ }
19
+ lock = false;
20
+ runAfter = false;
21
+ };
22
+ return nodeSchedule.scheduleJob(args.cron, call);
23
+ }
24
+ //# sourceMappingURL=job.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"job.js","sourceRoot":"","sources":["../../src/libs/job.ts"],"names":[],"mappings":";;AAKA,4BA2BC;AA3BM,KAAK,UAAU,QAAQ,CAAC,IAAU;IACrC,MAAM,YAAY,GAAmC,OAAO,CAAC,eAAe,CAAC,CAAA;IAE7E,IAAI,IAAI,GAAG,KAAK,CAAA;IAChB,IAAI,QAAQ,GAAG,KAAK,CAAA;IAEpB,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,IAAI,CAAC,CAAC,GAAQ,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAA;IAExE,MAAM,IAAI,GAAG,KAAK,EAAE,QAAc,EAAE,QAAe,EAAE,EAAE;QACnD,IAAI,IAAI,KAAK,IAAI,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACrC,QAAQ,GAAG,IAAI,CAAA;YACf,OAAM;QACV,CAAC;QAED,IAAI,GAAG,IAAI,CAAA;QAEX,MAAM,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,aAAa,CAAC,CAAA;QAE7C,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,YAAY,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC,CAAA;QACrD,CAAC;QAED,IAAI,GAAG,KAAK,CAAA;QACZ,QAAQ,GAAG,KAAK,CAAA;IACpB,CAAC,CAAA;IAED,OAAO,YAAY,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;AACpD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pwi-plata-type",
3
- "version": "0.4.200",
3
+ "version": "0.4.201",
4
4
  "main": "__BUILD__/index",
5
5
  "types": "src/@types/exported.d.ts",
6
6
  "bin": {
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ import * as PlataTools from './libs/tools'
4
4
  import * as PlataModels from './libs/models'
5
5
  import * as PlataSwagger from './libs/swagger'
6
6
  import * as PlataSql from './libs/sql'
7
+ import * as PlataJob from './libs/job'
7
8
  import { PlataFs } from './libs/fs'
8
9
  import { PlataAxios } from './libs/axios'
9
10
 
@@ -15,5 +16,6 @@ export {
15
16
  PlataSwagger,
16
17
  PlataAxios,
17
18
  PlataSql,
18
- PlataFs
19
+ PlataFs,
20
+ PlataJob,
19
21
  }
@@ -0,0 +1,33 @@
1
+ interface Args {
2
+ cron: string
3
+ job: (fireDate: Date) => Promise<unknown>
4
+ onError?: (err: any) => Promise<unknown> | unknown
5
+ }
6
+ export async function schedule(args: Args) {
7
+ const nodeSchedule: typeof import('node-schedule') = require('node-schedule')
8
+
9
+ let lock = false
10
+ let runAfter = false
11
+
12
+ const errorCallback = args.onError ?? ((err: any) => console.error(err))
13
+
14
+ const call = async (fireDate: Date, skipLock?: true) => {
15
+ if (lock === true && skipLock !== true) {
16
+ runAfter = true
17
+ return
18
+ }
19
+
20
+ lock = true
21
+
22
+ await args.job(fireDate).catch(errorCallback)
23
+
24
+ if (runAfter === true) {
25
+ return setImmediate(() => call(new Date(), true))
26
+ }
27
+
28
+ lock = false
29
+ runAfter = false
30
+ }
31
+
32
+ return nodeSchedule.scheduleJob(args.cron, call)
33
+ }