@orion-js/dogs 3.11.15 → 3.12.0

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.
Files changed (51) hide show
  1. package/dist/index.cjs +114048 -0
  2. package/dist/index.d.ts +215 -0
  3. package/dist/index.js +114022 -0
  4. package/package.json +23 -22
  5. package/LICENSE +0 -21
  6. package/lib/defineJob/index.d.ts +0 -2
  7. package/lib/defineJob/index.js +0 -12
  8. package/lib/events.test.d.ts +0 -1
  9. package/lib/events.test.js +0 -133
  10. package/lib/history.test.d.ts +0 -1
  11. package/lib/history.test.js +0 -140
  12. package/lib/index.d.ts +0 -12
  13. package/lib/index.js +0 -40
  14. package/lib/recurrent.test.d.ts +0 -1
  15. package/lib/recurrent.test.js +0 -47
  16. package/lib/repos/JobsHistoryRepo.d.ts +0 -7
  17. package/lib/repos/JobsHistoryRepo.js +0 -67
  18. package/lib/repos/JobsRepo.d.ts +0 -19
  19. package/lib/repos/JobsRepo.js +0 -158
  20. package/lib/service/index.d.ts +0 -10
  21. package/lib/service/index.js +0 -50
  22. package/lib/service/index.test.d.ts +0 -1
  23. package/lib/service/index.test.js +0 -51
  24. package/lib/services/EventsService.d.ts +0 -5
  25. package/lib/services/EventsService.js +0 -36
  26. package/lib/services/Executor.d.ts +0 -20
  27. package/lib/services/Executor.js +0 -195
  28. package/lib/services/WorkerService.d.ts +0 -16
  29. package/lib/services/WorkerService.js +0 -142
  30. package/lib/services/WorkerService.test.d.ts +0 -1
  31. package/lib/services/WorkerService.test.js +0 -10
  32. package/lib/services/getNextRunDate.d.ts +0 -9
  33. package/lib/services/getNextRunDate.js +0 -19
  34. package/lib/stale.test.d.ts +0 -1
  35. package/lib/stale.test.js +0 -108
  36. package/lib/tests/setup.d.ts +0 -1
  37. package/lib/tests/setup.js +0 -19
  38. package/lib/types/Events.d.ts +0 -21
  39. package/lib/types/Events.js +0 -2
  40. package/lib/types/HistoryRecord.d.ts +0 -21
  41. package/lib/types/HistoryRecord.js +0 -83
  42. package/lib/types/JobRecord.d.ts +0 -13
  43. package/lib/types/JobRecord.js +0 -59
  44. package/lib/types/JobsDefinition.d.ts +0 -61
  45. package/lib/types/JobsDefinition.js +0 -2
  46. package/lib/types/StartConfig.d.ts +0 -28
  47. package/lib/types/StartConfig.js +0 -2
  48. package/lib/types/Worker.d.ts +0 -38
  49. package/lib/types/Worker.js +0 -2
  50. package/lib/types/index.d.ts +0 -6
  51. package/lib/types/index.js +0 -22
@@ -0,0 +1,215 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ import { OrionLogger } from '@orion-js/logger';
4
+ import { ModelToDocumentTypeWithoutId } from '@orion-js/mongodb';
5
+
6
+ export type Blackbox = {
7
+ [name: string]: any;
8
+ };
9
+ export interface JobToRun {
10
+ jobId: string;
11
+ executionId: string;
12
+ name: string;
13
+ type: "event" | "recurrent";
14
+ params: Blackbox;
15
+ tries: number;
16
+ lockTime: number;
17
+ priority: number;
18
+ uniqueIdentifier?: string;
19
+ }
20
+ export interface ExecutionContext {
21
+ record: JobToRun;
22
+ definition: JobDefinition;
23
+ tries: number;
24
+ logger: OrionLogger;
25
+ extendLockTime: (extraTime: number) => Promise<void>;
26
+ clearStaleTimeout: () => void;
27
+ }
28
+ export interface WorkerInstance {
29
+ running: boolean;
30
+ workerIndex: number;
31
+ stop: () => Promise<void>;
32
+ respawn: () => Promise<void>;
33
+ promise?: Promise<any>;
34
+ }
35
+ export interface WorkersInstance {
36
+ running: boolean;
37
+ workersCount: number;
38
+ workers: WorkerInstance[];
39
+ /**
40
+ * Stop all workers and wait for them to finish
41
+ */
42
+ stop: () => Promise<void>;
43
+ }
44
+ export interface JobRetryResultBase {
45
+ action: "retry" | "dismiss";
46
+ }
47
+ export type JobRetryResultRunIn = JobRetryResultBase & {
48
+ runIn: number;
49
+ };
50
+ export type JobRetryResultRunAt = JobRetryResultBase & {
51
+ runAt: Date;
52
+ };
53
+ export type JobRetryResult = JobRetryResultRunIn | JobRetryResultRunAt | JobRetryResultBase;
54
+ export interface BaseJobDefinition {
55
+ /**
56
+ * The function to execute when the job is executed.
57
+ */
58
+ resolve: (params: Blackbox, context: ExecutionContext) => Promise<Blackbox | void>;
59
+ /**
60
+ * Called if the job fails.
61
+ */
62
+ onError?: (error: Error, params: Blackbox, context: ExecutionContext) => Promise<JobRetryResult>;
63
+ /**
64
+ * Called if the job locktime is expired. The job will be executed again.
65
+ */
66
+ onStale?: (params: Blackbox, context: ExecutionContext) => Promise<void>;
67
+ /**
68
+ * Save the executions of the job time in milliseconds. Default is 1 week. Set to 0 to disable.
69
+ */
70
+ saveExecutionsFor?: number;
71
+ }
72
+ export interface RecurrentJobDefinition extends BaseJobDefinition {
73
+ /**
74
+ * Type of the job.
75
+ */
76
+ type: "recurrent";
77
+ /**
78
+ * A function executed after each execution that returns the date of the next run.
79
+ */
80
+ getNextRun?: () => Date;
81
+ /**
82
+ * Run every x milliseconds. This will be ignored if getNextRun is defined.
83
+ */
84
+ runEvery?: number;
85
+ /**
86
+ * The priority of the job. Higher is more priority. Default is 100.
87
+ */
88
+ priority?: number;
89
+ }
90
+ export interface EventJobDefinition extends BaseJobDefinition {
91
+ /**
92
+ * Type of the job.
93
+ */
94
+ type: "event";
95
+ }
96
+ export type JobDefinition = RecurrentJobDefinition | EventJobDefinition;
97
+ export type JobDefinitionWithName = JobDefinition & {
98
+ name: string;
99
+ };
100
+ export interface JobsDefinition {
101
+ [jobName: string]: JobDefinition;
102
+ }
103
+ export declare const defineJob: (options: JobDefinition) => JobDefinition;
104
+ export type LogLevels = "debug" | "info" | "warn" | "error" | "none";
105
+ export interface StartWorkersConfig {
106
+ /**
107
+ * Object map of the jobs that this workers will execute
108
+ */
109
+ jobs: JobsDefinition;
110
+ /**
111
+ * Time in milliseconds to wait between each look without results for a job
112
+ * to run at the database. Default is 3000.
113
+ */
114
+ pollInterval: number;
115
+ /**
116
+ * Time in milliseconds to wait too look for a job after a job execution. Default is 100.
117
+ */
118
+ cooldownPeriod: number;
119
+ /**
120
+ * Number of workers to start. Default is 1.
121
+ */
122
+ workersCount: number;
123
+ /**
124
+ * Time in milliseconds to lock a job for execution. Default is 30000 (30 seconds).
125
+ * If a job is locked for longer than this time, it will be considered as failed.
126
+ * This is to prevent a job from being executed multiple times at the same time.
127
+ * You can extend this time inside a job by calling extendLockTime from context.
128
+ */
129
+ lockTime: number;
130
+ }
131
+ export interface ScheduleJobOptionsBase {
132
+ name: string;
133
+ params?: Blackbox;
134
+ priority?: number;
135
+ uniqueIdentifier?: string;
136
+ }
137
+ export type ScheduleJobOptionsRunIn = ScheduleJobOptionsBase & {
138
+ runIn: number;
139
+ };
140
+ export type ScheduleJobOptionsRunAt = ScheduleJobOptionsBase & {
141
+ runAt: Date;
142
+ };
143
+ export type ScheduleJobOptions = ScheduleJobOptionsRunIn | ScheduleJobOptionsRunAt | ScheduleJobOptionsBase;
144
+ export interface ScheduleJobRecordOptions {
145
+ name: string;
146
+ params: Blackbox;
147
+ nextRunAt: Date;
148
+ priority: number;
149
+ uniqueIdentifier?: string;
150
+ }
151
+ export declare class HistoryRecord {
152
+ _id: string;
153
+ jobId: string;
154
+ executionId: string;
155
+ jobName: string;
156
+ type: "recurrent" | "event";
157
+ priority: number;
158
+ tries: number;
159
+ uniqueIdentifier?: string;
160
+ startedAt: Date;
161
+ endedAt: Date;
162
+ duration: number;
163
+ expiresAt?: Date;
164
+ status: "success" | "error" | "stale";
165
+ errorMessage?: string;
166
+ params?: Blackbox;
167
+ result?: Blackbox;
168
+ }
169
+ declare class JobsHistoryRepo {
170
+ history: () => import("@orion-js/mongodb").Collection<HistoryRecord>;
171
+ saveExecution(record: ModelToDocumentTypeWithoutId<HistoryRecord>): Promise<void>;
172
+ getExecutions(jobName: string, limit?: number, skip?: number): Promise<HistoryRecord[]>;
173
+ }
174
+ export declare class JobRecord {
175
+ _id: string;
176
+ jobName: string;
177
+ type: "recurrent" | "event";
178
+ priority: number;
179
+ uniqueIdentifier?: string;
180
+ nextRunAt: Date;
181
+ lastRunAt?: Date;
182
+ lockedUntil?: Date;
183
+ tries?: number;
184
+ params?: any;
185
+ }
186
+ declare class JobsRepo {
187
+ jobs: () => import("@orion-js/mongodb").Collection<JobRecord>;
188
+ getJobAndLock(jobNames: string[], lockTime: number): Promise<JobToRun>;
189
+ setJobRecordPriority(jobId: string, priority: number): Promise<void>;
190
+ scheduleNextRun(options: {
191
+ jobId: string;
192
+ nextRunAt: Date;
193
+ addTries: boolean;
194
+ priority: number;
195
+ }): Promise<void>;
196
+ deleteEventJob(jobId: string): Promise<void>;
197
+ extendLockTime(jobId: string, extraTime: number): Promise<void>;
198
+ ensureJobRecord(job: JobDefinitionWithName): Promise<void>;
199
+ scheduleJob(options: ScheduleJobRecordOptions): Promise<void>;
200
+ }
201
+ export declare function Jobs(): ClassDecorator;
202
+ export interface JobsPropertyDescriptor extends Omit<PropertyDecorator, "value"> {
203
+ value?: JobDefinition["resolve"];
204
+ }
205
+ export declare function RecurrentJob(options: Omit<RecurrentJobDefinition, "resolve" | "type">): (target: any, propertyKey: string, descriptor: JobsPropertyDescriptor) => void;
206
+ export declare function EventJob(options?: Omit<EventJobDefinition, "resolve" | "type">): (target: any, propertyKey: string, descriptor: JobsPropertyDescriptor) => void;
207
+ export declare function getServiceJobs(target: any): {
208
+ [key: string]: JobDefinition;
209
+ };
210
+ export declare const jobsHistoryRepo: JobsHistoryRepo;
211
+ export declare const jobsRepo: JobsRepo;
212
+ export declare const startWorkers: (config: Partial<StartWorkersConfig>) => WorkersInstance;
213
+ export declare const scheduleJob: (options: ScheduleJobOptions) => Promise<void>;
214
+
215
+ export {};