@orion-js/dogs 3.1.0-alpha.7 → 3.1.0-alpha.8
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 +5 -6
- package/lib/history.test.js +4 -4
- package/lib/repos/JobsRepo.d.ts +1 -0
- package/lib/repos/JobsRepo.js +7 -4
- package/lib/services/Executor.d.ts +1 -0
- package/lib/services/Executor.js +18 -14
- 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
|
@@ -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:
|
|
93
|
+
lockTime: 10,
|
|
95
94
|
logLevel: 'info'
|
|
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)();
|
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/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
|
{
|
|
@@ -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: {
|
|
@@ -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) {
|
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;
|
|
@@ -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)('debug', `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)('debug', `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)
|
|
@@ -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();
|
|
@@ -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.8",
|
|
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": "f11193ee2cc10b9a7c99ef618ae284d319a34de4"
|
|
39
39
|
}
|