Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
glennmichael123 committed Jan 8, 2025
1 parent d06fa05 commit ca0d243
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
30 changes: 16 additions & 14 deletions storage/framework/core/queue/src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ export class Queue implements Dispatchable {
) { }

async dispatch(): Promise<void> {
const queueName = this.options.queue || 'default'

if (['database', 'redis'].includes(queueDriver)) {
await storeJob(this.name, {
queue: queueName,
payload: this.payload,
context: this.options.context,
maxTries: this.options.maxTries,
timeout: this.options.timeout,
backoff: this.options.backoff,
delay: this.options.delay
})

process.exit(0)
}

if (this.options.afterResponse) {
process.on('beforeExit', async () => {
await this.dispatchNow()
Expand All @@ -112,21 +128,7 @@ export class Queue implements Dispatchable {
return
}

const queueName = this.options.queue || 'default'

try {
if (['database', 'redis'].includes(queueDriver)) {
await storeJob(this.name, {
queue: queueName,
payload: this.payload,
context: this.options.context,
maxTries: this.options.maxTries,
timeout: this.options.timeout,
backoff: this.options.backoff,
})

process.exit(0)
}

await runJob(this.name, {
queue: queueName,
Expand Down
12 changes: 11 additions & 1 deletion storage/framework/core/queue/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { JobOptions } from './job'
import Job from '../../../orm/src/models/Job'

export async function storeJob(name: string, options: JobOptions): Promise<void> {
interface QueueOption extends JobOptions {
delay: number
}

export async function storeJob(name: string, options: QueueOption): Promise<void> {
const payloadJson = JSON.stringify({
displayName: `app/Jobs/${name}.ts`,
name,
Expand All @@ -14,7 +18,13 @@ export async function storeJob(name: string, options: JobOptions): Promise<void>
queue: options.queue,
payload: payloadJson,
attempts: 0,
available_at: generateUnixTimestamp(options.delay || 0)
}

await Job.create(job)
}

function generateUnixTimestamp(secondsToAdd: number): number {
const now = Date.now()
return Math.floor((now / 1000) + secondsToAdd)
}

0 comments on commit ca0d243

Please sign in to comment.