Skip to content

Commit b4965d3

Browse files
authored
add debounce support on insert() via singletonSeconds (#485)
1 parent ead2bf0 commit b4965d3

6 files changed

+14
-6
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pg-boss",
3-
"version": "10.0.6",
3+
"version": "10.1.0",
44
"description": "Queueing jobs in Postgres from Node.js like a boss",
55
"main": "./src/index.js",
66
"engines": {

src/manager.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,9 @@ class Manager extends EventEmitter {
408408
this.config.retryBackoff // 6
409409
]
410410

411-
return await db.executeSql(this.insertJobsCommand, params)
411+
const { rows } = await db.executeSql(this.insertJobsCommand, params)
412+
413+
return (rows.length) ? rows.map(i => i.id) : null
412414
}
413415

414416
getDebounceStartAfter (singletonSeconds, clockOffset) {

src/plans.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ function insertJobs (schema) {
728728
priority,
729729
start_after,
730730
singleton_key,
731+
singleton_on,
731732
dead_letter,
732733
expire_in,
733734
keep_until,
@@ -743,6 +744,10 @@ function insertJobs (schema) {
743744
COALESCE(priority, 0) as priority,
744745
j.start_after,
745746
"singletonKey" as singleton_key,
747+
CASE
748+
WHEN "singletonSeconds" IS NOT NULL THEN 'epoch'::timestamp + '1 second'::interval * ("singletonSeconds" * floor( date_part('epoch', now()) / "singletonSeconds" ))
749+
ELSE NULL
750+
END as singleton_on,
746751
COALESCE("deadLetter", q.dead_letter) as dead_letter,
747752
CASE
748753
WHEN "expireInSeconds" IS NOT NULL THEN "expireInSeconds" * interval '1s'
@@ -778,7 +783,7 @@ function insertJobs (schema) {
778783
"retryDelay" integer,
779784
"retryBackoff" boolean,
780785
"singletonKey" text,
781-
"singletonOn" text,
786+
"singletonSeconds" integer,
782787
"expireInSeconds" integer,
783788
"keepUntil" timestamp with time zone,
784789
"deadLetter" text

src/timekeeper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Timekeeper extends EventEmitter {
6060
batchSize: 50
6161
}
6262

63-
await this.manager.work(QUEUES.SEND_IT, options, (jobs) => this.manager.insert(jobs.map(i => i.data)))
63+
await this.manager.work(QUEUES.SEND_IT, options, async (jobs) => { await this.manager.insert(jobs.map(i => i.data)) })
6464

6565
setImmediate(() => this.onCron())
6666

types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ declare namespace PgBoss {
207207
retryBackoff?: boolean;
208208
startAfter?: Date | string;
209209
singletonKey?: string;
210+
singletonSeconds?: number;
210211
expireInSeconds?: number;
211212
keepUntil?: Date | string;
212213
deadLetter?: string;

0 commit comments

Comments
 (0)