From 1229feb69cb2796e4fe486bf0b72367aee6d168a Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Thu, 9 Nov 2023 10:22:45 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=EF=B8=8F=20Refactor=20for=20zustan?= =?UTF-8?q?d=20and=20tanstack=20react=20query=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/jobqueue.service.ts | 91 +++++++++++++++++++++++++----------- services/settings.service.ts | 1 + 2 files changed, 64 insertions(+), 28 deletions(-) diff --git a/services/jobqueue.service.ts b/services/jobqueue.service.ts index 348e6d8..591665d 100644 --- a/services/jobqueue.service.ts +++ b/services/jobqueue.service.ts @@ -49,9 +49,14 @@ export default class JobQueueService extends Service { rest: "/GET enqueue", handler: async (ctx: Context<{}>) => { // Enqueue the job - const job = await this.localQueue(ctx, "enqueue.async", ctx.params, { - priority: 10, - }); + const job = await this.localQueue( + ctx, + "enqueue.async", + ctx.params, + { + priority: 10, + } + ); console.log(`Job ${job.id} enqueued`); return job.id; @@ -65,13 +70,17 @@ export default class JobQueueService extends Service { }> ) => { try { - console.log(`Recieved Job ID ${ctx.locals.job.id}, processing...`); + console.log( + `Recieved Job ID ${ctx.locals.job.id}, processing...` + ); console.log(ctx.params); // 1. De-structure the job params const { fileObject } = ctx.locals.job.data.params; // 2. Extract metadata from the archive - const result = await extractFromArchive(fileObject.filePath); + const result = await extractFromArchive( + fileObject.filePath + ); const { name, filePath, @@ -84,7 +93,9 @@ export default class JobQueueService extends Service { } = result; // 3a. Infer any issue-related metadata from the filename - const { inferredIssueDetails } = refineQuery(result.name); + const { inferredIssueDetails } = refineQuery( + result.name + ); console.log( "Issue metadata inferred: ", JSON.stringify(inferredIssueDetails, null, 2) @@ -124,7 +135,8 @@ export default class JobQueueService extends Service { // "acquisition.directconnect.downloads": [], // mark the metadata source - "acquisition.source.name": ctx.locals.job.data.params.sourcedFrom, + "acquisition.source.name": + ctx.locals.job.data.params.sourcedFrom, }; // 3c. Add the bundleId, if present to the payload @@ -135,8 +147,13 @@ export default class JobQueueService extends Service { // 3d. Add the sourcedMetadata, if present if ( - !isNil(ctx.locals.job.data.params.sourcedMetadata) && - !isUndefined(ctx.locals.job.data.params.sourcedMetadata.comicvine) + !isNil( + ctx.locals.job.data.params.sourcedMetadata + ) && + !isUndefined( + ctx.locals.job.data.params.sourcedMetadata + .comicvine + ) ) { Object.assign( payload.sourcedMetadata, @@ -145,11 +162,15 @@ export default class JobQueueService extends Service { } // 4. write to mongo - const importResult = await this.broker.call("library.rawImportToDB", { - importType: ctx.locals.job.data.params.importType, - bundleId, - payload, - }); + const importResult = await this.broker.call( + "library.rawImportToDB", + { + importType: + ctx.locals.job.data.params.importType, + bundleId, + payload, + } + ); return { data: { importResult, @@ -161,9 +182,14 @@ export default class JobQueueService extends Service { console.error( `An error occurred processing Job ID ${ctx.locals.job.id}` ); - throw new MoleculerError(error, 500, "IMPORT_JOB_ERROR", { - data: ctx.params.sessionId, - }); + throw new MoleculerError( + error, + 500, + "IMPORT_JOB_ERROR", + { + data: ctx.params.sessionId, + } + ); } }, }, @@ -191,7 +217,8 @@ export default class JobQueueService extends Service { statuses: { $push: { status: "$_id.status", - earliestTimestamp: "$earliestTimestamp", + earliestTimestamp: + "$earliestTimestamp", count: "$count", }, }, @@ -211,7 +238,10 @@ export default class JobQueueService extends Service { { $cond: [ { - $eq: ["$$this.status", "completed"], + $eq: [ + "$$this.status", + "completed", + ], }, "$$this.count", 0, @@ -231,7 +261,10 @@ export default class JobQueueService extends Service { { $cond: [ { - $eq: ["$$this.status", "failed"], + $eq: [ + "$$this.status", + "failed", + ], }, "$$this.count", 0, @@ -260,10 +293,10 @@ export default class JobQueueService extends Service { console.log("Queue drained."); await this.broker.call("socket.broadcast", { namespace: "/", - event: "action", + event: "LS_IMPORT_QUEUE_DRAINED", args: [ { - type: "LS_IMPORT_QUEUE_DRAINED", + message: "drained", }, ], }); @@ -274,14 +307,15 @@ export default class JobQueueService extends Service { // 2. Increment the completed job counter await pubClient.incr("completedJobCount"); // 3. Fetch the completed job count for the final payload to be sent to the client - const completedJobCount = await pubClient.get("completedJobCount"); + const completedJobCount = await pubClient.get( + "completedJobCount" + ); // 4. Emit the LS_COVER_EXTRACTED event with the necessary details await this.broker.call("socket.broadcast", { namespace: "/", - event: "action", + event: "LS_COVER_EXTRACTED", args: [ { - type: "LS_COVER_EXTRACTED", completedJobCount, importResult: job.returnvalue.data.importResult, }, @@ -302,7 +336,9 @@ export default class JobQueueService extends Service { async "enqueue.async.failed"(ctx) { const job = await this.job(ctx.params.id); await pubClient.incr("failedJobCount"); - const failedJobCount = await pubClient.get("failedJobCount"); + const failedJobCount = await pubClient.get( + "failedJobCount" + ); await JobResult.create({ id: ctx.params.id, @@ -315,10 +351,9 @@ export default class JobQueueService extends Service { // 4. Emit the LS_COVER_EXTRACTION_FAILED event with the necessary details await this.broker.call("socket.broadcast", { namespace: "/", - event: "action", + event: "LS_COVER_EXTRACTION_FAILED", args: [ { - type: "LS_COVER_EXTRACTION_FAILED", failedJobCount, importResult: job, }, diff --git a/services/settings.service.ts b/services/settings.service.ts index 2bcaf02..95c5e4e 100644 --- a/services/settings.service.ts +++ b/services/settings.service.ts @@ -57,6 +57,7 @@ export default class SettingsService extends Service { }> ) { try { + console.log(ctx.params); let query = {}; const { settingsKey, settingsObjectId } = ctx.params;