|
|
|
@ -6,21 +6,31 @@ import {
|
|
|
|
|
} from '@nestjs/bull'; |
|
|
|
|
import { Job } from 'bull'; |
|
|
|
|
import boxen from 'boxen'; |
|
|
|
|
import { OnEvent } from '@nestjs/event-emitter'; |
|
|
|
|
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter'; |
|
|
|
|
import { JobEvents, JOBS_QUEUE, JobStatus } from '../../../interface/Jobs'; |
|
|
|
|
import { JobsRedisService } from './jobs-redis.service'; |
|
|
|
|
|
|
|
|
|
@Processor(JOBS_QUEUE) |
|
|
|
|
export class JobsEventService { |
|
|
|
|
constructor(private jobsRedisService: JobsRedisService) {} |
|
|
|
|
constructor( |
|
|
|
|
private jobsRedisService: JobsRedisService, |
|
|
|
|
private eventEmitter: EventEmitter2, |
|
|
|
|
) {} |
|
|
|
|
|
|
|
|
|
@OnQueueActive() |
|
|
|
|
onActive(job: Job) { |
|
|
|
|
if (process.env['NC_WORKER_CONTAINER']) { |
|
|
|
|
this.jobsRedisService.publish(`jobs-${job.id.toString()}`, { |
|
|
|
|
cmd: JobEvents.STATUS, |
|
|
|
|
id: job.id.toString(), |
|
|
|
|
status: JobStatus.ACTIVE, |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
this.eventEmitter.emit(JobEvents.STATUS, { |
|
|
|
|
id: job.id.toString(), |
|
|
|
|
status: JobStatus.ACTIVE, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnQueueFailed() |
|
|
|
@ -36,6 +46,7 @@ export class JobsEventService {
|
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
if (process.env['NC_WORKER_CONTAINER']) { |
|
|
|
|
this.jobsRedisService.publish(`jobs-${job.id.toString()}`, { |
|
|
|
|
cmd: JobEvents.STATUS, |
|
|
|
|
id: job.id.toString(), |
|
|
|
@ -46,10 +57,23 @@ export class JobsEventService {
|
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
this.jobsRedisService.unsubscribe(`jobs-${job.id.toString()}`); |
|
|
|
|
this.eventEmitter.emit(JobEvents.STATUS, { |
|
|
|
|
id: job.id.toString(), |
|
|
|
|
status: JobStatus.FAILED, |
|
|
|
|
data: { |
|
|
|
|
error: { |
|
|
|
|
message: error?.message, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnQueueCompleted() |
|
|
|
|
onCompleted(job: Job, data: any) { |
|
|
|
|
if (process.env['NC_WORKER_CONTAINER']) { |
|
|
|
|
this.jobsRedisService.publish(`jobs-${job.id.toString()}`, { |
|
|
|
|
cmd: JobEvents.STATUS, |
|
|
|
|
id: job.id.toString(), |
|
|
|
@ -58,14 +82,26 @@ export class JobsEventService {
|
|
|
|
|
result: data, |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
this.jobsRedisService.unsubscribe(`jobs-${job.id.toString()}`); |
|
|
|
|
this.eventEmitter.emit(JobEvents.STATUS, { |
|
|
|
|
id: job.id.toString(), |
|
|
|
|
status: JobStatus.COMPLETED, |
|
|
|
|
data: { |
|
|
|
|
result: data, |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnEvent(JobEvents.LOG) |
|
|
|
|
onLog(data: { id: string; data: { message: string } }) { |
|
|
|
|
if (process.env['NC_WORKER_CONTAINER']) { |
|
|
|
|
this.jobsRedisService.publish(`jobs-${data.id}`, { |
|
|
|
|
cmd: JobEvents.LOG, |
|
|
|
|
id: data.id, |
|
|
|
|
data: data.data, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|