Skip to content

Commit 6b06753

Browse files
chore: wip
1 parent 43a75f6 commit 6b06753

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

storage/framework/core/queue/src/job.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class Queue implements Dispatchable {
131131
private deferWithDelay(): void {
132132
setTimeout(async () => {
133133
await this.dispatchNow()
134-
}, this.options.delay * 1000)
134+
}, this.options.delay || 0 * 1000)
135135
}
136136

137137
private async runJobImmediately(jobPayload: any): Promise<void> {

storage/framework/core/queue/src/process.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { log } from '@stacksjs/logging'
44
import FailedJob from '../../../orm/src/models/FailedJob'
55
import { Job } from '../../../orm/src/models/Job'
66
import { runJob } from './job'
7+
import type { JobOptions } from '@stacksjs/types'
78

89
interface QueuePayload {
910
path: string
@@ -36,36 +37,51 @@ async function executeJobs(queue: string | undefined): Promise<void> {
3637
const jobs = await Job.when(queue !== undefined, (query: JobModel) => query.where('queue', queue)).get()
3738

3839
for (const job of jobs) {
39-
if (!job.payload)
40-
continue
40+
let currentAttempts = job.attempts || 1 // Assuming the job has an `attempts` field tracking its attempts
4141

42-
if (job.available_at && job.available_at > timestampNow())
43-
continue
42+
if (!job.payload) continue
43+
44+
if (job.available_at && job.available_at > timestampNow()) continue
4445

4546
const body: QueuePayload = JSON.parse(job.payload)
46-
const currentAttempts = job.attempts || 0
47+
const classPayload = JSON.parse(job.payload) as JobOptions
48+
49+
const maxTries = Number(classPayload.tries || 3)
4750

4851
log.info(`Running job: ${body.path}`)
4952

53+
// Increment attempts before running the job
5054
await updateJobAttempts(job, currentAttempts)
5155

5256
try {
57+
// Run the job
5358
await runJob(body.name, {
5459
queue: job.queue,
5560
payload: body.params,
5661
context: '',
57-
maxTries: body.maxTries,
62+
maxTries,
5863
timeout: 60,
5964
})
6065

66+
// If job is successful, delete it
6167
await job.delete()
6268
log.info(`Successfully ran job: ${body.path}`)
6369
}
6470
catch (error) {
65-
const stringifiedError = JSON.stringify(error)
66-
67-
storeFailedJob(job, stringifiedError)
68-
log.error(`Job failed: ${body.path}`, stringifiedError)
71+
// Increment the attempt count
72+
currentAttempts++
73+
74+
if (currentAttempts > maxTries) {
75+
// If attempts exceed maxTries, store as failed job and delete
76+
const stringifiedError = JSON.stringify(error)
77+
storeFailedJob(job, stringifiedError)
78+
await job.delete() // Delete job only after exceeding maxTries
79+
log.error(`Job failed after ${maxTries} attempts: ${body.path}`, stringifiedError)
80+
} else {
81+
// If attempts are below maxTries, just update the job's attempt count
82+
await updateJobAttempts(job, currentAttempts)
83+
log.error(`Job failed, retrying... Attempt ${currentAttempts}/${maxTries}: ${body.path}`)
84+
}
6985
}
7086
}
7187
}
@@ -95,9 +111,9 @@ function now(): string {
95111
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
96112
}
97113

98-
async function updateJobAttempts(job: any, currentAttempts: number): Promise<void> {
114+
async function updateJobAttempts(job: JobModel, currentAttempts: number): Promise<void> {
99115
try {
100-
await job.update({ attempts: currentAttempts + 1 })
116+
await job.update({ attempts: currentAttempts })
101117
}
102118
catch (error) {
103119
log.error('Failed to update job attempts:', error)

storage/framework/core/queue/src/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export async function storeJob(name: string, options: QueueOption): Promise<void
1010
const payloadJson = JSON.stringify({
1111
path: `app/Jobs/${name}.ts`,
1212
name,
13-
maxTries: options.tries || 1,
1413
timeout: null,
1514
timeoutAt: null,
1615
params: options.payload || {},

0 commit comments

Comments
 (0)