1
0
forked from noxious/server

Compare commits

...

1 Commits

Author SHA1 Message Date
Zaxiure
4b1f6884ae
Example code 2024-09-22 15:34:35 +02:00
3 changed files with 26 additions and 3 deletions

View File

@ -1,9 +1,14 @@
import { TSocket } from '../utilities/types' import { TSocket } from '../utilities/types'
import { Server as SocketServer } from 'socket.io' import { Server as SocketServer } from 'socket.io'
import QueueManager from '../managers/queueManager'
export default class SomeJob { export default class SomeJob {
constructor(private params: any) {} constructor(private params: any) {}
public static initializeSocket(socket: TSocket) {
socket.on('character:connect', () => QueueManager.newJob('SomeJob', {}, socket));
}
async execute(io: SocketServer, socket?: TSocket) { async execute(io: SocketServer, socket?: TSocket) {
// Handle the event // Handle the event
if (socket) { if (socket) {

View File

@ -6,15 +6,18 @@ import { TSocket } from '../utilities/types'
import { queueLogger } from '../utilities/logger' import { queueLogger } from '../utilities/logger'
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import { Dirent } from 'node:fs'
class QueueManager { class QueueManager {
private connection!: IORedis private connection!: IORedis
private queue!: Queue private queue!: Queue
private worker!: Worker private worker!: Worker
private io!: SocketServer private io!: SocketServer
private socket!: TSocket
public async boot(io: SocketServer) { public async boot(io: SocketServer, socket: TSocket) {
this.io = io this.io = io
this.socket = socket
this.connection = new IORedis(config.REDIS_URL, { this.connection = new IORedis(config.REDIS_URL, {
maxRetriesPerRequest: null maxRetriesPerRequest: null
@ -46,6 +49,20 @@ class QueueManager {
}) })
queueLogger.info('Queue manager loaded') 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) { private async processJob(job: Job) {

View File

@ -58,8 +58,7 @@ export class Server {
appLogger.error(`Socket.IO failed to start: ${error.message}`) appLogger.error(`Socket.IO failed to start: ${error.message}`)
} }
// Load queue manager
await QueueManager.boot(this.io)
// Add http API routes // Add http API routes
await addHttpRoutes(this.app) await addHttpRoutes(this.app)
@ -88,6 +87,8 @@ export class Server {
private async handleConnection(socket: TSocket) { private async handleConnection(socket: TSocket) {
const eventsPath = path.join(__dirname, 'socketEvents') const eventsPath = path.join(__dirname, 'socketEvents')
try { try {
// Load queue manager
await QueueManager.boot(this.io, socket);
await this.loadEventHandlers(eventsPath, socket) await this.loadEventHandlers(eventsPath, socket)
} catch (error: any) { } catch (error: any) {
appLogger.error(`Failed to load event handlers: ${error.message}`) appLogger.error(`Failed to load event handlers: ${error.message}`)