Continuation of refactor

This commit is contained in:
2024-12-28 20:40:05 +01:00
parent 6dda79f8b2
commit e1a6f650fb
27 changed files with 158 additions and 236 deletions

View File

@ -5,7 +5,7 @@ import IORedis from 'ioredis'
import { Server as SocketServer } from 'socket.io'
import config from '#application/config'
import { queueLogger } from '#application/logger'
import Logger, { LoggerType } from '#application/logger'
import { getAppPath } from '#application/storage'
import { TSocket } from '#application/types'
@ -14,6 +14,7 @@ class QueueManager {
private queue!: Queue
private worker!: Worker
private io!: SocketServer
private logger = Logger.type(LoggerType.QUEUE)
public async boot(io: SocketServer) {
this.io = io
@ -24,9 +25,9 @@ class QueueManager {
try {
await this.connection.ping()
queueLogger.info('Successfully connected to Redis')
this.logger.info('Successfully connected to Redis')
} catch (error) {
queueLogger.error('Failed to connect to Redis:', error)
this.logger.error('Failed to connect to Redis:', error)
process.exit(1)
}
@ -40,14 +41,14 @@ class QueueManager {
})
this.worker.on('completed', (job) => {
queueLogger.info(`Job ${job?.id} has completed`)
this.logger.info(`Job ${job?.id} has completed`)
})
this.worker.on('failed', (job, err) => {
queueLogger.error(`Job ${job?.id} failed with error: ${err}`)
this.logger.error(`Job ${job?.id} failed with error: ${err}`)
})
queueLogger.info('Queue manager loaded')
this.logger.info('Queue manager loaded')
}
private async processJob(job: Job) {
@ -59,7 +60,7 @@ class QueueManager {
const jobPath = getAppPath('jobs', `${jobName}${extension}`)
if (!fs.existsSync(jobPath)) {
queueLogger.warn(`Job file not found: ${jobPath}`)
this.logger.warn(`Job file not found: ${jobPath}`)
return
}
@ -67,7 +68,7 @@ class QueueManager {
const JobClass = JobModule.default
if (!JobClass || typeof JobClass !== 'function') {
queueLogger.warn(`Invalid job class in file: ${jobPath}`)
this.logger.warn(`Invalid job class in file: ${jobPath}`)
return
}
@ -78,14 +79,14 @@ class QueueManager {
if (socket) {
await jobInstance.execute(this.io, socket)
} else {
queueLogger.warn(`Socket not found for job: ${socketId}`)
this.logger.warn(`Socket not found for job: ${socketId}`)
await jobInstance.execute(this.io)
}
} else {
await jobInstance.execute(this.io)
}
} catch (error: any) {
queueLogger.error(`Error processing job ${jobName}: ${error.message}`)
this.logger.error(`Error processing job ${jobName}: ${error.message}`)
}
}