@orion-js/dogs 3.1.0-alpha.6 → 3.1.0-alpha.9
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/lib/events.test.js +9 -10
- package/lib/history.test.js +4 -4
- package/lib/recurrent.test.js +2 -2
- package/lib/repos/JobsRepo.d.ts +1 -0
- package/lib/repos/JobsRepo.js +11 -8
- package/lib/services/EventsService.js +1 -1
- package/lib/services/Executor.d.ts +1 -0
- package/lib/services/Executor.js +21 -17
- package/lib/services/WorkerService.js +6 -6
- package/lib/types/HistoryRecord.d.ts +1 -1
- package/lib/types/HistoryRecord.js +2 -2
- package/lib/types/JobRecord.d.ts +1 -1
- package/lib/types/JobRecord.js +2 -2
- package/lib/types/Worker.d.ts +1 -1
- package/package.json +2 -2
package/lib/events.test.js
CHANGED
|
@@ -17,7 +17,7 @@ describe('Event tests', () => {
|
|
|
17
17
|
workersCount: 1,
|
|
18
18
|
pollInterval: 10,
|
|
19
19
|
cooldownPeriod: 10,
|
|
20
|
-
logLevel: '
|
|
20
|
+
logLevel: 'none'
|
|
21
21
|
});
|
|
22
22
|
expect(count).toBe(0);
|
|
23
23
|
await (0, _1.scheduleJob)({
|
|
@@ -56,7 +56,7 @@ describe('Event tests', () => {
|
|
|
56
56
|
workersCount: 1,
|
|
57
57
|
pollInterval: 10,
|
|
58
58
|
cooldownPeriod: 10,
|
|
59
|
-
logLevel: '
|
|
59
|
+
logLevel: 'none'
|
|
60
60
|
});
|
|
61
61
|
expect(passes).toBe(false);
|
|
62
62
|
await (0, _1.scheduleJob)({
|
|
@@ -74,8 +74,7 @@ describe('Event tests', () => {
|
|
|
74
74
|
const job = (0, _1.defineJob)({
|
|
75
75
|
type: 'event',
|
|
76
76
|
async resolve(params, context) {
|
|
77
|
-
if (
|
|
78
|
-
expect(context.tries).toBe(2);
|
|
77
|
+
if (context.tries === 2) {
|
|
79
78
|
context.extendLockTime(10000);
|
|
80
79
|
}
|
|
81
80
|
await (0, helpers_1.sleep)(100);
|
|
@@ -88,20 +87,20 @@ describe('Event tests', () => {
|
|
|
88
87
|
});
|
|
89
88
|
const instance = (0, _1.startWorkers)({
|
|
90
89
|
jobs: { [jobId]: job },
|
|
91
|
-
workersCount:
|
|
90
|
+
workersCount: 2,
|
|
92
91
|
pollInterval: 10,
|
|
93
92
|
cooldownPeriod: 10,
|
|
94
|
-
lockTime:
|
|
95
|
-
logLevel: '
|
|
93
|
+
lockTime: 10,
|
|
94
|
+
logLevel: 'none'
|
|
96
95
|
});
|
|
97
96
|
await (0, _1.scheduleJob)({
|
|
98
97
|
name: jobId,
|
|
99
98
|
runIn: 1
|
|
100
99
|
});
|
|
101
|
-
await (0, helpers_1.sleep)(
|
|
100
|
+
await (0, helpers_1.sleep)(300);
|
|
102
101
|
await instance.stop();
|
|
103
|
-
expect(ranCount).toBe(2);
|
|
104
102
|
expect(staleCount).toBe(1);
|
|
103
|
+
expect(ranCount).toBe(2);
|
|
105
104
|
});
|
|
106
105
|
it('Should only schedule one job with uniqueIdentifier', async () => {
|
|
107
106
|
const jobId = (0, helpers_1.generateId)();
|
|
@@ -117,7 +116,7 @@ describe('Event tests', () => {
|
|
|
117
116
|
workersCount: 1,
|
|
118
117
|
pollInterval: 10,
|
|
119
118
|
cooldownPeriod: 10,
|
|
120
|
-
logLevel: '
|
|
119
|
+
logLevel: 'none'
|
|
121
120
|
});
|
|
122
121
|
await (0, _1.scheduleJob)({
|
|
123
122
|
name: jobId,
|
package/lib/history.test.js
CHANGED
|
@@ -34,7 +34,7 @@ describe('Test Jobs History', () => {
|
|
|
34
34
|
_id: expect.any(String),
|
|
35
35
|
executionId: expect.any(String),
|
|
36
36
|
jobName: jobId,
|
|
37
|
-
|
|
37
|
+
type: 'event',
|
|
38
38
|
priority: 1,
|
|
39
39
|
tries: 1,
|
|
40
40
|
startedAt: expect.any(Date),
|
|
@@ -75,7 +75,7 @@ describe('Test Jobs History', () => {
|
|
|
75
75
|
_id: expect.any(String),
|
|
76
76
|
executionId: expect.any(String),
|
|
77
77
|
jobName: jobId,
|
|
78
|
-
|
|
78
|
+
type: 'event',
|
|
79
79
|
priority: 1,
|
|
80
80
|
tries: 1,
|
|
81
81
|
startedAt: expect.any(Date),
|
|
@@ -119,7 +119,7 @@ describe('Test Jobs History', () => {
|
|
|
119
119
|
_id: expect.any(String),
|
|
120
120
|
executionId: expect.any(String),
|
|
121
121
|
jobName: jobId,
|
|
122
|
-
|
|
122
|
+
type: 'event',
|
|
123
123
|
priority: 1,
|
|
124
124
|
tries: 1,
|
|
125
125
|
startedAt: expect.any(Date),
|
|
@@ -137,7 +137,7 @@ describe('Test Jobs History', () => {
|
|
|
137
137
|
_id: expect.any(String),
|
|
138
138
|
executionId: expect.any(String),
|
|
139
139
|
jobName: jobId,
|
|
140
|
-
|
|
140
|
+
type: 'event',
|
|
141
141
|
priority: 1,
|
|
142
142
|
tries: 1,
|
|
143
143
|
startedAt: expect.any(Date),
|
package/lib/recurrent.test.js
CHANGED
|
@@ -18,7 +18,7 @@ describe('Recurrent tests', () => {
|
|
|
18
18
|
workersCount: 1,
|
|
19
19
|
pollInterval: 100,
|
|
20
20
|
cooldownPeriod: 100,
|
|
21
|
-
logLevel: '
|
|
21
|
+
logLevel: 'none'
|
|
22
22
|
});
|
|
23
23
|
await (0, helpers_1.sleep)(500);
|
|
24
24
|
await instance.stop();
|
|
@@ -38,7 +38,7 @@ describe('Recurrent tests', () => {
|
|
|
38
38
|
workersCount: 1,
|
|
39
39
|
pollInterval: 10,
|
|
40
40
|
cooldownPeriod: 10,
|
|
41
|
-
logLevel: '
|
|
41
|
+
logLevel: 'none'
|
|
42
42
|
});
|
|
43
43
|
await (0, helpers_1.sleep)(200);
|
|
44
44
|
await instance.stop();
|
package/lib/repos/JobsRepo.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export declare class JobsRepo {
|
|
|
10
10
|
nextRunAt: Date;
|
|
11
11
|
addTries: boolean;
|
|
12
12
|
}): Promise<void>;
|
|
13
|
+
deleteEventJob(jobId: string): Promise<void>;
|
|
13
14
|
extendLockTime(jobId: string, extraTime: number): Promise<void>;
|
|
14
15
|
ensureJobRecord(job: JobDefinitionWithName): Promise<void>;
|
|
15
16
|
scheduleJob(options: ScheduleJobRecordOptions): Promise<void>;
|
package/lib/repos/JobsRepo.js
CHANGED
|
@@ -31,7 +31,7 @@ let JobsRepo = class JobsRepo {
|
|
|
31
31
|
},
|
|
32
32
|
options: {
|
|
33
33
|
unique: true,
|
|
34
|
-
partialFilterExpression: {
|
|
34
|
+
partialFilterExpression: { type: 'recurrent' }
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
{
|
|
@@ -53,7 +53,7 @@ let JobsRepo = class JobsRepo {
|
|
|
53
53
|
nextRunAt: { $lte: new Date() },
|
|
54
54
|
$or: [{ lockedUntil: { $exists: false } }, { lockedUntil: { $lte: new Date() } }]
|
|
55
55
|
}, {
|
|
56
|
-
$set: { lockedUntil }
|
|
56
|
+
$set: { lockedUntil, lastRunAt: new Date() }
|
|
57
57
|
}, {
|
|
58
58
|
mongoOptions: {
|
|
59
59
|
sort: {
|
|
@@ -67,7 +67,7 @@ let JobsRepo = class JobsRepo {
|
|
|
67
67
|
return;
|
|
68
68
|
let tries = job.tries || 1;
|
|
69
69
|
if (job.lockedUntil) {
|
|
70
|
-
(0, log_1.log)('
|
|
70
|
+
(0, log_1.log)('info', `Running job "${job.jobName}" that was staled`);
|
|
71
71
|
this.jobs.updateOne(job._id, { $inc: { tries: 1 } });
|
|
72
72
|
tries++;
|
|
73
73
|
}
|
|
@@ -75,7 +75,7 @@ let JobsRepo = class JobsRepo {
|
|
|
75
75
|
jobId: job._id,
|
|
76
76
|
name: job.jobName,
|
|
77
77
|
params: job.params,
|
|
78
|
-
|
|
78
|
+
type: job.type,
|
|
79
79
|
tries,
|
|
80
80
|
lockTime,
|
|
81
81
|
priority: job.priority,
|
|
@@ -92,6 +92,9 @@ let JobsRepo = class JobsRepo {
|
|
|
92
92
|
}
|
|
93
93
|
await this.jobs.updateOne(options.jobId, updator);
|
|
94
94
|
}
|
|
95
|
+
async deleteEventJob(jobId) {
|
|
96
|
+
await this.jobs.deleteOne({ _id: jobId, type: 'event' });
|
|
97
|
+
}
|
|
95
98
|
async extendLockTime(jobId, extraTime) {
|
|
96
99
|
const lockedUntil = new Date(Date.now() + extraTime);
|
|
97
100
|
await this.jobs.updateOne({
|
|
@@ -105,7 +108,7 @@ let JobsRepo = class JobsRepo {
|
|
|
105
108
|
jobName: job.name
|
|
106
109
|
}, {
|
|
107
110
|
$set: {
|
|
108
|
-
|
|
111
|
+
type: job.type,
|
|
109
112
|
priority: job.priority || 1
|
|
110
113
|
},
|
|
111
114
|
$setOnInsert: {
|
|
@@ -113,10 +116,10 @@ let JobsRepo = class JobsRepo {
|
|
|
113
116
|
}
|
|
114
117
|
});
|
|
115
118
|
if (result.upsertedId) {
|
|
116
|
-
(0, log_1.log)('
|
|
119
|
+
(0, log_1.log)('info', `Created job record for "${job.name}"`);
|
|
117
120
|
}
|
|
118
121
|
else {
|
|
119
|
-
(0, log_1.log)('
|
|
122
|
+
(0, log_1.log)('info', `Record for job "${job.name}" already exists`);
|
|
120
123
|
}
|
|
121
124
|
}
|
|
122
125
|
async scheduleJob(options) {
|
|
@@ -127,7 +130,7 @@ let JobsRepo = class JobsRepo {
|
|
|
127
130
|
params: options.params,
|
|
128
131
|
nextRunAt: options.nextRunAt,
|
|
129
132
|
priority: options.priority,
|
|
130
|
-
|
|
133
|
+
type: 'event'
|
|
131
134
|
});
|
|
132
135
|
}
|
|
133
136
|
catch (error) {
|
|
@@ -16,7 +16,7 @@ const JobsRepo_1 = require("../repos/JobsRepo");
|
|
|
16
16
|
const getNextRunDate_1 = require("./getNextRunDate");
|
|
17
17
|
let EventsService = class EventsService {
|
|
18
18
|
async scheduleJob(options) {
|
|
19
|
-
(0, log_1.log)('
|
|
19
|
+
(0, log_1.log)('info', 'Scheduling job...', options);
|
|
20
20
|
await this.jobsRepo.scheduleJob({
|
|
21
21
|
name: options.name,
|
|
22
22
|
priority: options.priority || 1,
|
package/lib/services/Executor.js
CHANGED
|
@@ -32,12 +32,8 @@ let Executor = class Executor {
|
|
|
32
32
|
}
|
|
33
33
|
getJobDefinition(jobToRun, jobs) {
|
|
34
34
|
const job = jobs[jobToRun.name];
|
|
35
|
-
if (
|
|
36
|
-
(0, log_1.log)('warn', `Job record ${jobToRun.name} is
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
if (jobToRun.isRecurrent && job.type === 'event') {
|
|
40
|
-
(0, log_1.log)('warn', `Job record ${jobToRun.name} is recurrent but definition is event`);
|
|
35
|
+
if (jobToRun.type !== job.type) {
|
|
36
|
+
(0, log_1.log)('warn', `Job record "${jobToRun.name}" is "${jobToRun.type}" but definition is "${job.type}"`);
|
|
41
37
|
return;
|
|
42
38
|
}
|
|
43
39
|
return job;
|
|
@@ -58,7 +54,7 @@ let Executor = class Executor {
|
|
|
58
54
|
return;
|
|
59
55
|
}
|
|
60
56
|
else {
|
|
61
|
-
(0, log_1.log)('
|
|
57
|
+
(0, log_1.log)('info', `Error executing job "${jobToRun.name}"`, error);
|
|
62
58
|
}
|
|
63
59
|
const result = await job.onError(error, jobToRun.params, context);
|
|
64
60
|
if (result.action === 'dismiss') {
|
|
@@ -81,7 +77,7 @@ let Executor = class Executor {
|
|
|
81
77
|
await this.jobsHistoryRepo.saveExecution({
|
|
82
78
|
executionId: jobToRun.jobId,
|
|
83
79
|
jobName: jobToRun.name,
|
|
84
|
-
|
|
80
|
+
type: jobToRun.type,
|
|
85
81
|
priority: jobToRun.priority,
|
|
86
82
|
tries: jobToRun.tries,
|
|
87
83
|
uniqueIdentifier: jobToRun.uniqueIdentifier,
|
|
@@ -96,6 +92,20 @@ let Executor = class Executor {
|
|
|
96
92
|
});
|
|
97
93
|
}
|
|
98
94
|
}
|
|
95
|
+
async afterExecutionSuccess(job, jobToRun) {
|
|
96
|
+
if (job.type === 'recurrent') {
|
|
97
|
+
(0, log_1.log)('info', `Scheduling next run for recurrent job "${jobToRun.name}"`);
|
|
98
|
+
await this.jobsRepo.scheduleNextRun({
|
|
99
|
+
jobId: jobToRun.jobId,
|
|
100
|
+
nextRunAt: (0, getNextRunDate_1.getNextRunDate)(job),
|
|
101
|
+
addTries: false
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (job.type === 'event') {
|
|
105
|
+
(0, log_1.log)('info', `Removing event job after success "${jobToRun.name}"`);
|
|
106
|
+
await this.jobsRepo.deleteEventJob(jobToRun.jobId);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
99
109
|
async executeJob(jobs, jobToRun) {
|
|
100
110
|
const job = this.getJobDefinition(jobToRun, jobs);
|
|
101
111
|
if (!job)
|
|
@@ -103,11 +113,11 @@ let Executor = class Executor {
|
|
|
103
113
|
const startedAt = new Date();
|
|
104
114
|
const onStale = () => {
|
|
105
115
|
if (job.onStale) {
|
|
106
|
-
(0, log_1.log)('
|
|
116
|
+
(0, log_1.log)('info', `Job "${jobToRun.name}" is stale`);
|
|
107
117
|
job.onStale(jobToRun.params, context);
|
|
108
118
|
}
|
|
109
119
|
else {
|
|
110
|
-
(0, log_1.log)('
|
|
120
|
+
(0, log_1.log)('error', `Job "${jobToRun.name}" is stale`);
|
|
111
121
|
}
|
|
112
122
|
this.saveExecution({
|
|
113
123
|
startedAt,
|
|
@@ -130,13 +140,7 @@ let Executor = class Executor {
|
|
|
130
140
|
job,
|
|
131
141
|
jobToRun
|
|
132
142
|
});
|
|
133
|
-
|
|
134
|
-
await this.jobsRepo.scheduleNextRun({
|
|
135
|
-
jobId: jobToRun.jobId,
|
|
136
|
-
nextRunAt: (0, getNextRunDate_1.getNextRunDate)(job),
|
|
137
|
-
addTries: false
|
|
138
|
-
});
|
|
139
|
-
}
|
|
143
|
+
await this.afterExecutionSuccess(job, jobToRun);
|
|
140
144
|
}
|
|
141
145
|
catch (error) {
|
|
142
146
|
context.clearStaleTimeout();
|
|
@@ -36,14 +36,14 @@ let WorkerService = class WorkerService {
|
|
|
36
36
|
(0, log_1.log)('debug', 'No job to run');
|
|
37
37
|
return false;
|
|
38
38
|
}
|
|
39
|
-
(0, log_1.log)('
|
|
39
|
+
(0, log_1.log)('info', `Got job to run: ${JSON.stringify(jobToRun)}`);
|
|
40
40
|
await this.executor.executeJob(config.jobs, jobToRun);
|
|
41
41
|
return true;
|
|
42
42
|
}
|
|
43
43
|
async startWorker(config, workersInstance) {
|
|
44
44
|
while (true) {
|
|
45
45
|
if (!workersInstance.running) {
|
|
46
|
-
(0, log_1.log)('
|
|
46
|
+
(0, log_1.log)('info', 'Got signal to stop. Stopping worker...');
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
49
|
try {
|
|
@@ -77,15 +77,15 @@ let WorkerService = class WorkerService {
|
|
|
77
77
|
await Promise.all(jobs
|
|
78
78
|
.filter(job => job.type === 'recurrent')
|
|
79
79
|
.map(async (job) => {
|
|
80
|
-
(0, log_1.log)('
|
|
80
|
+
(0, log_1.log)('info', `Ensuring records for job "${job.name}"...`);
|
|
81
81
|
await this.jobsRepo.ensureJobRecord(job);
|
|
82
82
|
}));
|
|
83
83
|
}
|
|
84
84
|
async runWorkers(config, workersInstance) {
|
|
85
|
-
(0, log_1.log)('
|
|
85
|
+
(0, log_1.log)('info', 'Will ensure records for recurrent jobs');
|
|
86
86
|
await this.ensureRecords(config);
|
|
87
87
|
for (const workerIndex of (0, lodash_1.range)(config.workersCount)) {
|
|
88
|
-
(0, log_1.log)('
|
|
88
|
+
(0, log_1.log)('info', `Starting worker ${workerIndex}`);
|
|
89
89
|
const workerPromise = this.startWorker(config, workersInstance);
|
|
90
90
|
workersInstance.workers.push(workerPromise);
|
|
91
91
|
}
|
|
@@ -105,7 +105,7 @@ let WorkerService = class WorkerService {
|
|
|
105
105
|
};
|
|
106
106
|
(0, log_1.setLogLevel)(config.logLevel);
|
|
107
107
|
const workersInstance = this.createWorkersInstanceDefinition(config);
|
|
108
|
-
(0, log_1.log)('
|
|
108
|
+
(0, log_1.log)('info', 'Starting workers', config);
|
|
109
109
|
this.runWorkers(config, workersInstance);
|
|
110
110
|
return workersInstance;
|
|
111
111
|
}
|
|
@@ -27,8 +27,8 @@ __decorate([
|
|
|
27
27
|
], HistoryRecord.prototype, "jobName", void 0);
|
|
28
28
|
__decorate([
|
|
29
29
|
(0, typed_model_1.Prop)(),
|
|
30
|
-
__metadata("design:type",
|
|
31
|
-
], HistoryRecord.prototype, "
|
|
30
|
+
__metadata("design:type", String)
|
|
31
|
+
], HistoryRecord.prototype, "type", void 0);
|
|
32
32
|
__decorate([
|
|
33
33
|
(0, typed_model_1.Prop)(),
|
|
34
34
|
__metadata("design:type", Number)
|
package/lib/types/JobRecord.d.ts
CHANGED
package/lib/types/JobRecord.js
CHANGED
|
@@ -23,8 +23,8 @@ __decorate([
|
|
|
23
23
|
], JobRecord.prototype, "jobName", void 0);
|
|
24
24
|
__decorate([
|
|
25
25
|
(0, typed_model_1.Prop)(),
|
|
26
|
-
__metadata("design:type",
|
|
27
|
-
], JobRecord.prototype, "
|
|
26
|
+
__metadata("design:type", String)
|
|
27
|
+
], JobRecord.prototype, "type", void 0);
|
|
28
28
|
__decorate([
|
|
29
29
|
(0, typed_model_1.Prop)(),
|
|
30
30
|
__metadata("design:type", Number)
|
package/lib/types/Worker.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/dogs",
|
|
3
|
-
"version": "3.1.0-alpha.
|
|
3
|
+
"version": "3.1.0-alpha.9",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "65ebf7047a46aa5e93e9066d82ca70f81d69f10c"
|
|
39
39
|
}
|