🔧 Refactor and added getJobCounts

This commit is contained in:
2023-08-23 11:47:47 -05:00
parent 8b584080e2
commit 6ee609f2b9
2 changed files with 43 additions and 24 deletions

View File

@@ -23,6 +23,13 @@ export default class JobQueueService extends Service {
},
},
actions: {
getJobCountsByType: {
rest: "GET /getJobCountsByType",
handler: async (ctx: Context<{}>) => {
console.log(ctx.params);
return await this.$resolve("jobqueue").getJobCounts();
}
},
toggle: {
rest: "GET /toggle",
handler: async (ctx: Context<{ action: String }>) => {
@@ -203,35 +210,35 @@ export default class JobQueueService extends Service {
status: "completed",
failedReason: {},
});
// 6. Purge it from Redis
await job.remove();
console.log(`Job ID ${ctx.params.id} completed.`);
},
async "enqueue.async.failed"(ctx) {
const jobState = await this.job(ctx.params.id);
const job = await this.job(ctx.params.id);
await pubClient.incr("failedJobCount");
const failedJobCount = await pubClient.get("failedJobCount");
await JobResult.create({
id: ctx.params.id,
status: "failed",
failedReason: jobState.failedReason,
failedReason: job.failedReason,
});
// 4. Emit the LS_COVER_EXTRACTION_FAILED event with the necessary details
await this.broker.call("socket.broadcast", {
namespace: "/", //optional
namespace: "/",
event: "action",
args: [
{
type: "LS_COVER_EXTRACTION_FAILED",
failedJobCount,
importResult: jobState,
importResult: job,
},
], //optional
],
});
},
},
});

View File

@@ -1,5 +1,6 @@
"use strict";
import { Service, ServiceBroker, ServiceSchema, Context } from "moleculer";
import {JobType} from "moleculer-bullmq";
import { createClient } from "redis";
import { createAdapter } from "@socket.io/redis-adapter";
import Session from "../models/session.model";
@@ -36,27 +37,38 @@ export default class SocketService extends Service {
const sessionRecord = await Session.find({
sessionId: data.session.sessionId,
});
// check for sessionId's existence
// 1. Check for sessionId's existence, and a match
if (
sessionRecord.length !== 0 &&
sessionRecord[0].sessionId ===
data.session.sessionId
data.session.sessionId
) {
// 1. Get job counts
console.log("yea?")
const completedJobCount = await pubClient.get("completedJobCount");
const failedJobCount = await pubClient.get("failedJobCount");
await this.broker.call("socket.broadcast", {
namespace: "/", //optional
event: "action",
args: [
{
type: "RESTORE_JOB_COUNTS_AFTER_SESSION_RESTORATION",
completedJobCount,
failedJobCount,
},
],
});
// 2. Find if the queue has active jobs
const jobs: JobType = await this.broker.call("jobqueue.getJobCountsByType", {})
const { active, prioritized } = jobs;
if (active > 0 && prioritized > 0) {
// 3. Get job counts
const completedJobCount = await pubClient.get("completedJobCount");
const failedJobCount = await pubClient.get("failedJobCount");
// 4. Send the counts to the active socket.io session
await this.broker.call("socket.broadcast", {
namespace: "/",
event: "action",
args: [
{
type: "RESTORE_JOB_COUNTS_AFTER_SESSION_RESTORATION",
completedJobCount,
failedJobCount,
queueStatus: "running",
},
],
});
}
}
} catch (err) {
throw new MoleculerError(