Working proof of concept for queue
This commit is contained in:
@ -4,6 +4,8 @@ import config from '../utilities/config'
|
||||
import { Server as SocketServer } from 'socket.io'
|
||||
import { TSocket } from '../utilities/types'
|
||||
import { queueLogger } from '../utilities/logger'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
class QueueManager {
|
||||
private connection!: IORedis
|
||||
@ -26,7 +28,10 @@ class QueueManager {
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
this.queue = new Queue('jobs')
|
||||
this.queue = new Queue('jobs', {
|
||||
connection: this.connection
|
||||
})
|
||||
|
||||
this.worker = new Worker('jobs', this.processJob.bind(this), {
|
||||
connection: this.connection,
|
||||
concurrency: 10000
|
||||
@ -44,46 +49,51 @@ class QueueManager {
|
||||
}
|
||||
|
||||
private async processJob(job: Job) {
|
||||
const { jobClass, params, socketId } = job.data
|
||||
console.log('Processing job:', job.data.jobName)
|
||||
const { jobName, params, socketId } = job.data
|
||||
|
||||
console.log('Processing job:', job)
|
||||
try {
|
||||
const JobClass = await import(`../jobs/${jobClass}`)
|
||||
const jobsDir = path.join(process.cwd(), 'src', 'jobs')
|
||||
const extension = config.ENV === 'development' ? '.ts' : '.js'
|
||||
const jobPath = path.join(jobsDir, `${jobName}${extension}`)
|
||||
|
||||
if (!JobClass) {
|
||||
queueLogger.warn(`Job class not found: ${jobClass}`)
|
||||
if (!fs.existsSync(jobPath)) {
|
||||
queueLogger.warn(`Job file not found: ${jobPath}`)
|
||||
return
|
||||
}
|
||||
|
||||
console.log('Job class:', JobClass)
|
||||
const JobModule = await import(jobPath)
|
||||
const JobClass = JobModule.default
|
||||
|
||||
console.log('Job class:', JobClass.name)
|
||||
if (!JobClass || typeof JobClass !== 'function') {
|
||||
queueLogger.warn(`Invalid job class in file: ${jobPath}`)
|
||||
return
|
||||
}
|
||||
|
||||
const job = new JobClass(params)
|
||||
const jobInstance = new JobClass(params)
|
||||
|
||||
if (socketId && this.io) {
|
||||
const socket = this.io.sockets.sockets.get(socketId)
|
||||
if (socket) {
|
||||
await job.execute(this.io, socket)
|
||||
await jobInstance.execute(this.io, socket)
|
||||
} else {
|
||||
queueLogger.warn(`Socket not found for job: ${socketId}`)
|
||||
await job.execute(this.io)
|
||||
await jobInstance.execute(this.io)
|
||||
}
|
||||
} else {
|
||||
await job.execute(this.io)
|
||||
await jobInstance.execute(this.io)
|
||||
}
|
||||
} catch (error: any) {
|
||||
queueLogger.error(`Error processing job: ${error.message}`)
|
||||
queueLogger.error(`Error processing job ${jobName}: ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
public async addToQueue(jobClass: any, params: any, socket?: TSocket) {
|
||||
public async newJob(jobName: string, params: any, socket?: TSocket) {
|
||||
const jobData = {
|
||||
jobClass: jobClass.name,
|
||||
jobName,
|
||||
params,
|
||||
socketId: socket?.id
|
||||
}
|
||||
|
||||
await this.queue.add('job', jobData)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user