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

View File

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