@lenne.tech/nest-server 8.6.18 → 8.6.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenne.tech/nest-server",
3
- "version": "8.6.18",
3
+ "version": "8.6.19",
4
4
  "description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
5
5
  "keywords": [
6
6
  "node",
package/src/config.env.ts CHANGED
@@ -12,9 +12,11 @@ const config: { [env: string]: IServerOptions } = {
12
12
  local: {
13
13
  cronJobs: {
14
14
  sayHello: {
15
- cronTime: CronExpression.EVERY_5_MINUTES,
16
- timeZone: 'Europe/Berlin',
15
+ cronTime: CronExpression.EVERY_10_SECONDS,
17
16
  runOnInit: false,
17
+ runParallel: 1,
18
+ timeZone: 'Europe/Berlin',
19
+ throwException: false,
18
20
  },
19
21
  },
20
22
  email: {
@@ -24,11 +24,24 @@ export interface CronJobConfig {
24
24
  onComplete?: CronCommand | null;
25
25
 
26
26
  /**
27
- * This will immediately fire your `onTickfunction as soon as the requisit initialization has happened.
28
- * This option is set to ``true``` by default.
27
+ * This will immediately fire your `onTickfunction` as soon as the requisit initialization has happened.
28
+ * This option is set to `true` by default.
29
29
  */
30
30
  runOnInit?: boolean;
31
31
 
32
+ /**
33
+ * Depending on how long the execution of a job takes, it may happen that several executions take place at the
34
+ * same time. This can be prevented with `runParallel = false`.This option is set to `true` by default.
35
+ * If a number is specified, it is used as the number of maximum parallel executions.
36
+ */
37
+ runParallel?: boolean | number;
38
+
39
+ /**
40
+ * Whether an exception is thrown or only acknowledged with a console.error.
41
+ * This option is set to `true` by default.
42
+ */
43
+ throwException?: boolean;
44
+
32
45
  /**
33
46
  * Specify the timezone for the execution. This will modify the actual time relative to your timezone.
34
47
  * If the timezone is invalid, an error is thrown. Can be any string accepted by luxon's `DateTime.setZone()`
@@ -15,6 +15,11 @@ export abstract class CoreCronJobs {
15
15
  log: boolean;
16
16
  };
17
17
 
18
+ /**
19
+ * Cron jobs that are currently running
20
+ */
21
+ runningJobs: Record<string, Date[]> = {};
22
+
18
23
  // ===================================================================================================================
19
24
  // Initializations
20
25
  // ===================================================================================================================
@@ -51,18 +56,20 @@ export abstract class CoreCronJobs {
51
56
  }
52
57
 
53
58
  // Prepare config
54
- let config: CronExpression | string | Date | Falsy | CronJobConfig = CronExpressionOrConfig;
55
- if (typeof config === 'string' || config instanceof Date) {
56
- config = {
57
- cronTime: config,
59
+ let conf: CronExpression | string | Date | Falsy | CronJobConfig = CronExpressionOrConfig;
60
+ if (typeof conf === 'string' || conf instanceof Date) {
61
+ conf = {
62
+ cronTime: conf,
58
63
  };
59
64
  }
60
65
 
61
66
  // Set defaults
62
- config = {
63
- timeZone: 'Europe/Berlin',
67
+ const config: CronJobConfig = {
64
68
  runOnInit: true,
65
- ...config,
69
+ runParallel: true,
70
+ throwException: true,
71
+ timeZone: 'Europe/Berlin',
72
+ ...conf,
66
73
  };
67
74
 
68
75
  // Check if cron job should be activated
@@ -81,8 +88,40 @@ export abstract class CoreCronJobs {
81
88
  // Init cron job
82
89
  const job = new CronJob(
83
90
  config.cronTime,
84
- () => {
85
- this[name]();
91
+ async () => {
92
+ // Get current processes of cron job
93
+ const dates = this.runningJobs[name];
94
+
95
+ // Check if parallel execution is allowed and if so how many can run in parallel
96
+ if (
97
+ dates?.length &&
98
+ (!config.runParallel || (typeof config.runParallel === 'number' && dates.length >= config.runParallel))
99
+ ) {
100
+ return;
101
+ }
102
+
103
+ // Prepare the acquisition of parallel job executions
104
+ if (!this.runningJobs[name]) {
105
+ this.runningJobs[name] = [];
106
+ }
107
+ const date = new Date();
108
+ this.runningJobs[name].push(date);
109
+
110
+ // Execute the job and wait until job process is done
111
+ try {
112
+ await this[name]();
113
+ } catch (e) {
114
+ // Remove job from running list
115
+ this.runningJobs[name] = this.runningJobs[name].filter((item) => item !== date);
116
+ if (config.throwException) {
117
+ throw e;
118
+ } else {
119
+ console.error(e);
120
+ }
121
+ }
122
+
123
+ // Remove job from running list
124
+ this.runningJobs[name] = this.runningJobs[name].filter((item) => item !== date);
86
125
  },
87
126
  null,
88
127
  true,
@@ -20,7 +20,10 @@ export class CronJobs extends CoreCronJobs {
20
20
  // Cron jobs
21
21
  // ===================================================================================================================
22
22
 
23
- protected sayHello() {
23
+ protected async sayHello() {
24
24
  console.log('Hello :)');
25
+ await new Promise<void>((resolve) => {
26
+ setTimeout(() => resolve(), 30000);
27
+ });
25
28
  }
26
29
  }