@joystick.js/node-canary 0.0.0-canary.443 → 0.0.0-canary.444
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
import i from"../../../../lib/timestamps.js";const
|
|
1
|
+
import u from"cluster";import i from"../../../../lib/timestamps.js";const o={add_job:function(e={}){return(this.db?.collection(`queue_${this.queue.name}`)).insertOne({...e,attempts:0})},count_jobs:function(e=""){return(this.db?.collection(`queue_${this.queue.name}`)).countDocuments({status:e,locked_by:this.machine_id})},delete_job:function(e=""){return(this.db?.collection(`queue_${this.queue.name}`)).deleteOne({_id:e})},delete_incomplete_jobs_for_machine:function(){return(this.db?.collection(`queue_${this.queue.name}`)).deleteMany({status:{$in:["incomplete","running"]},locked_by:this.machine_id})},get_jobs:function(e={}){return(this.db?.collection(`queue_${this.queue.name}`)).find({...e,environment:process.env.NODE_ENV}).toArray()},get_next_job_to_run:async function(){return await(this.db?.collection(`queue_${this.queue.name}`)).findOneAndUpdate({$or:[{status:"pending",environment:process.env.NODE_ENV,next_run_at:{$lte:i.get_current_time()},locked_by:{$exists:!1}},{status:"pending",environment:process.env.NODE_ENV,next_run_at:{$lte:i.get_current_time()},locked_by:null}]},{$set:{status:"running",started_at:i.get_current_time(),locked_by:this.machine_id}},{sort:{next_run_at:1}})},initialize_database:async function(){if(u.isPrimary||u.isWorker&&u.worker.id===1){try{await this.db.createCollection(`queue_${this.queue.name}`)}catch{}const e=this.db?.collection(`queue_${this.queue.name}`),t=await e?.indexes();await e.createIndex({status:1}),await e.createIndex({status:1,next_run_at:1}),await e.createIndex({status:1,environment:1,next_run_at:1,locked_by:1}),(this.queue.options?.cleanup?.completedAfterSeconds||this.queue.options?.cleanup?.completed_after_seconds)&&(t?.find(n=>n?.name==="completed_at_1")&&await e.dropIndex({completed_at:1}),await e.createIndex({completed_at:1},{expireAfterSeconds:this?.queue?.options?.cleanup?.completedAfterSeconds||this.queue.options?.cleanup?.completed_after_seconds})),(this.queue.options?.cleanup?.failedAfterSeconds||this.queue.options?.cleanup?.failed_after_seconds)&&(t?.find(n=>n?.name==="failed_at_1")&&await e.dropIndex({failed_at:1}),await e.createIndex({failed_at:1},{expireAfterSeconds:this?.queue?.options?.cleanup?.failedAfterSeconds||this.queue.options?.cleanup?.failed_after_seconds}))}},log_attempt:function(e=""){return(this.db?.collection(`queue_${this.queue.name}`)).updateOne({_id:e},{$inc:{attempts:1}})},requeue_job:function(e="",t=null){return(this.db?.collection(`queue_${this.queue.name}`)).updateOne({_id:e},{$set:{status:"pending",next_run_at:t},$unset:{locked_by:""}})},set_job_completed:function(e=""){return(this.db?.collection(`queue_${this.queue.name}`)).updateOne({_id:e},{$set:{status:"completed",completed_at:i.get_current_time({mongodb_ttl:!0})}})},set_job_failed:function(e="",t=""){return(this.db?.collection(`queue_${this.queue.name}`)).updateOne({_id:e},{$set:{status:"failed",failed_at:i.get_current_time({mongodb_ttl:!0}),error:t}})},set_jobs_for_machine_pending:function(){return(this.db?.collection(`queue_${this.queue.name}`)).updateMany({status:{$in:["pending","running"]},locked_by:this.machine_id},{$set:{status:"pending"},$unset:{locked_by:""}})}};var a=o;export{a as default};
|
package/package.json
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import cluster from 'cluster';
|
|
1
2
|
import timestamps from '../../../../lib/timestamps.js';
|
|
2
3
|
|
|
3
4
|
const queues ={
|
|
@@ -62,34 +63,40 @@ const queues ={
|
|
|
62
63
|
return next_job;
|
|
63
64
|
},
|
|
64
65
|
initialize_database: async function () {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
// NOTE: Drop the error. We anticipate it after the first run.
|
|
69
|
-
}
|
|
66
|
+
// NOTE: Add this check to avoid clustered apps from creating a race condition
|
|
67
|
+
// when initializing indexes below (they step on each other's toes and cause
|
|
68
|
+
// errors to be thrown). Only a primary or 1st worker should run this.
|
|
70
69
|
|
|
71
|
-
|
|
70
|
+
if (cluster.isPrimary || (cluster.isWorker && cluster.worker.id === 1)) {
|
|
71
|
+
try {
|
|
72
|
+
await this.db.createCollection(`queue_${this.queue.name}`);
|
|
73
|
+
} catch {
|
|
74
|
+
// NOTE: Drop the error. We anticipate it after the first run.
|
|
75
|
+
}
|
|
72
76
|
|
|
73
|
-
|
|
77
|
+
const db = this.db?.collection(`queue_${this.queue.name}`);
|
|
74
78
|
|
|
75
|
-
|
|
76
|
-
await db.createIndex({ status: 1, next_run_at: 1 });
|
|
77
|
-
await db.createIndex({ status: 1, environment: 1, next_run_at: 1, locked_by: 1 });
|
|
79
|
+
const indexes = await db?.indexes();
|
|
78
80
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
81
|
+
await db.createIndex({ status: 1 });
|
|
82
|
+
await db.createIndex({ status: 1, next_run_at: 1 });
|
|
83
|
+
await db.createIndex({ status: 1, environment: 1, next_run_at: 1, locked_by: 1 });
|
|
83
84
|
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
if (this.queue.options?.cleanup?.completedAfterSeconds || this.queue.options?.cleanup?.completed_after_seconds) {
|
|
86
|
+
if (indexes?.find((index) => index?.name === 'completed_at_1')) {
|
|
87
|
+
await db.dropIndex({ completed_at: 1 });
|
|
88
|
+
}
|
|
86
89
|
|
|
87
|
-
|
|
88
|
-
if (indexes?.find((index) => index?.name === 'failed_at_1')) {
|
|
89
|
-
await db.dropIndex({ failed_at: 1 });
|
|
90
|
+
await db.createIndex({ completed_at: 1 }, { expireAfterSeconds: this?.queue?.options?.cleanup?.completedAfterSeconds || this.queue.options?.cleanup?.completed_after_seconds });
|
|
90
91
|
}
|
|
91
92
|
|
|
92
|
-
|
|
93
|
+
if (this.queue.options?.cleanup?.failedAfterSeconds || this.queue.options?.cleanup?.failed_after_seconds) {
|
|
94
|
+
if (indexes?.find((index) => index?.name === 'failed_at_1')) {
|
|
95
|
+
await db.dropIndex({ failed_at: 1 });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
await db.createIndex({ failed_at: 1 }, { expireAfterSeconds: this?.queue?.options?.cleanup?.failedAfterSeconds || this.queue.options?.cleanup?.failed_after_seconds });
|
|
99
|
+
}
|
|
93
100
|
}
|
|
94
101
|
},
|
|
95
102
|
log_attempt: function (job_id = '') {
|