@orion-js/dogs 3.2.1 → 3.2.4
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/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/repos/JobsHistoryRepo.d.ts +1 -1
- package/lib/repos/JobsHistoryRepo.js +3 -3
- package/lib/repos/JobsRepo.d.ts +1 -1
- package/lib/repos/JobsRepo.js +10 -10
- package/lib/service/index.d.ts +9 -0
- package/lib/service/index.js +34 -0
- package/lib/service/index.test.d.ts +1 -0
- package/lib/service/index.test.js +37 -0
- package/package.json +4 -4
package/lib/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { ScheduleJobOptions } from './types/Events';
|
|
|
4
4
|
import { JobsHistoryRepo } from './repos/JobsHistoryRepo';
|
|
5
5
|
import { JobsRepo } from './repos/JobsRepo';
|
|
6
6
|
export * from './types';
|
|
7
|
+
export * from './service';
|
|
7
8
|
declare const jobsHistoryRepo: JobsHistoryRepo;
|
|
8
9
|
declare const jobsRepo: JobsRepo;
|
|
9
10
|
declare const startWorkers: (config: Partial<StartWorkersConfig>) => import("./types").WorkersInstance;
|
package/lib/index.js
CHANGED
|
@@ -19,6 +19,7 @@ const WorkerService_1 = require("./services/WorkerService");
|
|
|
19
19
|
const JobsHistoryRepo_1 = require("./repos/JobsHistoryRepo");
|
|
20
20
|
const JobsRepo_1 = require("./repos/JobsRepo");
|
|
21
21
|
__exportStar(require("./types"), exports);
|
|
22
|
+
__exportStar(require("./service"), exports);
|
|
22
23
|
const workerService = (0, services_1.getInstance)(WorkerService_1.WorkerService);
|
|
23
24
|
const eventsService = (0, services_1.getInstance)(EventsService_1.EventsService);
|
|
24
25
|
const jobsHistoryRepo = (0, services_1.getInstance)(JobsHistoryRepo_1.JobsHistoryRepo);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ModelToDocumentTypeWithoutId } from '@orion-js/mongodb';
|
|
2
2
|
import { HistoryRecord } from '../types/HistoryRecord';
|
|
3
3
|
export declare class JobsHistoryRepo {
|
|
4
|
-
history: import("@orion-js/mongodb").Collection<HistoryRecord>;
|
|
4
|
+
history: () => import("@orion-js/mongodb").Collection<HistoryRecord>;
|
|
5
5
|
saveExecution(record: ModelToDocumentTypeWithoutId<HistoryRecord>): Promise<void>;
|
|
6
6
|
getExecutions(jobName: string, limit?: number, skip?: number): Promise<HistoryRecord[]>;
|
|
7
7
|
}
|
|
@@ -13,7 +13,7 @@ const lodash_1 = require("lodash");
|
|
|
13
13
|
const HistoryRecord_1 = require("../types/HistoryRecord");
|
|
14
14
|
let JobsHistoryRepo = class JobsHistoryRepo {
|
|
15
15
|
constructor() {
|
|
16
|
-
this.history = (0, mongodb_1.createCollection)({
|
|
16
|
+
this.history = () => (0, mongodb_1.createCollection)({
|
|
17
17
|
name: 'orionjs.jobs_dogs_history',
|
|
18
18
|
model: HistoryRecord_1.HistoryRecord,
|
|
19
19
|
indexes: [
|
|
@@ -35,7 +35,7 @@ let JobsHistoryRepo = class JobsHistoryRepo {
|
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
async saveExecution(record) {
|
|
38
|
-
await this.history.upsert({ executionId: record.executionId }, {
|
|
38
|
+
await this.history().upsert({ executionId: record.executionId }, {
|
|
39
39
|
$setOnInsert: {
|
|
40
40
|
status: record.status
|
|
41
41
|
},
|
|
@@ -45,7 +45,7 @@ let JobsHistoryRepo = class JobsHistoryRepo {
|
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
async getExecutions(jobName, limit, skip) {
|
|
48
|
-
const cursor = this.history.find({ jobName }).sort({ startedAt: -1 });
|
|
48
|
+
const cursor = this.history().find({ jobName }).sort({ startedAt: -1 });
|
|
49
49
|
if (skip) {
|
|
50
50
|
cursor.skip(skip);
|
|
51
51
|
}
|
package/lib/repos/JobsRepo.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { JobRecord } from '../types/JobRecord';
|
|
|
3
3
|
import { JobDefinitionWithName } from '../types/JobsDefinition';
|
|
4
4
|
import { JobToRun } from '../types/Worker';
|
|
5
5
|
export declare class JobsRepo {
|
|
6
|
-
jobs: import("@orion-js/mongodb").Collection<JobRecord>;
|
|
6
|
+
jobs: () => import("@orion-js/mongodb").Collection<JobRecord>;
|
|
7
7
|
getJobAndLock(jobNames: string[], lockTime: number): Promise<JobToRun>;
|
|
8
8
|
setJobRecordPriority(jobId: string, priority: number): Promise<void>;
|
|
9
9
|
scheduleNextRun(options: {
|
package/lib/repos/JobsRepo.js
CHANGED
|
@@ -15,7 +15,7 @@ const lodash_1 = require("lodash");
|
|
|
15
15
|
const JobRecord_1 = require("../types/JobRecord");
|
|
16
16
|
let JobsRepo = class JobsRepo {
|
|
17
17
|
constructor() {
|
|
18
|
-
this.jobs = (0, mongodb_1.createCollection)({
|
|
18
|
+
this.jobs = () => (0, mongodb_1.createCollection)({
|
|
19
19
|
name: 'orionjs.jobs_dogs_records',
|
|
20
20
|
model: JobRecord_1.JobRecord,
|
|
21
21
|
indexes: [
|
|
@@ -50,7 +50,7 @@ let JobsRepo = class JobsRepo {
|
|
|
50
50
|
}
|
|
51
51
|
async getJobAndLock(jobNames, lockTime) {
|
|
52
52
|
const lockedUntil = new Date(Date.now() + lockTime);
|
|
53
|
-
const job = await this.jobs.findOneAndUpdate({
|
|
53
|
+
const job = await this.jobs().findOneAndUpdate({
|
|
54
54
|
jobName: { $in: jobNames },
|
|
55
55
|
nextRunAt: { $lte: new Date() },
|
|
56
56
|
$or: [{ lockedUntil: { $exists: false } }, { lockedUntil: { $lte: new Date() } }]
|
|
@@ -70,7 +70,7 @@ let JobsRepo = class JobsRepo {
|
|
|
70
70
|
let tries = job.tries || 1;
|
|
71
71
|
if (job.lockedUntil) {
|
|
72
72
|
logger_1.logger.info(`Running job "${job.jobName}" that was staled`);
|
|
73
|
-
this.jobs.updateOne(job._id, { $inc: { tries: 1 } });
|
|
73
|
+
this.jobs().updateOne(job._id, { $inc: { tries: 1 } });
|
|
74
74
|
tries++;
|
|
75
75
|
}
|
|
76
76
|
return {
|
|
@@ -86,7 +86,7 @@ let JobsRepo = class JobsRepo {
|
|
|
86
86
|
};
|
|
87
87
|
}
|
|
88
88
|
async setJobRecordPriority(jobId, priority) {
|
|
89
|
-
await this.jobs.updateOne(jobId, { $set: { priority } });
|
|
89
|
+
await this.jobs().updateOne(jobId, { $set: { priority } });
|
|
90
90
|
}
|
|
91
91
|
async scheduleNextRun(options) {
|
|
92
92
|
const updator = {
|
|
@@ -96,22 +96,22 @@ let JobsRepo = class JobsRepo {
|
|
|
96
96
|
if (options.addTries) {
|
|
97
97
|
updator.$inc = { tries: 1 };
|
|
98
98
|
}
|
|
99
|
-
await this.jobs.updateOne(options.jobId, updator);
|
|
99
|
+
await this.jobs().updateOne(options.jobId, updator);
|
|
100
100
|
}
|
|
101
101
|
async deleteEventJob(jobId) {
|
|
102
|
-
await this.jobs.deleteOne({ _id: jobId, type: 'event' });
|
|
102
|
+
await this.jobs().deleteOne({ _id: jobId, type: 'event' });
|
|
103
103
|
}
|
|
104
104
|
async extendLockTime(jobId, extraTime) {
|
|
105
105
|
const lockedUntil = new Date(Date.now() + extraTime);
|
|
106
|
-
await this.jobs.updateOne({
|
|
106
|
+
await this.jobs().updateOne({
|
|
107
107
|
_id: jobId
|
|
108
108
|
}, {
|
|
109
109
|
$set: { lockedUntil }
|
|
110
110
|
});
|
|
111
111
|
}
|
|
112
112
|
async ensureJobRecord(job) {
|
|
113
|
-
await this.jobs.connectionPromise;
|
|
114
|
-
const result = await this.jobs.upsert({
|
|
113
|
+
await this.jobs().connectionPromise;
|
|
114
|
+
const result = await this.jobs().upsert({
|
|
115
115
|
jobName: job.name
|
|
116
116
|
}, {
|
|
117
117
|
$set: {
|
|
@@ -131,7 +131,7 @@ let JobsRepo = class JobsRepo {
|
|
|
131
131
|
}
|
|
132
132
|
async scheduleJob(options) {
|
|
133
133
|
try {
|
|
134
|
-
await this.jobs.insertOne({
|
|
134
|
+
await this.jobs().insertOne({
|
|
135
135
|
jobName: options.name,
|
|
136
136
|
uniqueIdentifier: options.uniqueIdentifier,
|
|
137
137
|
params: options.params,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { JobDefinition } 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 Job(options: Omit<JobDefinition, 'resolve'>): (target: any, propertyKey: string, descriptor: JobsPropertyDescriptor) => void;
|
|
7
|
+
export declare function getServiceJobs(target: any): {
|
|
8
|
+
[key: string]: JobDefinition;
|
|
9
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getServiceJobs = exports.Job = exports.Jobs = void 0;
|
|
4
|
+
const services_1 = require("@orion-js/services");
|
|
5
|
+
const defineJob_1 = require("../defineJob");
|
|
6
|
+
function Jobs() {
|
|
7
|
+
return function (target) {
|
|
8
|
+
(0, services_1.Service)()(target);
|
|
9
|
+
target.prototype.service = target;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
exports.Jobs = Jobs;
|
|
13
|
+
function Job(options) {
|
|
14
|
+
return function (target, propertyKey, descriptor) {
|
|
15
|
+
if (!descriptor.value)
|
|
16
|
+
throw new Error(`You must pass resolver function to ${propertyKey}`);
|
|
17
|
+
target.echoes = target.echoes || {};
|
|
18
|
+
target.echoes[propertyKey] = (0, defineJob_1.defineJob)({
|
|
19
|
+
...options,
|
|
20
|
+
resolve: async (params, viewer) => {
|
|
21
|
+
const instance = (0, services_1.getInstance)(target.service);
|
|
22
|
+
return await instance[propertyKey](params, viewer);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
exports.Job = Job;
|
|
28
|
+
function getServiceJobs(target) {
|
|
29
|
+
if (!target.prototype) {
|
|
30
|
+
throw new Error('You must pass a class to getServiceRoutes');
|
|
31
|
+
}
|
|
32
|
+
return target.prototype.echoes || {};
|
|
33
|
+
}
|
|
34
|
+
exports.getServiceJobs = getServiceJobs;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
};
|
|
20
|
+
__decorate([
|
|
21
|
+
(0, _1.Job)({ type: 'event' }),
|
|
22
|
+
__metadata("design:type", Function),
|
|
23
|
+
__metadata("design:paramtypes", []),
|
|
24
|
+
__metadata("design:returntype", Promise)
|
|
25
|
+
], ExampleJobsService.prototype, "job1", null);
|
|
26
|
+
ExampleJobsService = __decorate([
|
|
27
|
+
(0, _1.Jobs)()
|
|
28
|
+
], ExampleJobsService);
|
|
29
|
+
const jobs = (0, _1.getServiceJobs)(ExampleJobsService);
|
|
30
|
+
expect(jobs).toMatchObject({
|
|
31
|
+
job1: {
|
|
32
|
+
type: 'event',
|
|
33
|
+
resolve: expect.any(Function)
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/dogs",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.4",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@orion-js/helpers": "^3.2.0",
|
|
21
|
-
"@orion-js/mongodb": "^3.2.
|
|
21
|
+
"@orion-js/mongodb": "^3.2.4",
|
|
22
22
|
"@orion-js/services": "^3.2.0",
|
|
23
|
-
"@orion-js/typed-model": "^3.2.
|
|
23
|
+
"@orion-js/typed-model": "^3.2.4"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"@orion-js/logger": "3.1.0-alpha.12"
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"publishConfig": {
|
|
40
40
|
"access": "public"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "a1c4cff581b31f24896ec96a0a180d043dbf802a"
|
|
43
43
|
}
|