@joystick.js/node-canary 0.0.0-canary.413 → 0.0.0-canary.414
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 u={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(){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==="
|
|
1
|
+
import i from"../../../../lib/timestamps.js";const u={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(){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 s=u;export{s as default};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import s from"node-cron";import
|
|
1
|
+
import s from"node-cron";import a from"../../postgresql/handle_cleanup_queues.js";import n from"../../../../lib/timestamps.js";import i from"../../../../lib/wait.js";const o={add_job:async function(e={}){const t=this?.db,[u]=await t?.query("SELECT * FROM information_schema.tables WHERE table_name = $1",[`queue_${this.queue.name}`]);return u||await i(1),t?.query(`
|
|
2
2
|
INSERT INTO queue_${this.queue.name} (
|
|
3
3
|
_id,
|
|
4
4
|
status,
|
|
@@ -77,7 +77,7 @@ import s from"node-cron";import i from"../../postgresql/handle_cleanup_queues.js
|
|
|
77
77
|
environment text,
|
|
78
78
|
attempts smallint
|
|
79
79
|
)
|
|
80
|
-
`),await e?.query(`ALTER TABLE queue_${this.queue.name} ADD COLUMN IF NOT EXISTS environment text`),await e?.query(`ALTER TABLE queue_${this.queue.name} ADD COLUMN IF NOT EXISTS attempts smallint`),await e?.query(`CREATE INDEX IF NOT EXISTS status_index ON queue_${this.queue.name} (status)`),await e?.query(`CREATE INDEX IF NOT EXISTS
|
|
80
|
+
`),await e?.query(`ALTER TABLE queue_${this.queue.name} ADD COLUMN IF NOT EXISTS environment text`),await e?.query(`ALTER TABLE queue_${this.queue.name} ADD COLUMN IF NOT EXISTS attempts smallint`),await e?.query(`CREATE INDEX IF NOT EXISTS status_index ON queue_${this.queue.name} (status)`),await e?.query(`CREATE INDEX IF NOT EXISTS status_next_run_at_index ON queue_${this.queue.name} (status, next_run_at)`),await e?.query(`CREATE INDEX IF NOT EXISTS next_job_index ON queue_${this.queue.name} (status, environment, next_run_at, locked_by)`),await e?.query(`CREATE INDEX IF NOT EXISTS completed_at_index ON queue_${this.queue.name} (completed_at)`),await e?.query(`CREATE INDEX IF NOT EXISTS failed_at_index ON queue_${this.queue.name} (failed_at)`),(this.queue.options?.cleanup?.completedAfterSeconds||this.queue.options?.cleanup?.completed_after_seconds)&&s.schedule("*/30 * * * * *",()=>{a({database:e,table:`queue_${this.queue.name}`,seconds:this.queue.options?.cleanup?.completedAfterSeconds||this.queue.options?.cleanup?.completed_after_seconds})}),(this.queue.options?.cleanup?.failedAfterSeconds||this.queue.options?.cleanup?.failed_after_seconds)&&s.schedule("*/30 * * * * *",()=>{a({database:e,table:`queue_${this.queue.name}`,seconds:this.queue.options?.cleanup?.failedAfterSeconds||this.queue.options?.cleanup?.failed_after_seconds})})},log_attempt:function(e=""){return this?.db?.query(`
|
|
81
81
|
UPDATE
|
|
82
82
|
queue_${this.queue.name}
|
|
83
83
|
SET
|
package/package.json
CHANGED
|
@@ -77,7 +77,7 @@ const queues ={
|
|
|
77
77
|
await db.createIndex({ status: 1, environment: 1, next_run_at: 1, locked_by: 1 });
|
|
78
78
|
|
|
79
79
|
if (this.queue.options?.cleanup?.completedAfterSeconds || this.queue.options?.cleanup?.completed_after_seconds) {
|
|
80
|
-
if (indexes?.find((index) => index?.name === '
|
|
80
|
+
if (indexes?.find((index) => index?.name === 'completed_at_1')) {
|
|
81
81
|
await db.dropIndex({ completed_at: 1 });
|
|
82
82
|
}
|
|
83
83
|
|
|
@@ -85,7 +85,7 @@ const queues ={
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
if (this.queue.options?.cleanup?.failedAfterSeconds || this.queue.options?.cleanup?.failed_after_seconds) {
|
|
88
|
-
if (indexes?.find((index) => index?.name === '
|
|
88
|
+
if (indexes?.find((index) => index?.name === 'failed_at_1')) {
|
|
89
89
|
await db.dropIndex({ failed_at: 1 });
|
|
90
90
|
}
|
|
91
91
|
|
|
@@ -171,11 +171,11 @@ const queues = {
|
|
|
171
171
|
await db?.query(`ALTER TABLE queue_${this.queue.name} ADD COLUMN IF NOT EXISTS attempts smallint`);
|
|
172
172
|
|
|
173
173
|
await db?.query(`CREATE INDEX IF NOT EXISTS status_index ON queue_${this.queue.name} (status)`);
|
|
174
|
-
await db?.query(`CREATE INDEX IF NOT EXISTS
|
|
175
|
-
await db?.query(`CREATE INDEX IF NOT EXISTS
|
|
174
|
+
await db?.query(`CREATE INDEX IF NOT EXISTS status_next_run_at_index ON queue_${this.queue.name} (status, next_run_at)`);
|
|
175
|
+
await db?.query(`CREATE INDEX IF NOT EXISTS next_job_index ON queue_${this.queue.name} (status, environment, next_run_at, locked_by)`);
|
|
176
176
|
|
|
177
|
-
await db?.query(`CREATE INDEX IF NOT EXISTS
|
|
178
|
-
await db?.query(`CREATE INDEX IF NOT EXISTS
|
|
177
|
+
await db?.query(`CREATE INDEX IF NOT EXISTS completed_at_index ON queue_${this.queue.name} (completed_at)`);
|
|
178
|
+
await db?.query(`CREATE INDEX IF NOT EXISTS failed_at_index ON queue_${this.queue.name} (failed_at)`);
|
|
179
179
|
|
|
180
180
|
// NOTE: PostgreSQL does NOT have a TTL index or event-based row expiration feature,
|
|
181
181
|
// so we "polyfill" here with 30 second cron jobs to do the cleanup for us.
|