Skip to content

Commit e49c224

Browse files
authored
refactor: clean up dead code and minor bugs in job queue system (#16234)
- replaces a hardcoded `'payload-jobs'` string - uses `getCurrentDate()` consistently instead of `new Date()` - removes dead code: redundant `taskStatus` reassignment in `jobAfterRead`, commented-out debug logging in `calculateBackoffWaitUntil`, and a redundant queue reassignment in the run endpoint. - reduces `getCurrentDate()` calls in backoff calculation from four to one, - guards and simplifies logging-only array allocations behind the `silent` check in `runJobs` --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1214013522421649
1 parent 67c2c47 commit e49c224

8 files changed

Lines changed: 23 additions & 37 deletions

File tree

packages/payload/src/queues/config/collection.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,5 @@ export function jobAfterRead({ config, doc }: { config: SanitizedConfig; doc: Jo
296296
jobLog: doc.log || [],
297297
})
298298
doc.input = doc.input || {}
299-
doc.taskStatus = doc.taskStatus || {}
300299
return doc
301300
}

packages/payload/src/queues/endpoints/run.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ export const runJobsEndpoint: Endpoint = {
6868
silent,
6969
}
7070

71-
if (typeof queue === 'string') {
72-
runJobsArgs.queue = queue
73-
}
74-
7571
const parsedLimit = Number(limit)
7672
if (!isNaN(parsedLimit)) {
7773
runJobsArgs.limit = parsedLimit

packages/payload/src/queues/errors/calculateBackoffWaitUntil.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,22 @@ export function calculateBackoffWaitUntil({
99
retriesConfig: number | RetryConfig
1010
totalTried: number
1111
}): Date {
12-
let waitUntil: Date = getCurrentDate()
12+
const now = getCurrentDate()
13+
let waitUntil: Date = now
14+
1315
if (typeof retriesConfig === 'object') {
1416
if (retriesConfig.backoff) {
1517
if (retriesConfig.backoff.type === 'fixed') {
1618
waitUntil = retriesConfig.backoff.delay
17-
? new Date(getCurrentDate().getTime() + retriesConfig.backoff.delay)
18-
: getCurrentDate()
19+
? new Date(now.getTime() + retriesConfig.backoff.delay)
20+
: now
1921
} else if (retriesConfig.backoff.type === 'exponential') {
2022
// 2 ^ (attempts - 1) * delay (current attempt is not included in totalTried, thus no need for -1)
2123
const delay = retriesConfig.backoff.delay ? retriesConfig.backoff.delay : 0
22-
waitUntil = new Date(getCurrentDate().getTime() + Math.pow(2, totalTried) * delay)
24+
waitUntil = new Date(now.getTime() + Math.pow(2, totalTried) * delay)
2325
}
2426
}
2527
}
2628

27-
/*
28-
const differenceInMSBetweenNowAndWaitUntil = waitUntil.getTime() - getCurrentDate().getTime()
29-
30-
const differenceInSBetweenNowAndWaitUntil = differenceInMSBetweenNowAndWaitUntil / 1000
31-
console.log('Calculated backoff', {
32-
differenceInMSBetweenNowAndWaitUntil,
33-
differenceInSBetweenNowAndWaitUntil,
34-
retriesConfig,
35-
totalTried,
36-
})*/
3729
return waitUntil
3830
}

packages/payload/src/queues/operations/handleSchedules/countRunnableOrActiveJobsForQueue.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { PayloadRequest, Where } from '../../../types/index.js'
22
import type { TaskType } from '../../config/types/taskTypes.js'
33
import type { WorkflowTypes } from '../../config/types/workflowTypes.js'
44

5+
import { jobsCollectionSlug } from '../../config/collection.js'
6+
57
/**
68
* Gets all queued jobs that can be run. This means they either:
79
* - failed but do not have a definitive error => can be retried
@@ -63,7 +65,7 @@ export async function countRunnableOrActiveJobsForQueue({
6365
}
6466

6567
const runnableOrActiveJobsForQueue = await req.payload.db.count({
66-
collection: 'payload-jobs',
68+
collection: jobsCollectionSlug,
6769
req,
6870
where: {
6971
and,

packages/payload/src/queues/operations/handleSchedules/defaultAfterSchedule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const defaultAfterSchedule: AfterScheduleFn = async ({ jobStats, queueabl
4040
},
4141
},
4242
},
43-
updatedAt: new Date().toISOString(),
43+
updatedAt: getCurrentDate().toISOString(),
4444
} as JobStats,
4545
req,
4646
returning: false,

packages/payload/src/queues/operations/runJobs/index.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -303,27 +303,22 @@ export const runJobs = async (args: RunJobsArgs): Promise<RunJobsResult> => {
303303
}
304304
}
305305

306-
/**
307-
* Just for logging purposes, we want to know how many jobs are new and how many are existing (= already been tried).
308-
* This is only for logs - in the end we still want to run all jobs, regardless of whether they are new or existing.
309-
*/
310-
const { existingJobs, newJobs } = jobs.reduce(
311-
(acc, job) => {
306+
if (!silent || (typeof silent === 'object' && !silent.info)) {
307+
let newCount = 0
308+
let retryCount = 0
309+
310+
for (const job of jobs) {
312311
if (job.totalTried > 0) {
313-
acc.existingJobs.push(job)
312+
retryCount++
314313
} else {
315-
acc.newJobs.push(job)
314+
newCount++
316315
}
317-
return acc
318-
},
319-
{ existingJobs: [] as Job[], newJobs: [] as Job[] },
320-
)
316+
}
321317

322-
if (!silent || (typeof silent === 'object' && !silent.info)) {
323318
payload.logger.info({
324319
msg: `Running ${jobs.length} jobs.`,
325-
new: newJobs?.length,
326-
retrying: existingJobs?.length,
320+
new: newCount,
321+
retrying: retryCount,
327322
})
328323
}
329324

packages/payload/src/queues/operations/runJobs/runJSONJob/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export const runJSONJob = async ({
137137
return await runJSONJob({
138138
job,
139139
req,
140+
silent,
140141
updateJob,
141142
workflowConfig,
142143
workflowHandler,

packages/payload/src/queues/utilities/updateJob.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Job } from '../../index.js'
44
import type { PayloadRequest, Sort, Where } from '../../types/index.js'
55

66
import { jobAfterRead, jobsCollectionSlug } from '../config/collection.js'
7+
import { getCurrentDate } from './getCurrentDate.js'
78

89
type BaseArgs = {
910
data: Partial<Job>
@@ -85,7 +86,7 @@ export async function updateJobs({
8586

8687
if (typeof data.updatedAt === 'undefined') {
8788
// Ensure updatedAt date is always updated
88-
data.updatedAt = new Date().toISOString()
89+
data.updatedAt = getCurrentDate().toISOString()
8990
}
9091

9192
const args: UpdateJobsArgs = id

0 commit comments

Comments
 (0)