Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
glennmichael123 committed Jan 9, 2025
1 parent b2175cd commit 47d245a
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 1 deletion.
16 changes: 16 additions & 0 deletions database/migrations/1736423244968-create-failed_jobs-table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Database } from '@stacksjs/database'
import { sql } from '@stacksjs/database'

export async function up(db: Database<any>) {
await db.schema
.createTable('failed_jobs')
.addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
.addColumn('connection', 'varchar(100)', col => col.notNull())
.addColumn('queue', 'varchar(255)', col => col.notNull())
.addColumn('payload', 'varchar(255)', col => col.notNull())
.addColumn('exception', 'varchar(255)', col => col.notNull())
.addColumn('failed_at', 'date')
.addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql`CURRENT_TIMESTAMP`))
.addColumn('updated_at', 'timestamp')
.execute()
}
33 changes: 32 additions & 1 deletion storage/framework/core/queue/src/process.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { JobModel } from '../../../orm/src/models/Job'
import { ok, type Ok } from '@stacksjs/error-handling'
import { log } from '@stacksjs/logging'
import FailedJob from '../../../orm/src/models/FailedJob'
import { Job } from '../../../orm/src/models/Job'
import { runJob } from './job'

Expand Down Expand Up @@ -33,6 +35,7 @@ async function executeJobs(queue: string | undefined): Promise<void> {
const jobs = await Job.when(queue !== undefined, (query: any) => query.where('queue', queue)).get()

for (const job of jobs) {
storeFailedJob(job, 'test')
if (!job.payload)
continue

Expand All @@ -59,11 +62,39 @@ async function executeJobs(queue: string | undefined): Promise<void> {
log.info(`Successfully ran job: ${body.displayName}`)
}
catch (error) {
log.error(`Job failed: ${body.displayName}`, error)
const stringifiedError = JSON.stringify(error)

storeFailedJob(job, stringifiedError)
log.error(`Job failed: ${body.displayName}`, stringifiedError)
}
}
}

async function storeFailedJob(job: JobModel, exception: string) {
const data = {
connection: 'database',
queue: job.queue,
payload: job.payload,
exception,
failed_at: now(),
}

await FailedJob.create(data)
}

function now(): string {
const date = new Date()

const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
const hours = String(date.getHours()).padStart(2, '0')
const minutes = String(date.getMinutes()).padStart(2, '0')
const seconds = String(date.getSeconds()).padStart(2, '0')

return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

async function updateJobAttempts(job: any, currentAttempts: number): Promise<void> {
try {
await job.update({ attempts: currentAttempts + 1 })
Expand Down
67 changes: 67 additions & 0 deletions storage/framework/database/models/FailedJob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { Model } from '@stacksjs/types'
import { faker } from '@stacksjs/faker'
import { schema } from '@stacksjs/validation'

export default {
name: 'FailedJob',
table: 'failed_jobs',
primaryKey: 'id',
autoIncrement: true,

traits: {
useTimestamps: true,
},

attributes: {
connection: {
required: true,
fillable: true,
validation: {
rule: schema.string().maxLength(100),
message: {
maxLength: 'Connection must have a maximum of 100 characters',
string: 'Connection must be a string',
},
},
factory: () => 'default',
},

queue: {
required: true,
fillable: true,
validation: {
rule: schema.string().maxLength(255),
message: {
maxLength: 'Queue must have a maximum of 255 characters',
},
},
factory: () => 'default',
},

payload: {
required: true,
fillable: true,
validation: {
rule: schema.string(),
},
factory: () => faker.lorem.sentence(),
},

exception: {
required: true,
fillable: true,
validation: {
rule: schema.string(),
},
factory: () => faker.lorem.sentence(),
},

failed_at: {
fillable: true,
validation: {
rule: schema.date(),
},
factory: () => '2024-12-23 13:32:19',
},
},
} satisfies Model

0 comments on commit 47d245a

Please sign in to comment.