@orion-js/dogs 3.11.15 → 4.0.0-alpha.3
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/dist/index.cjs +114048 -0
- package/dist/index.d.ts +748 -0
- package/dist/index.js +114022 -0
- package/package.json +23 -22
- package/LICENSE +0 -21
- package/lib/defineJob/index.d.ts +0 -2
- package/lib/defineJob/index.js +0 -12
- package/lib/events.test.d.ts +0 -1
- package/lib/events.test.js +0 -133
- package/lib/history.test.d.ts +0 -1
- package/lib/history.test.js +0 -140
- package/lib/index.d.ts +0 -12
- package/lib/index.js +0 -40
- package/lib/recurrent.test.d.ts +0 -1
- package/lib/recurrent.test.js +0 -47
- package/lib/repos/JobsHistoryRepo.d.ts +0 -7
- package/lib/repos/JobsHistoryRepo.js +0 -67
- package/lib/repos/JobsRepo.d.ts +0 -19
- package/lib/repos/JobsRepo.js +0 -158
- package/lib/service/index.d.ts +0 -10
- package/lib/service/index.js +0 -50
- package/lib/service/index.test.d.ts +0 -1
- package/lib/service/index.test.js +0 -51
- package/lib/services/EventsService.d.ts +0 -5
- package/lib/services/EventsService.js +0 -36
- package/lib/services/Executor.d.ts +0 -20
- package/lib/services/Executor.js +0 -195
- package/lib/services/WorkerService.d.ts +0 -16
- package/lib/services/WorkerService.js +0 -142
- package/lib/services/WorkerService.test.d.ts +0 -1
- package/lib/services/WorkerService.test.js +0 -10
- package/lib/services/getNextRunDate.d.ts +0 -9
- package/lib/services/getNextRunDate.js +0 -19
- package/lib/stale.test.d.ts +0 -1
- package/lib/stale.test.js +0 -108
- package/lib/tests/setup.d.ts +0 -1
- package/lib/tests/setup.js +0 -19
- package/lib/types/Events.d.ts +0 -21
- package/lib/types/Events.js +0 -2
- package/lib/types/HistoryRecord.d.ts +0 -21
- package/lib/types/HistoryRecord.js +0 -83
- package/lib/types/JobRecord.d.ts +0 -13
- package/lib/types/JobRecord.js +0 -59
- package/lib/types/JobsDefinition.d.ts +0 -61
- package/lib/types/JobsDefinition.js +0 -2
- package/lib/types/StartConfig.d.ts +0 -28
- package/lib/types/StartConfig.js +0 -2
- package/lib/types/Worker.d.ts +0 -38
- package/lib/types/Worker.js +0 -2
- package/lib/types/index.d.ts +0 -6
- package/lib/types/index.js +0 -22
package/lib/repos/JobsRepo.js
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
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 helpers_1 = require("@orion-js/helpers");
|
|
11
|
-
const logger_1 = require("@orion-js/logger");
|
|
12
|
-
const mongodb_1 = require("@orion-js/mongodb");
|
|
13
|
-
const services_1 = require("@orion-js/services");
|
|
14
|
-
const lodash_1 = require("lodash");
|
|
15
|
-
const JobRecord_1 = require("../types/JobRecord");
|
|
16
|
-
let JobsRepo = class JobsRepo {
|
|
17
|
-
constructor() {
|
|
18
|
-
this.jobs = () => (0, mongodb_1.createCollection)({
|
|
19
|
-
idGeneration: 'uuid',
|
|
20
|
-
name: 'orionjs.jobs_dogs_records',
|
|
21
|
-
model: JobRecord_1.JobRecord,
|
|
22
|
-
indexes: [
|
|
23
|
-
{
|
|
24
|
-
keys: {
|
|
25
|
-
jobName: 1,
|
|
26
|
-
priority: -1,
|
|
27
|
-
nextRunAt: 1
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
keys: {
|
|
32
|
-
jobName: 1
|
|
33
|
-
},
|
|
34
|
-
options: {
|
|
35
|
-
unique: true,
|
|
36
|
-
partialFilterExpression: { type: 'recurrent' }
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
keys: {
|
|
41
|
-
uniqueIdentifier: 1
|
|
42
|
-
},
|
|
43
|
-
options: {
|
|
44
|
-
unique: true,
|
|
45
|
-
sparse: true
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
]
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
async getJobAndLock(jobNames, lockTime) {
|
|
52
|
-
const lockedUntil = new Date(Date.now() + lockTime);
|
|
53
|
-
const job = await this.jobs().findOneAndUpdate({
|
|
54
|
-
jobName: { $in: jobNames },
|
|
55
|
-
nextRunAt: { $lte: new Date() },
|
|
56
|
-
$or: [{ lockedUntil: { $exists: false } }, { lockedUntil: { $lte: new Date() } }]
|
|
57
|
-
}, {
|
|
58
|
-
$set: { lockedUntil, lastRunAt: new Date() }
|
|
59
|
-
}, {
|
|
60
|
-
mongoOptions: {
|
|
61
|
-
sort: {
|
|
62
|
-
priority: -1,
|
|
63
|
-
nextRunAt: 1
|
|
64
|
-
},
|
|
65
|
-
returnDocument: 'before'
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
if (!job)
|
|
69
|
-
return;
|
|
70
|
-
let tries = job.tries || 1;
|
|
71
|
-
if (job.lockedUntil) {
|
|
72
|
-
logger_1.logger.info(`Running job "${job.jobName}" that was staled`);
|
|
73
|
-
this.jobs().updateOne(job._id, { $inc: { tries: 1 } });
|
|
74
|
-
tries++;
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
jobId: job._id,
|
|
78
|
-
executionId: (0, helpers_1.generateId)(),
|
|
79
|
-
name: job.jobName,
|
|
80
|
-
params: job.params,
|
|
81
|
-
type: job.type,
|
|
82
|
-
tries,
|
|
83
|
-
lockTime,
|
|
84
|
-
priority: job.priority,
|
|
85
|
-
uniqueIdentifier: job.uniqueIdentifier
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
async setJobRecordPriority(jobId, priority) {
|
|
89
|
-
await this.jobs().updateOne(jobId, { $set: { priority } });
|
|
90
|
-
}
|
|
91
|
-
async scheduleNextRun(options) {
|
|
92
|
-
const updator = {
|
|
93
|
-
$set: { nextRunAt: options.nextRunAt, priority: options.priority },
|
|
94
|
-
$unset: { lockedUntil: '' }
|
|
95
|
-
};
|
|
96
|
-
if (options.addTries) {
|
|
97
|
-
updator.$inc = { tries: 1 };
|
|
98
|
-
}
|
|
99
|
-
await this.jobs().updateOne(options.jobId, updator);
|
|
100
|
-
}
|
|
101
|
-
async deleteEventJob(jobId) {
|
|
102
|
-
await this.jobs().deleteOne({ _id: jobId, type: 'event' });
|
|
103
|
-
}
|
|
104
|
-
async extendLockTime(jobId, extraTime) {
|
|
105
|
-
const lockedUntil = new Date(Date.now() + extraTime);
|
|
106
|
-
await this.jobs().updateOne({
|
|
107
|
-
_id: jobId
|
|
108
|
-
}, {
|
|
109
|
-
$set: { lockedUntil }
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
async ensureJobRecord(job) {
|
|
113
|
-
await this.jobs().connectionPromise;
|
|
114
|
-
const result = await this.jobs().upsert({
|
|
115
|
-
jobName: job.name
|
|
116
|
-
}, {
|
|
117
|
-
$set: {
|
|
118
|
-
type: job.type,
|
|
119
|
-
priority: job.priority
|
|
120
|
-
},
|
|
121
|
-
$setOnInsert: {
|
|
122
|
-
nextRunAt: new Date()
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
if (result.upsertedId) {
|
|
126
|
-
logger_1.logger.debug(`Created job record for "${job.name}"`);
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
logger_1.logger.debug(`Record for job "${job.name}" already exists`);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
async scheduleJob(options) {
|
|
133
|
-
try {
|
|
134
|
-
await this.jobs().insertOne({
|
|
135
|
-
jobName: options.name,
|
|
136
|
-
uniqueIdentifier: options.uniqueIdentifier,
|
|
137
|
-
params: options.params,
|
|
138
|
-
nextRunAt: options.nextRunAt,
|
|
139
|
-
priority: options.priority,
|
|
140
|
-
type: 'event'
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
catch (error) {
|
|
144
|
-
if (error.isValidationError &&
|
|
145
|
-
(0, lodash_1.values)(error.validationErrors).includes('notUnique') &&
|
|
146
|
-
options.uniqueIdentifier) {
|
|
147
|
-
logger_1.logger.info(`Job "${options.name}" with identifier "${options.uniqueIdentifier}" already exists`);
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
throw error;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
exports.JobsRepo = JobsRepo;
|
|
156
|
-
exports.JobsRepo = JobsRepo = __decorate([
|
|
157
|
-
(0, services_1.Service)()
|
|
158
|
-
], JobsRepo);
|
package/lib/service/index.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { EventJobDefinition, JobDefinition, RecurrentJobDefinition } from '../types';
|
|
2
|
-
export declare function Jobs(): ClassDecorator;
|
|
3
|
-
export interface JobsPropertyDescriptor extends Omit<PropertyDecorator, 'value'> {
|
|
4
|
-
value?: JobDefinition['resolve'];
|
|
5
|
-
}
|
|
6
|
-
export declare function RecurrentJob(options: Omit<RecurrentJobDefinition, 'resolve' | 'type'>): (target: any, propertyKey: string, descriptor: JobsPropertyDescriptor) => void;
|
|
7
|
-
export declare function EventJob(options?: Omit<EventJobDefinition, 'resolve' | 'type'>): (target: any, propertyKey: string, descriptor: JobsPropertyDescriptor) => void;
|
|
8
|
-
export declare function getServiceJobs(target: any): {
|
|
9
|
-
[key: string]: JobDefinition;
|
|
10
|
-
};
|
package/lib/service/index.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Jobs = Jobs;
|
|
4
|
-
exports.RecurrentJob = RecurrentJob;
|
|
5
|
-
exports.EventJob = EventJob;
|
|
6
|
-
exports.getServiceJobs = getServiceJobs;
|
|
7
|
-
const services_1 = require("@orion-js/services");
|
|
8
|
-
const defineJob_1 = require("../defineJob");
|
|
9
|
-
function Jobs() {
|
|
10
|
-
return function (target) {
|
|
11
|
-
(0, services_1.Service)()(target);
|
|
12
|
-
target.prototype.service = target;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
function RecurrentJob(options) {
|
|
16
|
-
return function (target, propertyKey, descriptor) {
|
|
17
|
-
if (!descriptor.value)
|
|
18
|
-
throw new Error(`You must pass resolver function to ${propertyKey}`);
|
|
19
|
-
target.echoes = target.echoes || {};
|
|
20
|
-
target.echoes[propertyKey] = (0, defineJob_1.defineJob)({
|
|
21
|
-
...options,
|
|
22
|
-
type: 'recurrent',
|
|
23
|
-
resolve: async (params, viewer) => {
|
|
24
|
-
const instance = (0, services_1.getInstance)(target.service);
|
|
25
|
-
return await instance[propertyKey](params, viewer);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
function EventJob(options = {}) {
|
|
31
|
-
return function (target, propertyKey, descriptor) {
|
|
32
|
-
if (!descriptor.value)
|
|
33
|
-
throw new Error(`You must pass resolver function to ${propertyKey}`);
|
|
34
|
-
target.echoes = target.echoes || {};
|
|
35
|
-
target.echoes[propertyKey] = (0, defineJob_1.defineJob)({
|
|
36
|
-
...options,
|
|
37
|
-
type: 'event',
|
|
38
|
-
resolve: async (params, viewer) => {
|
|
39
|
-
const instance = (0, services_1.getInstance)(target.service);
|
|
40
|
-
return await instance[propertyKey](params, viewer);
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
function getServiceJobs(target) {
|
|
46
|
-
if (!target.prototype) {
|
|
47
|
-
throw new Error('You must pass a class to getServiceRoutes');
|
|
48
|
-
}
|
|
49
|
-
return target.prototype.echoes || {};
|
|
50
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,51 +0,0 @@
|
|
|
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
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const _1 = require(".");
|
|
13
|
-
describe('Jobs (dogs) with service injections', () => {
|
|
14
|
-
it('Should define a jobs map using services', async () => {
|
|
15
|
-
let ExampleJobsService = class ExampleJobsService {
|
|
16
|
-
async job1() {
|
|
17
|
-
return {};
|
|
18
|
-
}
|
|
19
|
-
async job2() {
|
|
20
|
-
return {};
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
__decorate([
|
|
24
|
-
(0, _1.EventJob)(),
|
|
25
|
-
__metadata("design:type", Function),
|
|
26
|
-
__metadata("design:paramtypes", []),
|
|
27
|
-
__metadata("design:returntype", Promise)
|
|
28
|
-
], ExampleJobsService.prototype, "job1", null);
|
|
29
|
-
__decorate([
|
|
30
|
-
(0, _1.RecurrentJob)({ runEvery: 1000 }),
|
|
31
|
-
__metadata("design:type", Function),
|
|
32
|
-
__metadata("design:paramtypes", []),
|
|
33
|
-
__metadata("design:returntype", Promise)
|
|
34
|
-
], ExampleJobsService.prototype, "job2", null);
|
|
35
|
-
ExampleJobsService = __decorate([
|
|
36
|
-
(0, _1.Jobs)()
|
|
37
|
-
], ExampleJobsService);
|
|
38
|
-
const jobs = (0, _1.getServiceJobs)(ExampleJobsService);
|
|
39
|
-
expect(jobs).toMatchObject({
|
|
40
|
-
job1: {
|
|
41
|
-
type: 'event',
|
|
42
|
-
resolve: expect.any(Function)
|
|
43
|
-
},
|
|
44
|
-
job2: {
|
|
45
|
-
type: 'recurrent',
|
|
46
|
-
runEvery: 1000,
|
|
47
|
-
resolve: expect.any(Function)
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
});
|
|
@@ -1,36 +0,0 @@
|
|
|
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
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.EventsService = void 0;
|
|
13
|
-
const logger_1 = require("@orion-js/logger");
|
|
14
|
-
const services_1 = require("@orion-js/services");
|
|
15
|
-
const JobsRepo_1 = require("../repos/JobsRepo");
|
|
16
|
-
const getNextRunDate_1 = require("./getNextRunDate");
|
|
17
|
-
let EventsService = class EventsService {
|
|
18
|
-
async scheduleJob(options) {
|
|
19
|
-
logger_1.logger.debug('Scheduling job...', options);
|
|
20
|
-
await this.jobsRepo.scheduleJob({
|
|
21
|
-
name: options.name,
|
|
22
|
-
priority: options.priority || 100,
|
|
23
|
-
nextRunAt: (0, getNextRunDate_1.getNextRunDate)(options),
|
|
24
|
-
params: options.params || null,
|
|
25
|
-
uniqueIdentifier: options.uniqueIdentifier
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
exports.EventsService = EventsService;
|
|
30
|
-
__decorate([
|
|
31
|
-
(0, services_1.Inject)(),
|
|
32
|
-
__metadata("design:type", JobsRepo_1.JobsRepo)
|
|
33
|
-
], EventsService.prototype, "jobsRepo", void 0);
|
|
34
|
-
exports.EventsService = EventsService = __decorate([
|
|
35
|
-
(0, services_1.Service)()
|
|
36
|
-
], EventsService);
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { PlainObject } from '../types/HistoryRecord';
|
|
2
|
-
import { JobDefinition, JobsDefinition } from '../types/JobsDefinition';
|
|
3
|
-
import { ExecutionContext, JobToRun } from '../types/Worker';
|
|
4
|
-
export declare class Executor {
|
|
5
|
-
private readonly jobsRepo;
|
|
6
|
-
private readonly jobsHistoryRepo;
|
|
7
|
-
getContext(job: JobDefinition, jobToRun: JobToRun, onStale: Function): ExecutionContext;
|
|
8
|
-
getJobDefinition(jobToRun: JobToRun, jobs: JobsDefinition): JobDefinition;
|
|
9
|
-
onError(error: any, job: JobDefinition, jobToRun: JobToRun, context: ExecutionContext): Promise<void>;
|
|
10
|
-
saveExecution(options: {
|
|
11
|
-
startedAt: Date;
|
|
12
|
-
status: 'stale' | 'error' | 'success';
|
|
13
|
-
errorMessage?: string;
|
|
14
|
-
result?: PlainObject;
|
|
15
|
-
job: JobDefinition;
|
|
16
|
-
jobToRun: JobToRun;
|
|
17
|
-
}): Promise<void>;
|
|
18
|
-
afterExecutionSuccess(job: JobDefinition, jobToRun: JobToRun, context: ExecutionContext): Promise<void>;
|
|
19
|
-
executeJob(jobs: JobsDefinition, jobToRun: JobToRun, respawnWorker: Function): Promise<void>;
|
|
20
|
-
}
|
package/lib/services/Executor.js
DELETED
|
@@ -1,195 +0,0 @@
|
|
|
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
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.Executor = void 0;
|
|
13
|
-
const logger_1 = require("@orion-js/logger");
|
|
14
|
-
const services_1 = require("@orion-js/services");
|
|
15
|
-
const JobsHistoryRepo_1 = require("../repos/JobsHistoryRepo");
|
|
16
|
-
const JobsRepo_1 = require("../repos/JobsRepo");
|
|
17
|
-
const getNextRunDate_1 = require("./getNextRunDate");
|
|
18
|
-
const api_1 = require("@opentelemetry/api");
|
|
19
|
-
let Executor = class Executor {
|
|
20
|
-
getContext(job, jobToRun, onStale) {
|
|
21
|
-
let staleTimeout = setTimeout(() => onStale(), jobToRun.lockTime);
|
|
22
|
-
return {
|
|
23
|
-
definition: job,
|
|
24
|
-
record: jobToRun,
|
|
25
|
-
tries: jobToRun.tries || 0,
|
|
26
|
-
clearStaleTimeout: () => clearTimeout(staleTimeout),
|
|
27
|
-
extendLockTime: async (extraTime) => {
|
|
28
|
-
clearTimeout(staleTimeout);
|
|
29
|
-
staleTimeout = setTimeout(() => onStale(), extraTime);
|
|
30
|
-
await this.jobsRepo.extendLockTime(jobToRun.jobId, extraTime);
|
|
31
|
-
},
|
|
32
|
-
logger: logger_1.logger.addMetadata({
|
|
33
|
-
jobName: jobToRun.name,
|
|
34
|
-
jobId: jobToRun.jobId,
|
|
35
|
-
}),
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
getJobDefinition(jobToRun, jobs) {
|
|
39
|
-
const job = jobs[jobToRun.name];
|
|
40
|
-
if (jobToRun.type !== job.type) {
|
|
41
|
-
logger_1.logger.warn(`Job record "${jobToRun.name}" is "${jobToRun.type}" but definition is "${job.type}"`);
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
return job;
|
|
45
|
-
}
|
|
46
|
-
async onError(error, job, jobToRun, context) {
|
|
47
|
-
const scheduleRecurrent = async () => {
|
|
48
|
-
if (job.type === 'recurrent') {
|
|
49
|
-
await this.jobsRepo.scheduleNextRun({
|
|
50
|
-
jobId: jobToRun.jobId,
|
|
51
|
-
nextRunAt: (0, getNextRunDate_1.getNextRunDate)(job),
|
|
52
|
-
addTries: false,
|
|
53
|
-
priority: job.priority,
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
if (!job.onError) {
|
|
58
|
-
context.logger.error(`Error executing job "${jobToRun.name}"`, error);
|
|
59
|
-
await scheduleRecurrent();
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
context.logger.info(`Error executing job "${jobToRun.name}"`, error);
|
|
63
|
-
const result = await job.onError(error, jobToRun.params, context);
|
|
64
|
-
if (result.action === 'dismiss') {
|
|
65
|
-
await scheduleRecurrent();
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
if (result.action === 'retry') {
|
|
69
|
-
await this.jobsRepo.scheduleNextRun({
|
|
70
|
-
jobId: jobToRun.jobId,
|
|
71
|
-
nextRunAt: (0, getNextRunDate_1.getNextRunDate)(result),
|
|
72
|
-
addTries: true,
|
|
73
|
-
priority: job.type === 'recurrent' ? job.priority : jobToRun.priority,
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
async saveExecution(options) {
|
|
78
|
-
const { startedAt, status, errorMessage, result, job, jobToRun } = options;
|
|
79
|
-
const endedAt = new Date();
|
|
80
|
-
if (job.saveExecutionsFor !== 0) {
|
|
81
|
-
const oneWeek = 1000 * 60 * 60 * 24 * 7;
|
|
82
|
-
const saveExecutionsFor = job.saveExecutionsFor || oneWeek;
|
|
83
|
-
await this.jobsHistoryRepo.saveExecution({
|
|
84
|
-
jobId: jobToRun.jobId,
|
|
85
|
-
executionId: jobToRun.executionId,
|
|
86
|
-
jobName: jobToRun.name,
|
|
87
|
-
type: jobToRun.type,
|
|
88
|
-
priority: jobToRun.priority,
|
|
89
|
-
tries: jobToRun.tries,
|
|
90
|
-
uniqueIdentifier: jobToRun.uniqueIdentifier,
|
|
91
|
-
startedAt,
|
|
92
|
-
endedAt,
|
|
93
|
-
duration: endedAt.getTime() - startedAt.getTime(),
|
|
94
|
-
expiresAt: new Date(Date.now() + saveExecutionsFor),
|
|
95
|
-
status,
|
|
96
|
-
errorMessage,
|
|
97
|
-
params: jobToRun.params,
|
|
98
|
-
result,
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
async afterExecutionSuccess(job, jobToRun, context) {
|
|
103
|
-
if (job.type === 'recurrent') {
|
|
104
|
-
context.logger.debug(`Scheduling next run for recurrent job "${jobToRun.name}"`);
|
|
105
|
-
await this.jobsRepo.scheduleNextRun({
|
|
106
|
-
jobId: jobToRun.jobId,
|
|
107
|
-
nextRunAt: (0, getNextRunDate_1.getNextRunDate)(job),
|
|
108
|
-
addTries: false,
|
|
109
|
-
priority: job.priority,
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
if (job.type === 'event') {
|
|
113
|
-
context.logger.debug(`Removing event job after success "${jobToRun.name}"`);
|
|
114
|
-
await this.jobsRepo.deleteEventJob(jobToRun.jobId);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
async executeJob(jobs, jobToRun, respawnWorker) {
|
|
118
|
-
const job = this.getJobDefinition(jobToRun, jobs);
|
|
119
|
-
if (!job)
|
|
120
|
-
return;
|
|
121
|
-
const tracer = api_1.trace.getTracer('orionjs.dogs', '1.0');
|
|
122
|
-
await tracer.startActiveSpan(`job.${jobToRun.name}.${jobToRun.executionId}`, async (span) => {
|
|
123
|
-
try {
|
|
124
|
-
const startedAt = new Date();
|
|
125
|
-
const onStale = async () => {
|
|
126
|
-
if (job.onStale) {
|
|
127
|
-
context.logger.info(`Job "${jobToRun.name}" is stale`);
|
|
128
|
-
job.onStale(jobToRun.params, context);
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
context.logger.error(`Job "${jobToRun.name}" is stale`);
|
|
132
|
-
}
|
|
133
|
-
await this.jobsRepo.setJobRecordPriority(jobToRun.jobId, 0);
|
|
134
|
-
respawnWorker();
|
|
135
|
-
this.saveExecution({
|
|
136
|
-
startedAt,
|
|
137
|
-
status: 'stale',
|
|
138
|
-
result: null,
|
|
139
|
-
errorMessage: null,
|
|
140
|
-
job,
|
|
141
|
-
jobToRun,
|
|
142
|
-
});
|
|
143
|
-
};
|
|
144
|
-
const context = this.getContext(job, jobToRun, onStale);
|
|
145
|
-
try {
|
|
146
|
-
const result = await job.resolve(jobToRun.params, context);
|
|
147
|
-
context.clearStaleTimeout();
|
|
148
|
-
this.saveExecution({
|
|
149
|
-
startedAt,
|
|
150
|
-
status: 'success',
|
|
151
|
-
result: result || null,
|
|
152
|
-
errorMessage: null,
|
|
153
|
-
job,
|
|
154
|
-
jobToRun,
|
|
155
|
-
});
|
|
156
|
-
await this.afterExecutionSuccess(job, jobToRun, context);
|
|
157
|
-
}
|
|
158
|
-
catch (error) {
|
|
159
|
-
context.clearStaleTimeout();
|
|
160
|
-
this.saveExecution({
|
|
161
|
-
startedAt,
|
|
162
|
-
status: 'error',
|
|
163
|
-
result: null,
|
|
164
|
-
errorMessage: error.message,
|
|
165
|
-
job,
|
|
166
|
-
jobToRun,
|
|
167
|
-
});
|
|
168
|
-
await this.onError(error, job, jobToRun, context);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
catch (error) {
|
|
172
|
-
span.setStatus({
|
|
173
|
-
code: api_1.SpanStatusCode.ERROR,
|
|
174
|
-
message: error.message,
|
|
175
|
-
});
|
|
176
|
-
throw error;
|
|
177
|
-
}
|
|
178
|
-
finally {
|
|
179
|
-
span.end();
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
exports.Executor = Executor;
|
|
185
|
-
__decorate([
|
|
186
|
-
(0, services_1.Inject)(),
|
|
187
|
-
__metadata("design:type", JobsRepo_1.JobsRepo)
|
|
188
|
-
], Executor.prototype, "jobsRepo", void 0);
|
|
189
|
-
__decorate([
|
|
190
|
-
(0, services_1.Inject)(),
|
|
191
|
-
__metadata("design:type", JobsHistoryRepo_1.JobsHistoryRepo)
|
|
192
|
-
], Executor.prototype, "jobsHistoryRepo", void 0);
|
|
193
|
-
exports.Executor = Executor = __decorate([
|
|
194
|
-
(0, services_1.Service)()
|
|
195
|
-
], Executor);
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { JobDefinitionWithName, JobsDefinition } from '../types/JobsDefinition';
|
|
2
|
-
import { StartWorkersConfig } from '../types/StartConfig';
|
|
3
|
-
import { WorkerInstance, WorkersInstance } from '../types/Worker';
|
|
4
|
-
export declare class WorkerService {
|
|
5
|
-
private jobsRepo;
|
|
6
|
-
private executor;
|
|
7
|
-
getJobNames(jobs: JobsDefinition): string[];
|
|
8
|
-
getJobs(jobs: JobsDefinition): JobDefinitionWithName[];
|
|
9
|
-
runWorkerLoop(config: StartWorkersConfig, workerInstance: WorkerInstance): Promise<boolean>;
|
|
10
|
-
startWorker(config: StartWorkersConfig, workerInstance: WorkerInstance): Promise<void>;
|
|
11
|
-
createWorkersInstanceDefinition(config: StartWorkersConfig): WorkersInstance;
|
|
12
|
-
ensureRecords(config: StartWorkersConfig): Promise<void>;
|
|
13
|
-
startANewWorker(config: StartWorkersConfig, workersInstance: WorkersInstance): Promise<void>;
|
|
14
|
-
runWorkers(config: StartWorkersConfig, workersInstance: WorkersInstance): Promise<void>;
|
|
15
|
-
startWorkers(userConfig: Partial<StartWorkersConfig>): WorkersInstance;
|
|
16
|
-
}
|