@orion-js/dogs 3.1.0-alpha.5 → 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.
@@ -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 (ranCount === 1) {
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: 1,
90
+ workersCount: 2,
92
91
  pollInterval: 10,
93
92
  cooldownPeriod: 10,
94
- lockTime: 40,
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)(200);
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)();
@@ -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
- isRecurrent: false,
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
- isRecurrent: false,
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
- isRecurrent: false,
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
- isRecurrent: false,
140
+ type: 'event',
141
141
  priority: 1,
142
142
  tries: 1,
143
143
  startedAt: expect.any(Date),
package/lib/index.d.ts CHANGED
@@ -3,8 +3,9 @@ import { StartWorkersConfig } from './types/StartConfig';
3
3
  import { ScheduleJobOptions } from './types/Events';
4
4
  import { JobsHistoryRepo } from './repos/JobsHistoryRepo';
5
5
  import { JobsRepo } from './repos/JobsRepo';
6
+ export * from './types';
6
7
  declare const jobsHistoryRepo: JobsHistoryRepo;
7
8
  declare const jobsRepo: JobsRepo;
8
- declare const startWorkers: (config: Partial<StartWorkersConfig>) => import("./types/Worker").WorkersInstance;
9
+ declare const startWorkers: (config: Partial<StartWorkersConfig>) => import("./types").WorkersInstance;
9
10
  declare const scheduleJob: (options: ScheduleJobOptions) => Promise<void>;
10
11
  export { defineJob, startWorkers, scheduleJob, jobsHistoryRepo, jobsRepo };
package/lib/index.js CHANGED
@@ -1,4 +1,14 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
2
12
  Object.defineProperty(exports, "__esModule", { value: true });
3
13
  exports.jobsRepo = exports.jobsHistoryRepo = exports.scheduleJob = exports.startWorkers = exports.defineJob = void 0;
4
14
  const services_1 = require("@orion-js/services");
@@ -8,6 +18,7 @@ const EventsService_1 = require("./services/EventsService");
8
18
  const WorkerService_1 = require("./services/WorkerService");
9
19
  const JobsHistoryRepo_1 = require("./repos/JobsHistoryRepo");
10
20
  const JobsRepo_1 = require("./repos/JobsRepo");
21
+ __exportStar(require("./types"), exports);
11
22
  const workerService = (0, services_1.getInstance)(WorkerService_1.WorkerService);
12
23
  const eventsService = (0, services_1.getInstance)(EventsService_1.EventsService);
13
24
  const jobsHistoryRepo = (0, services_1.getInstance)(JobsHistoryRepo_1.JobsHistoryRepo);
@@ -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>;
@@ -31,7 +31,7 @@ let JobsRepo = class JobsRepo {
31
31
  },
32
32
  options: {
33
33
  unique: true,
34
- partialFilterExpression: { isRecurrent: true }
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: {
@@ -75,7 +75,7 @@ let JobsRepo = class JobsRepo {
75
75
  jobId: job._id,
76
76
  name: job.jobName,
77
77
  params: job.params,
78
- isRecurrent: job.isRecurrent,
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
- isRecurrent: true,
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
- isRecurrent: false
133
+ type: 'event'
131
134
  });
132
135
  }
133
136
  catch (error) {
@@ -15,5 +15,6 @@ export declare class Executor {
15
15
  job: JobDefinition;
16
16
  jobToRun: JobToRun;
17
17
  }): Promise<void>;
18
+ afterExecutionSuccess(job: JobDefinition, jobToRun: JobToRun): Promise<void>;
18
19
  executeJob(jobs: JobsDefinition, jobToRun: JobToRun): Promise<void>;
19
20
  }
@@ -32,12 +32,8 @@ let Executor = class Executor {
32
32
  }
33
33
  getJobDefinition(jobToRun, jobs) {
34
34
  const job = jobs[jobToRun.name];
35
- if (!jobToRun.isRecurrent && job.type === 'recurrent') {
36
- (0, log_1.log)('warn', `Job record ${jobToRun.name} is event but definition is recurrent`);
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
- isRecurrent: jobToRun.isRecurrent,
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
- if (job.type === 'recurrent') {
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();
@@ -5,7 +5,7 @@ export declare class HistoryRecord {
5
5
  _id: string;
6
6
  executionId: string;
7
7
  jobName: string;
8
- isRecurrent: boolean;
8
+ type: 'recurrent' | 'event';
9
9
  priority: number;
10
10
  tries: number;
11
11
  uniqueIdentifier?: string;
@@ -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", Boolean)
31
- ], HistoryRecord.prototype, "isRecurrent", void 0);
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)
@@ -2,7 +2,7 @@ import { PlainObject } from './HistoryRecord';
2
2
  export declare class JobRecord {
3
3
  _id: string;
4
4
  jobName: string;
5
- isRecurrent: boolean;
5
+ type: 'recurrent' | 'event';
6
6
  priority: number;
7
7
  uniqueIdentifier?: string;
8
8
  nextRunAt: Date;
@@ -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", Boolean)
27
- ], JobRecord.prototype, "isRecurrent", void 0);
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)
@@ -3,7 +3,7 @@ import { JobDefinition } from './JobsDefinition';
3
3
  export interface JobToRun {
4
4
  jobId: string;
5
5
  name: string;
6
- isRecurrent: boolean;
6
+ type: 'event' | 'recurrent';
7
7
  params: PlainObject;
8
8
  tries: number;
9
9
  lockTime: number;
@@ -0,0 +1,6 @@
1
+ export * from './Events';
2
+ export * from './HistoryRecord';
3
+ export * from './JobRecord';
4
+ export * from './JobsDefinition';
5
+ export * from './StartConfig';
6
+ export * from './Worker';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./Events"), exports);
14
+ __exportStar(require("./HistoryRecord"), exports);
15
+ __exportStar(require("./JobRecord"), exports);
16
+ __exportStar(require("./JobsDefinition"), exports);
17
+ __exportStar(require("./StartConfig"), exports);
18
+ __exportStar(require("./Worker"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/dogs",
3
- "version": "3.1.0-alpha.5",
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": "a5a1a46dd58b86b6bfb9523d1352b3d57891cb1c"
38
+ "gitHead": "f11193ee2cc10b9a7c99ef618ae284d319a34de4"
39
39
  }