@joystick.js/node-canary 0.0.0-canary.289 → 0.0.0-canary.290

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.
@@ -5,6 +5,7 @@ import generateId from "../../lib/generateId";
5
5
  import getTargetDatabaseProvider from "../databases/getTargetDatabaseProvider";
6
6
  import queryMap from "../databases/queryMap";
7
7
  import chalk from "chalk";
8
+ import timestamps from "../../lib/timestamps";
8
9
  class Queue {
9
10
  constructor(queueName = "", queueOptions = {}) {
10
11
  this._initDatabase = this._initDatabase.bind(this);
@@ -103,10 +104,18 @@ class Queue {
103
104
  });
104
105
  }
105
106
  async handleNextJob(nextJob = {}) {
106
- if (nextJob && nextJob?.job && this.options.jobs[nextJob?.job] && typeof this.options.jobs[nextJob?.job]?.run === "function") {
107
+ const job_definition = this.options.jobs[nextJob?.job];
108
+ if (nextJob && nextJob?.job && job_definition && typeof job_definition?.run === "function") {
107
109
  try {
110
+ if (typeof job_definition?.preflight?.okayToRun === "function") {
111
+ const okay_to_run = await job_definition?.preflight?.okayToRun(nextJob?.payload, nextJob);
112
+ console.log({ okay_to_run, job_id: nextJob?._id });
113
+ if (!okay_to_run) {
114
+ return this._handleRequeueJob(nextJob, timestamps.get_future_time("seconds", job_definition?.preflight?.requeueDelayInSeconds || 10));
115
+ }
116
+ }
108
117
  await this._logAttempt(nextJob?._id);
109
- await this.options.jobs[nextJob.job].run(nextJob?.payload, {
118
+ await job_definition.run(nextJob?.payload, {
110
119
  ...nextJob,
111
120
  queue: this,
112
121
  completed: () => this._handleJobCompleted(nextJob?._id),
@@ -116,7 +125,7 @@ class Queue {
116
125
  });
117
126
  } catch (exception) {
118
127
  this._handleJobFailed(nextJob?._id, exception);
119
- if (this.options.jobs[nextJob.job]?.requeueOnFailure) {
128
+ if (job_definition?.requeueOnFailure) {
120
129
  this._handleRequeueJob(nextJob?.nextRunAt, dayjs().add(10, "seconds").format());
121
130
  }
122
131
  }
@@ -0,0 +1,47 @@
1
+ var timestamps_default = {
2
+ get_current_time: (options2 = {}) => {
3
+ const timestamp = new Date().toISOString();
4
+ return options2?.mongodb_ttl ? new Date(timestamp) : timestamp;
5
+ },
6
+ get_future_time: (unit = "", quantity = 0, options2 = {}) => {
7
+ const date = new Date();
8
+ switch (unit) {
9
+ case "seconds":
10
+ date.setSeconds(date.getSeconds() + quantity);
11
+ return options2?.mongodb_ttl ? new Date(date.toISOString()) : date.toISOString();
12
+ case "minutes":
13
+ date.setMinutes(date.getMinutes() + quantity);
14
+ return options2?.mongodb_ttl ? new Date(date.toISOString()) : date.toISOString();
15
+ case "hours":
16
+ date.setHours(date.getHours() + quantity);
17
+ return options2?.mongodb_ttl ? new Date(date.toISOString()) : date.toISOString();
18
+ case "days":
19
+ date.setHours(date.getHours() + quantity * 24);
20
+ return options2?.mongodb_ttl ? new Date(date.toISOString()) : date.toISOString();
21
+ default:
22
+ return options2?.mongodb_ttl ? new Date(date.toISOString()) : date.toISOString();
23
+ }
24
+ },
25
+ get_past_time: (unit = "", quantity = 0) => {
26
+ const date = new Date();
27
+ switch (unit) {
28
+ case "seconds":
29
+ date.setSeconds(date.getSeconds() - quantity);
30
+ return options?.mongodb_ttl ? new Date(date.toISOString()) : date.toISOString();
31
+ case "minutes":
32
+ date.setMinutes(date.getMinutes() - quantity);
33
+ return options?.mongodb_ttl ? new Date(date.toISOString()) : date.toISOString();
34
+ case "hours":
35
+ date.setHours(date.getHours() - quantity);
36
+ return options?.mongodb_ttl ? new Date(date.toISOString()) : date.toISOString();
37
+ case "days":
38
+ date.setHours(date.getHours() - quantity * 24);
39
+ return options?.mongodb_ttl ? new Date(date.toISOString()) : date.toISOString();
40
+ default:
41
+ return options?.mongodb_ttl ? new Date(new Date().toISOString()) : new Date().toISOString();
42
+ }
43
+ }
44
+ };
45
+ export {
46
+ timestamps_default as default
47
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@joystick.js/node-canary",
3
- "version": "0.0.0-canary.289",
3
+ "version": "0.0.0-canary.290",
4
4
  "type": "module",
5
5
  "description": "A Node.js framework for building web apps.",
6
6
  "main": "./dist/index.js",