@tsed/agenda 7.53.0 → 7.55.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.
@@ -7,6 +7,7 @@ export declare class AgendaModule implements OnDestroy, AfterListen {
7
7
  protected injector: InjectorService;
8
8
  protected agenda: AgendaService;
9
9
  private enabled;
10
+ private drainJobsBeforeClose;
10
11
  private disableJobProcessing;
11
12
  $afterListen(): Promise<any>;
12
13
  $onDestroy(): Promise<any>;
@@ -8,6 +8,7 @@ declare global {
8
8
  */
9
9
  enabled?: boolean;
10
10
  disableJobProcessing?: boolean;
11
+ drainJobsBeforeClose?: boolean;
11
12
  } & AgendaConfig;
12
13
  }
13
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/agenda",
3
- "version": "7.53.0",
3
+ "version": "7.55.0",
4
4
  "description": "Agenda package for Ts.ED framework",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./lib/cjs/index.js",
@@ -25,16 +25,16 @@
25
25
  },
26
26
  "private": false,
27
27
  "devDependencies": {
28
- "@tsed/common": "7.53.0",
29
- "@tsed/core": "7.53.0",
30
- "@tsed/di": "7.53.0",
28
+ "@tsed/common": "7.55.0",
29
+ "@tsed/core": "7.55.0",
30
+ "@tsed/di": "7.55.0",
31
31
  "@tsed/eslint": "7.44.1",
32
32
  "@tsed/typescript": "7.44.1",
33
- "agenda": "^4.1.3",
33
+ "agenda": "^5.0.0",
34
34
  "eslint": "^8.12.0"
35
35
  },
36
36
  "peerDependencies": {
37
- "agenda": "^4.1.3"
37
+ "agenda": ">=4"
38
38
  },
39
39
  "repository": "https://github.com/tsedio/tsed",
40
40
  "bugs": {
package/readme.md CHANGED
@@ -61,6 +61,8 @@ const mongoConnectionString = "mongodb://127.0.0.1/agenda";
61
61
  @Configuration({
62
62
  agenda: {
63
63
  enabled: true, // Enable Agenda jobs for this instance.
64
+ // drainJobsBeforeStop: true, // Wait for jobs to finish before stopping the agenda process.
65
+ // disableJobProcessing: true, // Prevents jobs from being processed.
64
66
  // pass any options that you would normally pass to new Agenda(), e.g.
65
67
  db: {
66
68
  address: mongoConnectionString
@@ -11,6 +11,7 @@ jest.mock("agenda", () => {
11
11
  Agenda: class {
12
12
  close = jest.fn();
13
13
  stop = jest.fn();
14
+ drain = jest.fn();
14
15
  define = jest.fn();
15
16
  every = jest.fn();
16
17
  schedule = jest.fn();
@@ -131,11 +132,32 @@ describe("AgendaModule", () => {
131
132
 
132
133
  await agendaModule.$onDestroy();
133
134
 
135
+ expect(agendaModule.agenda.stop).toHaveBeenCalledWith();
134
136
  expect(agendaModule.agenda.close).toHaveBeenCalledWith({force: true});
135
137
  });
136
138
  });
137
139
  });
140
+ describe("when agenda is enabled and drainJobsBeforeClose = true", () => {
141
+ beforeEach(() =>
142
+ PlatformTest.create({
143
+ agenda: {
144
+ enabled: true,
145
+ drainJobsBeforeClose: true
146
+ }
147
+ })
148
+ );
149
+ afterEach(() => PlatformTest.reset());
150
+ describe("$onDestroy()", () => {
151
+ it("should close agenda", async () => {
152
+ const agendaModule = PlatformTest.get<any>(AgendaModule)!;
153
+
154
+ await agendaModule.$onDestroy();
138
155
 
156
+ expect(agendaModule.agenda.drain).toHaveBeenCalledWith();
157
+ expect(agendaModule.agenda.close).toHaveBeenCalledWith({force: true});
158
+ });
159
+ });
160
+ });
139
161
  describe("when agenda is enabled but disableJobProcessing = true", () => {
140
162
  beforeEach(() =>
141
163
  PlatformTest.create({
@@ -20,6 +20,9 @@ export class AgendaModule implements OnDestroy, AfterListen {
20
20
  @Constant("agenda.enabled", false)
21
21
  private enabled: boolean;
22
22
 
23
+ @Constant("agenda.drainJobsBeforeClose", false)
24
+ private drainJobsBeforeClose: boolean;
25
+
23
26
  @Constant("agenda.disableJobProcessing", false)
24
27
  private disableJobProcessing: boolean;
25
28
 
@@ -28,7 +31,10 @@ export class AgendaModule implements OnDestroy, AfterListen {
28
31
  const providers = this.getProviders();
29
32
 
30
33
  if (!this.disableJobProcessing) {
31
- this.logger.info("Agenda add definitions...");
34
+ this.logger.info({
35
+ event: "AGENDA_ADD_DEFINITIONS",
36
+ message: "Agenda add definitions"
37
+ });
32
38
  providers.forEach((provider) => this.addAgendaDefinitionsForProvider(provider));
33
39
 
34
40
  await this.injector.emit("$beforeAgendaStart");
@@ -37,22 +43,36 @@ export class AgendaModule implements OnDestroy, AfterListen {
37
43
  await this.agenda.start();
38
44
 
39
45
  if (!this.disableJobProcessing) {
40
- this.logger.info("Agenda add scheduled jobs...");
46
+ this.logger.info({
47
+ event: "AGENDA_ADD_JOBS",
48
+ message: "Agenda add scheduled jobs"
49
+ });
41
50
  await Promise.all(providers.map((provider) => this.scheduleJobsForProvider(provider)));
42
51
 
43
52
  await this.injector.emit("$afterAgendaStart");
44
53
  }
45
- } else {
46
- this.logger.info("Agenda disabled...");
47
54
  }
48
55
  }
49
56
 
50
57
  async $onDestroy(): Promise<any> {
51
58
  if (this.enabled) {
52
- await this.agenda.stop();
59
+ if (this.drainJobsBeforeClose && "drain" in this.agenda) {
60
+ this.logger.info({
61
+ event: "AGENDA_DRAIN",
62
+ message: "Agenda is draining all jobs before close"
63
+ });
64
+ await this.agenda.drain();
65
+ } else {
66
+ this.logger.info({
67
+ event: "AGENDA_STOP",
68
+ message: "Agenda is stopping"
69
+ });
70
+ await this.agenda.stop();
71
+ }
72
+
53
73
  await this.agenda.close({force: true});
54
74
 
55
- this.logger.info("Agenda stopped...");
75
+ this.logger.info({event: "AGENDA_STOP", message: "Agenda stopped"});
56
76
  }
57
77
  }
58
78
 
@@ -100,7 +120,7 @@ export class AgendaModule implements OnDestroy, AfterListen {
100
120
  Object.entries(store.define).forEach(([propertyKey, {name, ...options}]) => {
101
121
  const instance = this.injector.get(provider.token);
102
122
 
103
- const jobProcessor: Processor = instance[propertyKey].bind(instance) as Processor;
123
+ const jobProcessor: Processor<unknown> = instance[propertyKey].bind(instance) as Processor<unknown>;
104
124
  const jobName = this.getNameForJob(propertyKey, store.namespace, name);
105
125
 
106
126
  this.define(jobName, options, jobProcessor);
@@ -9,6 +9,7 @@ declare global {
9
9
  */
10
10
  enabled?: boolean;
11
11
  disableJobProcessing?: boolean;
12
+ drainJobsBeforeClose?: boolean;
12
13
  } & AgendaConfig;
13
14
  }
14
15
  }