better worker task handling, implement progress updates
This commit is contained in:
1 parent
c11e412e81
commit
62d1d34937
1 file changed
+34
-7
@@ -141,31 +141,48 @@ class NewJobQueue {
|
|||||||
return async (job) => {
|
return async (job) => {
|
||||||
try {
|
try {
|
||||||
// Get all maintenance windows for this monitor
|
// Get all maintenance windows for this monitor
|
||||||
|
await job.updateProgress(0);
|
||||||
const monitorId = job.data._id;
|
const monitorId = job.data._id;
|
||||||
const maintenanceWindowActive = await this.isInMaintenanceWindow(monitorId);
|
const maintenanceWindowActive = await this.isInMaintenanceWindow(monitorId);
|
||||||
// If a maintenance window is active, we're done
|
// If a maintenance window is active, we're done
|
||||||
|
|
||||||
if (maintenanceWindowActive) {
|
if (maintenanceWindowActive) {
|
||||||
|
await job.updateProgress(100);
|
||||||
this.logger.info({
|
this.logger.info({
|
||||||
message: `Monitor ${monitorId} is in maintenance window`,
|
message: `Monitor ${monitorId} is in maintenance window`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
method: "createWorker",
|
method: "createWorker",
|
||||||
});
|
});
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the current status
|
// Get the current status
|
||||||
|
await job.updateProgress(30);
|
||||||
const networkResponse = await this.networkService.getStatus(job);
|
const networkResponse = await this.networkService.getStatus(job);
|
||||||
// Handle status change
|
// Handle status change
|
||||||
|
await job.updateProgress(60);
|
||||||
const { monitor, statusChanged, prevStatus } =
|
const { monitor, statusChanged, prevStatus } =
|
||||||
await this.statusService.updateStatus(networkResponse);
|
await this.statusService.updateStatus(networkResponse);
|
||||||
// Handle notifications
|
// Handle notifications
|
||||||
this.notificationService.handleNotifications({
|
await job.updateProgress(80);
|
||||||
...networkResponse,
|
this.notificationService
|
||||||
monitor,
|
.handleNotifications({
|
||||||
prevStatus,
|
...networkResponse,
|
||||||
statusChanged,
|
monitor,
|
||||||
});
|
prevStatus,
|
||||||
|
statusChanged,
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
this.logger.error({
|
||||||
|
message: error.message,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "createJobHandler",
|
||||||
|
details: `Error sending notifications for job ${job.id}: ${error.message}`,
|
||||||
|
stack: error.stack,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
await job.updateProgress(100);
|
||||||
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
message: error.message,
|
message: error.message,
|
||||||
@@ -174,6 +191,7 @@ class NewJobQueue {
|
|||||||
details: `Error processing job ${job.id}: ${error.message}`,
|
details: `Error processing job ${job.id}: ${error.message}`,
|
||||||
stack: error.stack,
|
stack: error.stack,
|
||||||
});
|
});
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -195,6 +213,15 @@ class NewJobQueue {
|
|||||||
createWorker(queue) {
|
createWorker(queue) {
|
||||||
const worker = new this.Worker(queue.name, this.createJobHandler(), {
|
const worker = new this.Worker(queue.name, this.createJobHandler(), {
|
||||||
connection: this.connection,
|
connection: this.connection,
|
||||||
|
concurrency: 5,
|
||||||
|
});
|
||||||
|
worker.on("failed", (job, err) => {
|
||||||
|
this.logger.error({
|
||||||
|
message: `Worker failed job: ${job.id}`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "createWorker",
|
||||||
|
stack: err.stack,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
return worker;
|
return worker;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user