@orion-js/dogs 3.11.6 → 3.11.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/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -61,7 +61,7 @@ let JobsHistoryRepo = class JobsHistoryRepo {
61
61
  return await cursor.toArray();
62
62
  }
63
63
  };
64
- JobsHistoryRepo = __decorate([
64
+ exports.JobsHistoryRepo = JobsHistoryRepo;
65
+ exports.JobsHistoryRepo = JobsHistoryRepo = __decorate([
65
66
  (0, services_1.Service)()
66
67
  ], JobsHistoryRepo);
67
- exports.JobsHistoryRepo = JobsHistoryRepo;
@@ -152,7 +152,7 @@ let JobsRepo = class JobsRepo {
152
152
  }
153
153
  }
154
154
  };
155
- JobsRepo = __decorate([
155
+ exports.JobsRepo = JobsRepo;
156
+ exports.JobsRepo = JobsRepo = __decorate([
156
157
  (0, services_1.Service)()
157
158
  ], JobsRepo);
158
- exports.JobsRepo = JobsRepo;
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getServiceJobs = exports.EventJob = exports.RecurrentJob = exports.Jobs = void 0;
3
+ exports.Jobs = Jobs;
4
+ exports.RecurrentJob = RecurrentJob;
5
+ exports.EventJob = EventJob;
6
+ exports.getServiceJobs = getServiceJobs;
4
7
  const services_1 = require("@orion-js/services");
5
8
  const defineJob_1 = require("../defineJob");
6
9
  function Jobs() {
@@ -9,7 +12,6 @@ function Jobs() {
9
12
  target.prototype.service = target;
10
13
  };
11
14
  }
12
- exports.Jobs = Jobs;
13
15
  function RecurrentJob(options) {
14
16
  return function (target, propertyKey, descriptor) {
15
17
  if (!descriptor.value)
@@ -25,7 +27,6 @@ function RecurrentJob(options) {
25
27
  });
26
28
  };
27
29
  }
28
- exports.RecurrentJob = RecurrentJob;
29
30
  function EventJob(options = {}) {
30
31
  return function (target, propertyKey, descriptor) {
31
32
  if (!descriptor.value)
@@ -41,11 +42,9 @@ function EventJob(options = {}) {
41
42
  });
42
43
  };
43
44
  }
44
- exports.EventJob = EventJob;
45
45
  function getServiceJobs(target) {
46
46
  if (!target.prototype) {
47
47
  throw new Error('You must pass a class to getServiceRoutes');
48
48
  }
49
49
  return target.prototype.echoes || {};
50
50
  }
51
- exports.getServiceJobs = getServiceJobs;
@@ -26,11 +26,11 @@ let EventsService = class EventsService {
26
26
  });
27
27
  }
28
28
  };
29
+ exports.EventsService = EventsService;
29
30
  __decorate([
30
31
  (0, services_1.Inject)(),
31
32
  __metadata("design:type", JobsRepo_1.JobsRepo)
32
33
  ], EventsService.prototype, "jobsRepo", void 0);
33
- EventsService = __decorate([
34
+ exports.EventsService = EventsService = __decorate([
34
35
  (0, services_1.Service)()
35
36
  ], EventsService);
36
- exports.EventsService = EventsService;
@@ -2,8 +2,8 @@ import { PlainObject } from '../types/HistoryRecord';
2
2
  import { JobDefinition, JobsDefinition } from '../types/JobsDefinition';
3
3
  import { ExecutionContext, JobToRun } from '../types/Worker';
4
4
  export declare class Executor {
5
- private jobsRepo;
6
- private jobsHistoryRepo;
5
+ private readonly jobsRepo;
6
+ private readonly jobsHistoryRepo;
7
7
  getContext(job: JobDefinition, jobToRun: JobToRun, onStale: Function): ExecutionContext;
8
8
  getJobDefinition(jobToRun: JobToRun, jobs: JobsDefinition): JobDefinition;
9
9
  onError(error: any, job: JobDefinition, jobToRun: JobToRun, context: ExecutionContext): Promise<void>;
@@ -15,6 +15,7 @@ const services_1 = require("@orion-js/services");
15
15
  const JobsHistoryRepo_1 = require("../repos/JobsHistoryRepo");
16
16
  const JobsRepo_1 = require("../repos/JobsRepo");
17
17
  const getNextRunDate_1 = require("./getNextRunDate");
18
+ const api_1 = require("@opentelemetry/api");
18
19
  let Executor = class Executor {
19
20
  getContext(job, jobToRun, onStale) {
20
21
  let staleTimeout = setTimeout(() => onStale(), jobToRun.lockTime);
@@ -30,8 +31,8 @@ let Executor = class Executor {
30
31
  },
31
32
  logger: logger_1.logger.addMetadata({
32
33
  jobName: jobToRun.name,
33
- jobId: jobToRun.jobId
34
- })
34
+ jobId: jobToRun.jobId,
35
+ }),
35
36
  };
36
37
  }
37
38
  getJobDefinition(jobToRun, jobs) {
@@ -49,7 +50,7 @@ let Executor = class Executor {
49
50
  jobId: jobToRun.jobId,
50
51
  nextRunAt: (0, getNextRunDate_1.getNextRunDate)(job),
51
52
  addTries: false,
52
- priority: job.priority
53
+ priority: job.priority,
53
54
  });
54
55
  }
55
56
  };
@@ -58,9 +59,7 @@ let Executor = class Executor {
58
59
  await scheduleRecurrent();
59
60
  return;
60
61
  }
61
- else {
62
- context.logger.info(`Error executing job "${jobToRun.name}"`, error);
63
- }
62
+ context.logger.info(`Error executing job "${jobToRun.name}"`, error);
64
63
  const result = await job.onError(error, jobToRun.params, context);
65
64
  if (result.action === 'dismiss') {
66
65
  await scheduleRecurrent();
@@ -71,7 +70,7 @@ let Executor = class Executor {
71
70
  jobId: jobToRun.jobId,
72
71
  nextRunAt: (0, getNextRunDate_1.getNextRunDate)(result),
73
72
  addTries: true,
74
- priority: job.type === 'recurrent' ? job.priority : jobToRun.priority
73
+ priority: job.type === 'recurrent' ? job.priority : jobToRun.priority,
75
74
  });
76
75
  }
77
76
  }
@@ -96,7 +95,7 @@ let Executor = class Executor {
96
95
  status,
97
96
  errorMessage,
98
97
  params: jobToRun.params,
99
- result
98
+ result,
100
99
  });
101
100
  }
102
101
  }
@@ -107,7 +106,7 @@ let Executor = class Executor {
107
106
  jobId: jobToRun.jobId,
108
107
  nextRunAt: (0, getNextRunDate_1.getNextRunDate)(job),
109
108
  addTries: false,
110
- priority: job.priority
109
+ priority: job.priority,
111
110
  });
112
111
  }
113
112
  if (job.type === 'event') {
@@ -119,54 +118,70 @@ let Executor = class Executor {
119
118
  const job = this.getJobDefinition(jobToRun, jobs);
120
119
  if (!job)
121
120
  return;
122
- const startedAt = new Date();
123
- const onStale = async () => {
124
- if (job.onStale) {
125
- context.logger.info(`Job "${jobToRun.name}" is stale`);
126
- job.onStale(jobToRun.params, context);
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
+ }
127
170
  }
128
- else {
129
- context.logger.error(`Job "${jobToRun.name}" is stale`);
171
+ catch (error) {
172
+ span.setStatus({
173
+ code: api_1.SpanStatusCode.ERROR,
174
+ message: error.message,
175
+ });
176
+ throw error;
130
177
  }
131
- await this.jobsRepo.setJobRecordPriority(jobToRun.jobId, 0);
132
- respawnWorker();
133
- this.saveExecution({
134
- startedAt,
135
- status: 'stale',
136
- result: null,
137
- errorMessage: null,
138
- job,
139
- jobToRun
140
- });
141
- };
142
- const context = this.getContext(job, jobToRun, onStale);
143
- try {
144
- const result = await job.resolve(jobToRun.params, context);
145
- context.clearStaleTimeout();
146
- this.saveExecution({
147
- startedAt,
148
- status: 'success',
149
- result: result || null,
150
- errorMessage: null,
151
- job,
152
- jobToRun
153
- });
154
- await this.afterExecutionSuccess(job, jobToRun, context);
155
- }
156
- catch (error) {
157
- context.clearStaleTimeout();
158
- this.saveExecution({
159
- startedAt,
160
- status: 'error',
161
- result: null,
162
- errorMessage: error.message,
163
- job,
164
- jobToRun
165
- });
166
- await this.onError(error, job, jobToRun, context);
167
- }
178
+ finally {
179
+ span.end();
180
+ }
181
+ });
168
182
  }
169
183
  };
184
+ exports.Executor = Executor;
170
185
  __decorate([
171
186
  (0, services_1.Inject)(),
172
187
  __metadata("design:type", JobsRepo_1.JobsRepo)
@@ -175,7 +190,6 @@ __decorate([
175
190
  (0, services_1.Inject)(),
176
191
  __metadata("design:type", JobsHistoryRepo_1.JobsHistoryRepo)
177
192
  ], Executor.prototype, "jobsHistoryRepo", void 0);
178
- Executor = __decorate([
193
+ exports.Executor = Executor = __decorate([
179
194
  (0, services_1.Service)()
180
195
  ], Executor);
181
- exports.Executor = Executor;
@@ -128,6 +128,7 @@ let WorkerService = class WorkerService {
128
128
  return workersInstance;
129
129
  }
130
130
  };
131
+ exports.WorkerService = WorkerService;
131
132
  __decorate([
132
133
  (0, services_1.Inject)(),
133
134
  __metadata("design:type", JobsRepo_1.JobsRepo)
@@ -136,7 +137,6 @@ __decorate([
136
137
  (0, services_1.Inject)(),
137
138
  __metadata("design:type", Executor_1.Executor)
138
139
  ], WorkerService.prototype, "executor", void 0);
139
- WorkerService = __decorate([
140
+ exports.WorkerService = WorkerService = __decorate([
140
141
  (0, services_1.Service)()
141
142
  ], WorkerService);
142
- exports.WorkerService = WorkerService;
@@ -1,4 +1,4 @@
1
- export declare type Options = {
1
+ export type Options = {
2
2
  getNextRun?: () => Date;
3
3
  runIn?: number;
4
4
  runEvery?: number;
@@ -5,13 +5,13 @@ export interface ScheduleJobOptionsBase {
5
5
  priority?: number;
6
6
  uniqueIdentifier?: string;
7
7
  }
8
- export declare type ScheduleJobOptionsRunIn = ScheduleJobOptionsBase & {
8
+ export type ScheduleJobOptionsRunIn = ScheduleJobOptionsBase & {
9
9
  runIn: number;
10
10
  };
11
- export declare type ScheduleJobOptionsRunAt = ScheduleJobOptionsBase & {
11
+ export type ScheduleJobOptionsRunAt = ScheduleJobOptionsBase & {
12
12
  runAt: Date;
13
13
  };
14
- export declare type ScheduleJobOptions = ScheduleJobOptionsRunIn | ScheduleJobOptionsRunAt | ScheduleJobOptionsBase;
14
+ export type ScheduleJobOptions = ScheduleJobOptionsRunIn | ScheduleJobOptionsRunAt | ScheduleJobOptionsBase;
15
15
  export interface ScheduleJobRecordOptions {
16
16
  name: string;
17
17
  params: PlainObject;
@@ -1,4 +1,4 @@
1
- export declare type PlainObject = {
1
+ export type PlainObject = {
2
2
  [name: string]: any;
3
3
  };
4
4
  export declare class HistoryRecord {
@@ -13,6 +13,7 @@ exports.HistoryRecord = void 0;
13
13
  const typed_model_1 = require("@orion-js/typed-model");
14
14
  let HistoryRecord = class HistoryRecord {
15
15
  };
16
+ exports.HistoryRecord = HistoryRecord;
16
17
  __decorate([
17
18
  (0, typed_model_1.Prop)(),
18
19
  __metadata("design:type", String)
@@ -77,7 +78,6 @@ __decorate([
77
78
  (0, typed_model_1.Prop)({ type: 'blackbox', optional: true }),
78
79
  __metadata("design:type", Object)
79
80
  ], HistoryRecord.prototype, "result", void 0);
80
- HistoryRecord = __decorate([
81
+ exports.HistoryRecord = HistoryRecord = __decorate([
81
82
  (0, typed_model_1.TypedModel)()
82
83
  ], HistoryRecord);
83
- exports.HistoryRecord = HistoryRecord;
@@ -13,6 +13,7 @@ exports.JobRecord = void 0;
13
13
  const typed_model_1 = require("@orion-js/typed-model");
14
14
  let JobRecord = class JobRecord {
15
15
  };
16
+ exports.JobRecord = JobRecord;
16
17
  __decorate([
17
18
  (0, typed_model_1.Prop)(),
18
19
  __metadata("design:type", String)
@@ -53,7 +54,6 @@ __decorate([
53
54
  (0, typed_model_1.Prop)({ type: 'blackbox', optional: true }),
54
55
  __metadata("design:type", Object)
55
56
  ], JobRecord.prototype, "params", void 0);
56
- JobRecord = __decorate([
57
+ exports.JobRecord = JobRecord = __decorate([
57
58
  (0, typed_model_1.TypedModel)()
58
59
  ], JobRecord);
59
- exports.JobRecord = JobRecord;
@@ -3,13 +3,13 @@ import { ExecutionContext } from './Worker';
3
3
  export interface JobRetryResultBase {
4
4
  action: 'retry' | 'dismiss';
5
5
  }
6
- export declare type JobRetryResultRunIn = JobRetryResultBase & {
6
+ export type JobRetryResultRunIn = JobRetryResultBase & {
7
7
  runIn: number;
8
8
  };
9
- export declare type JobRetryResultRunAt = JobRetryResultBase & {
9
+ export type JobRetryResultRunAt = JobRetryResultBase & {
10
10
  runAt: Date;
11
11
  };
12
- export declare type JobRetryResult = JobRetryResultRunIn | JobRetryResultRunAt | JobRetryResultBase;
12
+ export type JobRetryResult = JobRetryResultRunIn | JobRetryResultRunAt | JobRetryResultBase;
13
13
  export interface BaseJobDefinition {
14
14
  /**
15
15
  * The function to execute when the job is executed.
@@ -52,8 +52,8 @@ export interface EventJobDefinition extends BaseJobDefinition {
52
52
  */
53
53
  type: 'event';
54
54
  }
55
- export declare type JobDefinition = RecurrentJobDefinition | EventJobDefinition;
56
- export declare type JobDefinitionWithName = JobDefinition & {
55
+ export type JobDefinition = RecurrentJobDefinition | EventJobDefinition;
56
+ export type JobDefinitionWithName = JobDefinition & {
57
57
  name: string;
58
58
  };
59
59
  export interface JobsDefinition {
@@ -1,5 +1,5 @@
1
1
  import { JobsDefinition } from './JobsDefinition';
2
- export declare type LogLevels = 'debug' | 'info' | 'warn' | 'error' | 'none';
2
+ export type LogLevels = 'debug' | 'info' | 'warn' | 'error' | 'none';
3
3
  export interface StartWorkersConfig {
4
4
  /**
5
5
  * Object map of the jobs that this workers will execute
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/dogs",
3
- "version": "3.11.6",
3
+ "version": "3.11.8",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "files": [
@@ -17,16 +17,17 @@
17
17
  "upgrade-interactive": "yarn upgrade-interactive"
18
18
  },
19
19
  "dependencies": {
20
- "@orion-js/helpers": "^3.11.6",
21
- "@orion-js/mongodb": "^3.11.6",
22
- "@orion-js/services": "^3.11.6",
23
- "@orion-js/typed-model": "^3.11.6"
20
+ "@opentelemetry/api": "^1.9.0",
21
+ "@orion-js/helpers": "^3.11.8",
22
+ "@orion-js/mongodb": "^3.11.8",
23
+ "@orion-js/services": "^3.11.8",
24
+ "@orion-js/typed-model": "^3.11.8"
24
25
  },
25
26
  "peerDependencies": {
26
27
  "@orion-js/logger": "*"
27
28
  },
28
29
  "devDependencies": {
29
- "@orion-js/logger": "^3.11.6",
30
+ "@orion-js/logger": "^3.11.8",
30
31
  "@shelf/jest-mongodb": "^4.3.2",
31
32
  "@types/jest": "^29.5.12",
32
33
  "@types/lodash": "4.14.176",
@@ -34,10 +35,10 @@
34
35
  "jest": "29.7.0",
35
36
  "reflect-metadata": "^0.1.13",
36
37
  "ts-jest": "29.1.2",
37
- "typescript": "^4.4.4"
38
+ "typescript": "^5.4.5"
38
39
  },
39
40
  "publishConfig": {
40
41
  "access": "public"
41
42
  },
42
- "gitHead": "04323e40315251064616cece5fba54bf378917aa"
43
+ "gitHead": "78d70ac414964ec657cc000cedfad8a5efff5cc3"
43
44
  }