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

View File

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