Example code
This commit is contained in:
parent
81428ea0c2
commit
4b1f6884ae
@ -1,9 +1,14 @@
|
||||
import { TSocket } from '../utilities/types'
|
||||
import { Server as SocketServer } from 'socket.io'
|
||||
import QueueManager from '../managers/queueManager'
|
||||
|
||||
export default class SomeJob {
|
||||
constructor(private params: any) {}
|
||||
|
||||
public static initializeSocket(socket: TSocket) {
|
||||
socket.on('character:connect', () => QueueManager.newJob('SomeJob', {}, socket));
|
||||
}
|
||||
|
||||
async execute(io: SocketServer, socket?: TSocket) {
|
||||
// Handle the event
|
||||
if (socket) {
|
||||
|
@ -6,15 +6,18 @@ import { TSocket } from '../utilities/types'
|
||||
import { queueLogger } from '../utilities/logger'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { Dirent } from 'node:fs'
|
||||
|
||||
class QueueManager {
|
||||
private connection!: IORedis
|
||||
private queue!: Queue
|
||||
private worker!: Worker
|
||||
private io!: SocketServer
|
||||
private socket!: TSocket
|
||||
|
||||
public async boot(io: SocketServer) {
|
||||
public async boot(io: SocketServer, socket: TSocket) {
|
||||
this.io = io
|
||||
this.socket = socket
|
||||
|
||||
this.connection = new IORedis(config.REDIS_URL, {
|
||||
maxRetriesPerRequest: null
|
||||
@ -46,6 +49,20 @@ class QueueManager {
|
||||
})
|
||||
|
||||
queueLogger.info('Queue manager loaded')
|
||||
await this.initializeSocketJobs()
|
||||
}
|
||||
|
||||
private async initializeSocketJobs() {
|
||||
const dir = path.join(__dirname, '../jobs')
|
||||
|
||||
const files: Dirent[] = await fs.promises.readdir(dir, { withFileTypes: true })
|
||||
|
||||
for (const file of files) {
|
||||
const fullPath = path.join(dir, file.name)
|
||||
|
||||
const module = await import(fullPath)
|
||||
module.default.initializeSocket(this.socket);
|
||||
}
|
||||
}
|
||||
|
||||
private async processJob(job: Job) {
|
||||
|
@ -58,8 +58,7 @@ export class Server {
|
||||
appLogger.error(`Socket.IO failed to start: ${error.message}`)
|
||||
}
|
||||
|
||||
// Load queue manager
|
||||
await QueueManager.boot(this.io)
|
||||
|
||||
|
||||
// Add http API routes
|
||||
await addHttpRoutes(this.app)
|
||||
@ -88,6 +87,8 @@ export class Server {
|
||||
private async handleConnection(socket: TSocket) {
|
||||
const eventsPath = path.join(__dirname, 'socketEvents')
|
||||
try {
|
||||
// Load queue manager
|
||||
await QueueManager.boot(this.io, socket);
|
||||
await this.loadEventHandlers(eventsPath, socket)
|
||||
} catch (error: any) {
|
||||
appLogger.error(`Failed to load event handlers: ${error.message}`)
|
||||
|
Loading…
x
Reference in New Issue
Block a user