1
0
forked from noxious/server
This commit is contained in:
Dennis Postma 2024-09-30 23:31:00 +02:00
parent 7b3c4b92a5
commit 4cbd62cbb0
2 changed files with 28 additions and 30 deletions

View File

@ -20,6 +20,8 @@ class CommandManager {
this.rl.on('close', () => { this.rl.on('close', () => {
this.rlClosed = true this.rlClosed = true
}) })
console.log(process.cwd())
} }
public async boot(io: Server) { public async boot(io: Server) {
@ -61,17 +63,14 @@ class CommandManager {
} }
private async loadCommands() { private async loadCommands() {
const baseDir = config.ENV === 'production' ? path.join(__dirname, '..') : process.cwd(); const commandsDir = path.join(process.cwd(), 'commands');
const commandsDir = path.join(baseDir, config.ENV === 'production' ? 'commands' : 'src', 'commands');
const extension = config.ENV === 'production' ? '.js' : '.ts';
commandLogger.info(`Loading commands from: ${commandsDir}`); commandLogger.info(`Loading commands from: ${commandsDir}`);
try { try {
const files = await fs.promises.readdir(commandsDir, { withFileTypes: true }); const files = await fs.promises.readdir(commandsDir, { withFileTypes: true });
for (const file of files) { for (const file of files) {
if (!file.isFile() || !file.name.endsWith(extension)) { if (!file.isFile() || (!file.name.endsWith('.ts') && !file.name.endsWith('.js'))) {
continue; continue;
} }

View File

@ -49,42 +49,41 @@ class QueueManager {
} }
private async processJob(job: Job) { private async processJob(job: Job) {
const { jobName, params, socketId } = job.data; const { jobName, params, socketId } = job.data
const baseDir = config.ENV === 'production' ? path.join(__dirname, '..') : process.cwd();
const jobsDir = path.join(baseDir, config.ENV === 'production' ? 'jobs' : 'src', 'jobs');
const extension = config.ENV === 'production' ? '.js' : '.ts';
const jobPath = path.join(jobsDir, `${jobName}${extension}`);
queueLogger.info(`Processing job: ${jobName}`);
if (!fs.existsSync(jobPath)) {
queueLogger.warn(`Job file not found: ${jobPath}`);
return;
}
try { try {
const module = await import(jobPath); const jobsDir = path.join(process.cwd(), 'jobs')
if (typeof module.default !== 'function') { const extension = config.ENV === 'development' ? '.ts' : '.js'
queueLogger.warn(`Unrecognized export in ${jobName}`); const jobPath = path.join(jobsDir, `${jobName}${extension}`)
return;
if (!fs.existsSync(jobPath)) {
queueLogger.warn(`Job file not found: ${jobPath}`)
return
} }
const JobClass = module.default; const JobModule = await import(jobPath)
const jobInstance = new JobClass(params); const JobClass = JobModule.default
if (!JobClass || typeof JobClass !== 'function') {
queueLogger.warn(`Invalid job class in file: ${jobPath}`)
return
}
const jobInstance = new JobClass(params)
if (socketId && this.io) { if (socketId && this.io) {
const socket = this.io.sockets.sockets.get(socketId); const socket = this.io.sockets.sockets.get(socketId)
if (socket) { if (socket) {
await jobInstance.execute(this.io, socket); await jobInstance.execute(this.io, socket)
} else { } else {
queueLogger.warn(`Socket not found for job: ${socketId}`); queueLogger.warn(`Socket not found for job: ${socketId}`)
await jobInstance.execute(this.io); await jobInstance.execute(this.io)
} }
} else { } else {
await jobInstance.execute(this.io); await jobInstance.execute(this.io)
} }
} catch (error) { } catch (error: any) {
queueLogger.error(`Error processing job ${jobName}: ${error instanceof Error ? error.message : String(error)}`); queueLogger.error(`Error processing job ${jobName}: ${error.message}`)
} }
} }