@orion-js/dogs 3.1.0-alpha.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.
Files changed (42) hide show
  1. package/LICENSE +21 -0
  2. package/lib/defineJob/index.d.ts +2 -0
  3. package/lib/defineJob/index.js +7 -0
  4. package/lib/events.test.d.ts +1 -0
  5. package/lib/events.test.js +136 -0
  6. package/lib/history.test.d.ts +1 -0
  7. package/lib/history.test.js +153 -0
  8. package/lib/index.d.ts +8 -0
  9. package/lib/index.js +21 -0
  10. package/lib/log/index.d.ts +5 -0
  11. package/lib/log/index.js +31 -0
  12. package/lib/recurrent.test.d.ts +1 -0
  13. package/lib/recurrent.test.js +47 -0
  14. package/lib/repos/JobsHistoryRepo.d.ts +7 -0
  15. package/lib/repos/JobsHistoryRepo.js +53 -0
  16. package/lib/repos/JobsRepo.d.ts +15 -0
  17. package/lib/repos/JobsRepo.js +146 -0
  18. package/lib/services/EventsService.d.ts +5 -0
  19. package/lib/services/EventsService.js +36 -0
  20. package/lib/services/Executor.d.ts +19 -0
  21. package/lib/services/Executor.js +166 -0
  22. package/lib/services/WorkerService.d.ts +15 -0
  23. package/lib/services/WorkerService.js +124 -0
  24. package/lib/services/WorkerService.test.d.ts +1 -0
  25. package/lib/services/WorkerService.test.js +10 -0
  26. package/lib/services/getNextRunDate.d.ts +7 -0
  27. package/lib/services/getNextRunDate.js +16 -0
  28. package/lib/tests/setup.d.ts +1 -0
  29. package/lib/tests/setup.js +19 -0
  30. package/lib/types/Events.d.ts +21 -0
  31. package/lib/types/Events.js +2 -0
  32. package/lib/types/HistoryRecord.d.ts +20 -0
  33. package/lib/types/HistoryRecord.js +79 -0
  34. package/lib/types/JobRecord.d.ts +13 -0
  35. package/lib/types/JobRecord.js +59 -0
  36. package/lib/types/JobsDefinition.d.ts +61 -0
  37. package/lib/types/JobsDefinition.js +2 -0
  38. package/lib/types/StartConfig.d.ts +32 -0
  39. package/lib/types/StartConfig.js +2 -0
  40. package/lib/types/Worker.d.ts +28 -0
  41. package/lib/types/Worker.js +2 -0
  42. package/package.json +39 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Orionjs Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ import { JobDefinition } from '../types/JobsDefinition';
2
+ export declare const defineJob: (options: JobDefinition) => JobDefinition;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defineJob = void 0;
4
+ const defineJob = (options) => {
5
+ return options;
6
+ };
7
+ exports.defineJob = defineJob;
@@ -0,0 +1 @@
1
+ import 'reflect-metadata';
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ require("reflect-metadata");
4
+ const helpers_1 = require("@orion-js/helpers");
5
+ const _1 = require(".");
6
+ describe('Event tests', () => {
7
+ it('Should run an event job', async () => {
8
+ let count = 0;
9
+ const job3 = (0, _1.defineJob)({
10
+ type: 'event',
11
+ async resolve(params) {
12
+ count += params.add;
13
+ }
14
+ });
15
+ const instance = (0, _1.startWorkers)({
16
+ jobs: { job3 },
17
+ workersCount: 1,
18
+ pollInterval: 10,
19
+ cooldownPeriod: 10,
20
+ logLevel: 'info'
21
+ });
22
+ expect(count).toBe(0);
23
+ await (0, _1.scheduleJob)({
24
+ name: 'job3',
25
+ params: { add: 5 },
26
+ runIn: 1
27
+ });
28
+ await (0, _1.scheduleJob)({
29
+ name: 'job3',
30
+ params: { add: 25 },
31
+ runIn: 1
32
+ });
33
+ await (0, helpers_1.sleep)(100);
34
+ await instance.stop();
35
+ expect(count).toBe(30);
36
+ });
37
+ it('Should run retry the job 3 times', async () => {
38
+ let passes = false;
39
+ const job4 = (0, _1.defineJob)({
40
+ type: 'event',
41
+ async resolve(params, context) {
42
+ if (context.tries < 3) {
43
+ throw new Error('Failed');
44
+ }
45
+ passes = true;
46
+ },
47
+ async onError() {
48
+ return {
49
+ action: 'retry',
50
+ runIn: 1
51
+ };
52
+ }
53
+ });
54
+ const instance = (0, _1.startWorkers)({
55
+ jobs: { job4 },
56
+ workersCount: 1,
57
+ pollInterval: 10,
58
+ cooldownPeriod: 10,
59
+ logLevel: 'info'
60
+ });
61
+ expect(passes).toBe(false);
62
+ await (0, _1.scheduleJob)({
63
+ name: 'job4',
64
+ runIn: 1
65
+ });
66
+ await (0, helpers_1.sleep)(100);
67
+ await instance.stop();
68
+ expect(passes).toBe(true);
69
+ });
70
+ it('Should throw locktime error and test extendLockTime', async () => {
71
+ const jobId = (0, helpers_1.generateId)();
72
+ let ranCount = 0;
73
+ let staleCount = 0;
74
+ const job = (0, _1.defineJob)({
75
+ type: 'event',
76
+ async resolve(params, context) {
77
+ if (ranCount === 1) {
78
+ expect(context.tries).toBe(2);
79
+ context.extendLockTime(10000);
80
+ }
81
+ await (0, helpers_1.sleep)(100);
82
+ ranCount++;
83
+ },
84
+ async onStale(params, context) {
85
+ expect(context.tries).toBe(1);
86
+ staleCount++;
87
+ }
88
+ });
89
+ const instance = (0, _1.startWorkers)({
90
+ jobs: { [jobId]: job },
91
+ workersCount: 1,
92
+ pollInterval: 10,
93
+ cooldownPeriod: 10,
94
+ lockTime: 40,
95
+ logLevel: 'info'
96
+ });
97
+ await (0, _1.scheduleJob)({
98
+ name: jobId,
99
+ runIn: 1
100
+ });
101
+ await (0, helpers_1.sleep)(200);
102
+ await instance.stop();
103
+ expect(ranCount).toBe(2);
104
+ expect(staleCount).toBe(1);
105
+ });
106
+ it('Should only schedule one job with uniqueIdentifier', async () => {
107
+ const jobId = (0, helpers_1.generateId)();
108
+ let ranCount = 0;
109
+ const job = (0, _1.defineJob)({
110
+ type: 'event',
111
+ async resolve() {
112
+ ranCount++;
113
+ }
114
+ });
115
+ const instance = (0, _1.startWorkers)({
116
+ jobs: { [jobId]: job },
117
+ workersCount: 1,
118
+ pollInterval: 10,
119
+ cooldownPeriod: 10,
120
+ logLevel: 'warn'
121
+ });
122
+ await (0, _1.scheduleJob)({
123
+ name: jobId,
124
+ runIn: 1,
125
+ uniqueIdentifier: 'unique'
126
+ });
127
+ await (0, _1.scheduleJob)({
128
+ name: jobId,
129
+ runIn: 1,
130
+ uniqueIdentifier: 'unique'
131
+ });
132
+ await (0, helpers_1.sleep)(50);
133
+ await instance.stop();
134
+ expect(ranCount).toBe(1);
135
+ });
136
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const helpers_1 = require("@orion-js/helpers");
4
+ const _1 = require(".");
5
+ describe('Test Jobs History', () => {
6
+ it('Should save success history types', async () => {
7
+ const jobId = (0, helpers_1.generateId)();
8
+ const job = (0, _1.defineJob)({
9
+ type: 'event',
10
+ async resolve() {
11
+ await (0, helpers_1.sleep)(50);
12
+ return {
13
+ number: 1
14
+ };
15
+ }
16
+ });
17
+ const instance = (0, _1.startWorkers)({
18
+ jobs: { [jobId]: job },
19
+ workersCount: 1,
20
+ pollInterval: 10
21
+ });
22
+ await (0, _1.scheduleJob)({
23
+ name: jobId,
24
+ runIn: 1,
25
+ params: { id: 2 }
26
+ });
27
+ await (0, helpers_1.sleep)(100);
28
+ await instance.stop();
29
+ const executions = await _1.jobsHistory.getExecutions(jobId);
30
+ expect(executions.length).toBe(1);
31
+ const execution = executions[0];
32
+ expect(execution.duration).toBeGreaterThan(49);
33
+ expect(execution).toEqual({
34
+ _id: expect.any(String),
35
+ executionId: expect.any(String),
36
+ jobName: jobId,
37
+ isRecurrent: false,
38
+ priority: 1,
39
+ tries: 1,
40
+ startedAt: expect.any(Date),
41
+ endedAt: expect.any(Date),
42
+ duration: expect.any(Number),
43
+ expiresAt: expect.any(Date),
44
+ status: 'success',
45
+ errorMessage: null,
46
+ params: { id: 2 },
47
+ result: { number: 1 }
48
+ });
49
+ });
50
+ it('Should save error history types', async () => {
51
+ const jobId = (0, helpers_1.generateId)();
52
+ const job = (0, _1.defineJob)({
53
+ type: 'event',
54
+ async resolve() {
55
+ throw new Error('Hello');
56
+ }
57
+ });
58
+ const instance = (0, _1.startWorkers)({
59
+ jobs: { [jobId]: job },
60
+ workersCount: 1,
61
+ pollInterval: 10,
62
+ logLevel: 'none'
63
+ });
64
+ await (0, _1.scheduleJob)({
65
+ name: jobId,
66
+ runIn: 1,
67
+ params: { id: 4 }
68
+ });
69
+ await (0, helpers_1.sleep)(100);
70
+ await instance.stop();
71
+ const executions = await _1.jobsHistory.getExecutions(jobId);
72
+ expect(executions.length).toBe(1);
73
+ const execution = executions[0];
74
+ expect(execution).toEqual({
75
+ _id: expect.any(String),
76
+ executionId: expect.any(String),
77
+ jobName: jobId,
78
+ isRecurrent: false,
79
+ priority: 1,
80
+ tries: 1,
81
+ startedAt: expect.any(Date),
82
+ endedAt: expect.any(Date),
83
+ duration: expect.any(Number),
84
+ expiresAt: expect.any(Date),
85
+ status: 'error',
86
+ errorMessage: 'Hello',
87
+ params: { id: 4 },
88
+ result: null
89
+ });
90
+ });
91
+ it('Should save stale history types', async () => {
92
+ const jobId = (0, helpers_1.generateId)();
93
+ const job = (0, _1.defineJob)({
94
+ type: 'event',
95
+ async resolve() {
96
+ await (0, helpers_1.sleep)(100);
97
+ return { status: 'ok' };
98
+ }
99
+ });
100
+ const instance = (0, _1.startWorkers)({
101
+ jobs: { [jobId]: job },
102
+ workersCount: 1,
103
+ pollInterval: 10,
104
+ lockTime: 10,
105
+ logLevel: 'none'
106
+ });
107
+ await (0, _1.scheduleJob)({
108
+ name: jobId,
109
+ runIn: 1
110
+ });
111
+ await (0, helpers_1.sleep)(150);
112
+ await instance.stop();
113
+ const executions = await _1.jobsHistory.getExecutions(jobId);
114
+ expect(executions.length).toBe(2);
115
+ const execution1 = executions[1];
116
+ expect(execution1.duration).toBeGreaterThanOrEqual(10);
117
+ expect(execution1.duration).toBeLessThan(100);
118
+ expect(execution1).toEqual({
119
+ _id: expect.any(String),
120
+ executionId: expect.any(String),
121
+ jobName: jobId,
122
+ isRecurrent: false,
123
+ priority: 1,
124
+ tries: 1,
125
+ startedAt: expect.any(Date),
126
+ endedAt: expect.any(Date),
127
+ duration: expect.any(Number),
128
+ expiresAt: expect.any(Date),
129
+ status: 'stale',
130
+ errorMessage: null,
131
+ params: null,
132
+ result: null
133
+ });
134
+ const execution2 = executions[0];
135
+ expect(execution2.duration).toBeGreaterThanOrEqual(100);
136
+ expect(execution2).toEqual({
137
+ _id: expect.any(String),
138
+ executionId: expect.any(String),
139
+ jobName: jobId,
140
+ isRecurrent: false,
141
+ priority: 1,
142
+ tries: 1,
143
+ startedAt: expect.any(Date),
144
+ endedAt: expect.any(Date),
145
+ duration: expect.any(Number),
146
+ expiresAt: expect.any(Date),
147
+ status: 'success',
148
+ errorMessage: null,
149
+ params: null,
150
+ result: { status: 'ok' }
151
+ });
152
+ });
153
+ });
package/lib/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { defineJob } from './defineJob';
2
+ import { StartWorkersConfig } from './types/StartConfig';
3
+ import { ScheduleJobOptions } from './types/Events';
4
+ import { JobsHistoryRepo } from './repos/JobsHistoryRepo';
5
+ declare const jobsHistory: JobsHistoryRepo;
6
+ declare const startWorkers: (config: Partial<StartWorkersConfig>) => import("./types/Worker").WorkersInstance;
7
+ declare const scheduleJob: (options: ScheduleJobOptions) => Promise<void>;
8
+ export { defineJob, startWorkers, scheduleJob, jobsHistory };
package/lib/index.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jobsHistory = exports.scheduleJob = exports.startWorkers = exports.defineJob = void 0;
4
+ const services_1 = require("@orion-js/services");
5
+ const defineJob_1 = require("./defineJob");
6
+ Object.defineProperty(exports, "defineJob", { enumerable: true, get: function () { return defineJob_1.defineJob; } });
7
+ const EventsService_1 = require("./services/EventsService");
8
+ const WorkerService_1 = require("./services/WorkerService");
9
+ const JobsHistoryRepo_1 = require("./repos/JobsHistoryRepo");
10
+ const workerService = (0, services_1.getInstance)(WorkerService_1.WorkerService);
11
+ const eventsService = (0, services_1.getInstance)(EventsService_1.EventsService);
12
+ const jobsHistory = (0, services_1.getInstance)(JobsHistoryRepo_1.JobsHistoryRepo);
13
+ exports.jobsHistory = jobsHistory;
14
+ const startWorkers = (config) => {
15
+ return workerService.startWorkers(config);
16
+ };
17
+ exports.startWorkers = startWorkers;
18
+ const scheduleJob = (options) => {
19
+ return eventsService.scheduleJob(options);
20
+ };
21
+ exports.scheduleJob = scheduleJob;
@@ -0,0 +1,5 @@
1
+ import { LogLevels } from '../types/StartConfig';
2
+ export declare let appLogLevel: LogLevels;
3
+ export declare const levelToInt: (level: LogLevels) => 0 | 1 | 2 | 3;
4
+ export declare const log: (level: LogLevels, ...args: any[]) => void;
5
+ export declare const setLogLevel: (level: LogLevels) => void;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setLogLevel = exports.log = exports.levelToInt = exports.appLogLevel = void 0;
4
+ exports.appLogLevel = 'info';
5
+ const levelToInt = (level) => {
6
+ switch (level) {
7
+ case 'debug':
8
+ return 0;
9
+ case 'info':
10
+ return 1;
11
+ case 'warn':
12
+ return 2;
13
+ case 'error':
14
+ return 3;
15
+ }
16
+ };
17
+ exports.levelToInt = levelToInt;
18
+ const log = (level, ...args) => {
19
+ if (exports.appLogLevel === 'none')
20
+ return;
21
+ const levelInt = (0, exports.levelToInt)(level);
22
+ const appLogLevelInt = (0, exports.levelToInt)(exports.appLogLevel);
23
+ if (levelInt < appLogLevelInt)
24
+ return;
25
+ console[level](...args);
26
+ };
27
+ exports.log = log;
28
+ const setLogLevel = (level) => {
29
+ exports.appLogLevel = level;
30
+ };
31
+ exports.setLogLevel = setLogLevel;
@@ -0,0 +1 @@
1
+ import 'reflect-metadata';
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ require("reflect-metadata");
4
+ const helpers_1 = require("@orion-js/helpers");
5
+ const _1 = require(".");
6
+ describe('Recurrent tests', () => {
7
+ it('Should run a recurrent job', async () => {
8
+ let ran = false;
9
+ const job1 = (0, _1.defineJob)({
10
+ type: 'recurrent',
11
+ runEvery: 1000,
12
+ async resolve() {
13
+ ran = true;
14
+ }
15
+ });
16
+ const instance = (0, _1.startWorkers)({
17
+ jobs: { job1 },
18
+ workersCount: 1,
19
+ pollInterval: 100,
20
+ cooldownPeriod: 100,
21
+ logLevel: 'info'
22
+ });
23
+ await (0, helpers_1.sleep)(500);
24
+ await instance.stop();
25
+ expect(ran).toBe(true);
26
+ });
27
+ it('Should run a recurrent job 3 times', async () => {
28
+ let count = 0;
29
+ const job2 = (0, _1.defineJob)({
30
+ type: 'recurrent',
31
+ runEvery: 20,
32
+ async resolve() {
33
+ count++;
34
+ }
35
+ });
36
+ const instance = (0, _1.startWorkers)({
37
+ jobs: { job2 },
38
+ workersCount: 1,
39
+ pollInterval: 10,
40
+ cooldownPeriod: 10,
41
+ logLevel: 'info'
42
+ });
43
+ await (0, helpers_1.sleep)(200);
44
+ await instance.stop();
45
+ expect(count).toBeGreaterThanOrEqual(3);
46
+ });
47
+ });
@@ -0,0 +1,7 @@
1
+ import { ModelToDocumentTypeWithoutId } from '@orion-js/mongodb';
2
+ import { HistoryRecord } from '../types/HistoryRecord';
3
+ export declare class JobsHistoryRepo {
4
+ private history;
5
+ saveExecution(record: ModelToDocumentTypeWithoutId<HistoryRecord>): Promise<void>;
6
+ getExecutions(jobName: string, limit?: number, skip?: number): Promise<HistoryRecord[]>;
7
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.JobsHistoryRepo = void 0;
10
+ const mongodb_1 = require("@orion-js/mongodb");
11
+ const services_1 = require("@orion-js/services");
12
+ const HistoryRecord_1 = require("../types/HistoryRecord");
13
+ let JobsHistoryRepo = class JobsHistoryRepo {
14
+ constructor() {
15
+ this.history = (0, mongodb_1.createCollection)({
16
+ name: 'orionjs.jobs_dogs_history',
17
+ model: HistoryRecord_1.HistoryRecord,
18
+ indexes: [
19
+ {
20
+ keys: {
21
+ jobName: 1,
22
+ startedAt: 1
23
+ }
24
+ },
25
+ {
26
+ keys: {
27
+ expiresAt: 1
28
+ },
29
+ options: {
30
+ expireAfterSeconds: 0
31
+ }
32
+ }
33
+ ]
34
+ });
35
+ }
36
+ async saveExecution(record) {
37
+ await this.history.insertOne(record);
38
+ }
39
+ async getExecutions(jobName, limit, skip) {
40
+ const cursor = this.history.find({ jobName }).sort({ startedAt: -1 });
41
+ if (skip) {
42
+ cursor.skip(skip);
43
+ }
44
+ if (limit) {
45
+ cursor.limit(limit);
46
+ }
47
+ return await cursor.toArray();
48
+ }
49
+ };
50
+ JobsHistoryRepo = __decorate([
51
+ (0, services_1.Service)()
52
+ ], JobsHistoryRepo);
53
+ exports.JobsHistoryRepo = JobsHistoryRepo;
@@ -0,0 +1,15 @@
1
+ import { ScheduleJobRecordOptions } from '../types/Events';
2
+ import { JobDefinitionWithName } from '../types/JobsDefinition';
3
+ import { JobToRun } from '../types/Worker';
4
+ export declare class JobsRepo {
5
+ private jobs;
6
+ getJobAndLock(jobNames: string[], lockTime: number): Promise<JobToRun>;
7
+ scheduleNextRun(options: {
8
+ jobId: string;
9
+ nextRunAt: Date;
10
+ addTries: boolean;
11
+ }): Promise<void>;
12
+ extendLockTime(jobId: string, extraTime: number): Promise<void>;
13
+ ensureJobRecord(job: JobDefinitionWithName): Promise<void>;
14
+ scheduleJob(options: ScheduleJobRecordOptions): Promise<void>;
15
+ }
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.JobsRepo = void 0;
10
+ const mongodb_1 = require("@orion-js/mongodb");
11
+ const services_1 = require("@orion-js/services");
12
+ const log_1 = require("../log");
13
+ const JobRecord_1 = require("../types/JobRecord");
14
+ let JobsRepo = class JobsRepo {
15
+ constructor() {
16
+ this.jobs = (0, mongodb_1.createCollection)({
17
+ name: 'orionjs.jobs_dogs_records',
18
+ model: JobRecord_1.JobRecord,
19
+ indexes: [
20
+ {
21
+ keys: {
22
+ jobName: 1,
23
+ nextRunAt: 1,
24
+ priority: 1,
25
+ lockedUntil: 1
26
+ }
27
+ },
28
+ {
29
+ keys: {
30
+ jobName: 1
31
+ },
32
+ options: {
33
+ unique: true,
34
+ partialFilterExpression: { isRecurrent: true }
35
+ }
36
+ },
37
+ {
38
+ keys: {
39
+ uniqueIdentifier: 1
40
+ },
41
+ options: {
42
+ unique: true,
43
+ sparse: true
44
+ }
45
+ }
46
+ ]
47
+ });
48
+ }
49
+ async getJobAndLock(jobNames, lockTime) {
50
+ const lockedUntil = new Date(Date.now() + lockTime);
51
+ const job = await this.jobs.findOneAndUpdate({
52
+ jobName: { $in: jobNames },
53
+ nextRunAt: { $lte: new Date() },
54
+ $or: [{ lockedUntil: { $exists: false } }, { lockedUntil: { $lte: new Date() } }]
55
+ }, {
56
+ $set: { lockedUntil }
57
+ }, {
58
+ mongoOptions: {
59
+ sort: {
60
+ priority: 1,
61
+ nextRunAt: 1
62
+ },
63
+ returnDocument: 'before'
64
+ }
65
+ });
66
+ if (!job)
67
+ return;
68
+ let tries = job.tries || 1;
69
+ if (job.lockedUntil) {
70
+ (0, log_1.log)('debug', `Running job "${job.jobName}" that was staled`);
71
+ this.jobs.updateOne(job._id, { $inc: { tries: 1 } });
72
+ tries++;
73
+ }
74
+ return {
75
+ jobId: job._id,
76
+ name: job.jobName,
77
+ params: job.params,
78
+ isRecurrent: job.isRecurrent,
79
+ tries,
80
+ lockTime,
81
+ priority: job.priority,
82
+ uniqueIdentifier: job.uniqueIdentifier
83
+ };
84
+ }
85
+ async scheduleNextRun(options) {
86
+ const updator = {
87
+ $set: { nextRunAt: options.nextRunAt },
88
+ $unset: { lockedUntil: '' }
89
+ };
90
+ if (options.addTries) {
91
+ updator.$inc = { tries: 1 };
92
+ }
93
+ await this.jobs.updateOne(options.jobId, updator);
94
+ }
95
+ async extendLockTime(jobId, extraTime) {
96
+ const lockedUntil = new Date(Date.now() + extraTime);
97
+ await this.jobs.updateOne({
98
+ _id: jobId
99
+ }, {
100
+ $set: { lockedUntil }
101
+ });
102
+ }
103
+ async ensureJobRecord(job) {
104
+ const result = await this.jobs.upsert({
105
+ jobName: job.name
106
+ }, {
107
+ $set: {
108
+ isRecurrent: true,
109
+ priority: job.priority || 1
110
+ },
111
+ $setOnInsert: {
112
+ nextRunAt: new Date()
113
+ }
114
+ });
115
+ if (result.upsertedId) {
116
+ (0, log_1.log)('debug', `Created job record for "${job.name}"`);
117
+ }
118
+ else {
119
+ (0, log_1.log)('debug', `Record for job "${job.name}" already exists`);
120
+ }
121
+ }
122
+ async scheduleJob(options) {
123
+ try {
124
+ await this.jobs.insertOne({
125
+ jobName: options.name,
126
+ uniqueIdentifier: options.uniqueIdentifier,
127
+ params: options.params,
128
+ nextRunAt: options.nextRunAt,
129
+ priority: options.priority,
130
+ isRecurrent: false
131
+ });
132
+ }
133
+ catch (error) {
134
+ if (error.code === 11000 && options.uniqueIdentifier) {
135
+ (0, log_1.log)('info', `Job "${options.name}" with identifier "${options.uniqueIdentifier}" already exists`);
136
+ }
137
+ else {
138
+ throw error;
139
+ }
140
+ }
141
+ }
142
+ };
143
+ JobsRepo = __decorate([
144
+ (0, services_1.Service)()
145
+ ], JobsRepo);
146
+ exports.JobsRepo = JobsRepo;
@@ -0,0 +1,5 @@
1
+ import { ScheduleJobOptions } from '../types/Events';
2
+ export declare class EventsService {
3
+ private jobsRepo;
4
+ scheduleJob(options: ScheduleJobOptions): Promise<void>;
5
+ }