🔧 Refactoring the import queue process

This commit is contained in:
2022-04-18 15:22:34 -07:00
parent 38a80e41b3
commit 4c071f2fc7
2 changed files with 74 additions and 79 deletions

View File

@@ -34,12 +34,7 @@ SOFTWARE.
"use strict"; "use strict";
import { refineQuery } from "filename-parser"; import { refineQuery } from "filename-parser";
import { import { Context, Service, ServiceBroker, ServiceSchema } from "moleculer";
Context,
Service,
ServiceBroker,
ServiceSchema
} from "moleculer";
import BullMQMixin, { SandboxedJob } from "moleculer-bull"; import BullMQMixin, { SandboxedJob } from "moleculer-bull";
import { DbMixin } from "../mixins/db.mixin"; import { DbMixin } from "../mixins/db.mixin";
import Comic from "../models/comic.model"; import Comic from "../models/comic.model";
@@ -56,18 +51,14 @@ export default class QueueService extends Service {
schema: ServiceSchema<{}> = { name: "importqueue" } schema: ServiceSchema<{}> = { name: "importqueue" }
) { ) {
super(broker); super(broker);
console.log(this.io);
this.parseServiceSchema({ this.parseServiceSchema({
name: "importqueue", name: "importqueue",
mixins: [ mixins: [BullMQMixin(REDIS_URI), DbMixin("comics", Comic)],
BullMQMixin(REDIS_URI),
DbMixin("comics", Comic),
],
settings: {}, settings: {},
hooks: {}, hooks: {},
queues: { queues: {
"process.import": { "process.import": {
concurrency: 30, concurrency: 1,
async process(job: SandboxedJob) { async process(job: SandboxedJob) {
console.info("New job received!", job.data); console.info("New job received!", job.data);
console.info(`Processing queue...`); console.info(`Processing queue...`);
@@ -130,11 +121,12 @@ export default class QueueService extends Service {
}, },
} }
); );
return Promise.resolve({ console.log("JAMA", dbImportResult);
return {
dbImportResult, dbImportResult,
id: job.id, id: job.id,
worker: process.pid, worker: process.pid,
}); };
}, },
}, },
}, },
@@ -195,12 +187,11 @@ export default class QueueService extends Service {
await this.getQueue("process.import").on( await this.getQueue("process.import").on(
"completed", "completed",
async (job, res) => { async (job, res) => {
await this.broker.call('socket.broadcast', { await this.broker.call("socket.broadcast", {
namespace: '/', //optional namespace: "/", //optional
event: "action", event: "action",
args: [{ type: "LS_COVER_EXTRACTED", result: res }], //optional args: [{ type: "LS_COVER_EXTRACTED", result: res }], //optional
});
})
console.info(`Job with the id '${job.id}' completed.`); console.info(`Job with the id '${job.id}' completed.`);
} }
); );

View File

@@ -123,18 +123,20 @@ export default class ImportService extends Service {
let comicExists = await Comic.exists({ let comicExists = await Comic.exists({
"rawFileDetails.name": `${path.basename( "rawFileDetails.name": `${path.basename(
item.path, item.path,
path.extname(item.path), path.extname(item.path)
)}`, )}`,
}); });
if (!comicExists) { if (!comicExists) {
// 2. Send the extraction job to the queue // 2. Send the extraction job to the queue
await broker.call("importqueue.processImport", { await broker.call(
"importqueue.processImport",
{
fileObject: { fileObject: {
filePath: item.path, filePath: item.path,
fileSize: item.stats.size, fileSize: item.stats.size,
}, },
}); }
);
} else { } else {
console.log( console.log(
"Comic already exists in the library." "Comic already exists in the library."
@@ -169,45 +171,34 @@ export default class ImportService extends Service {
}; };
}> }>
) { ) {
try {
let volumeDetails; let volumeDetails;
const comicMetadata = ctx.params; const comicMetadata = ctx.params;
console.log(comicMetadata); console.log(JSON.stringify(comicMetadata, null, 4));
// When an issue is added from the search CV feature // When an issue is added from the search CV feature
if ( if (
comicMetadata.sourcedMetadata.comicvine && comicMetadata.sourcedMetadata.comicvine &&
!isNil( !isNil(
comicMetadata.sourcedMetadata.comicvine.volume comicMetadata.sourcedMetadata.comicvine
.volume
) )
) { ) {
volumeDetails = await this.broker.call( volumeDetails = await this.broker.call(
"comicvine.getVolumes", "comicvine.getVolumes",
{ {
volumeURI: volumeURI:
comicMetadata.sourcedMetadata.comicvine comicMetadata.sourcedMetadata
.volume.api_detail_url, .comicvine.volume
.api_detail_url,
} }
); );
comicMetadata.sourcedMetadata.comicvine.volumeInformation = comicMetadata.sourcedMetadata.comicvine.volumeInformation =
volumeDetails.results; volumeDetails.results;
} }
return await Comic.create( return await Comic.create(ctx.params);
ctx.params, } catch (error) {
(error, data) => { console.log(error);
if (data) {
return data;
} else if (error) {
console.log("data", data);
console.log("error", error);
throw new Errors.MoleculerError(
"Failed to import comic book",
400,
"IMS_FAILED_COMIC_BOOK_IMPORT",
error
);
} }
}
);
}, },
}, },
applyComicVineMetadata: { applyComicVineMetadata: {
@@ -552,16 +543,29 @@ export default class ImportService extends Service {
.drop() .drop()
.then(async (data) => { .then(async (data) => {
console.info(data); console.info(data);
const coversFolderDeleteResult = fsExtra.emptyDirSync( const coversFolderDeleteResult =
path.resolve(`${USERDATA_DIRECTORY}/covers`) fsExtra.emptyDirSync(
path.resolve(
`${USERDATA_DIRECTORY}/covers`
)
); );
const expandedFolderDeleteResult = fsExtra.emptyDirSync( const expandedFolderDeleteResult =
fsExtra.emptyDirSync(
path.resolve( path.resolve(
`${USERDATA_DIRECTORY}/expanded` `${USERDATA_DIRECTORY}/expanded`
) )
); );
const eSIndicesDeleteResult = await ctx.broker.call("search.deleteElasticSearchIndices", {}); const eSIndicesDeleteResult =
return { data, coversFolderDeleteResult, expandedFolderDeleteResult, eSIndicesDeleteResult }; await ctx.broker.call(
"search.deleteElasticSearchIndices",
{}
);
return {
data,
coversFolderDeleteResult,
expandedFolderDeleteResult,
eSIndicesDeleteResult,
};
}) })
.catch((error) => error); .catch((error) => error);
}, },