🔧 Replaced rabbitMQ with bullMQ

This commit is contained in:
2021-10-26 15:07:42 -07:00
parent 755ac43820
commit 289b2ec3bd
8 changed files with 858 additions and 296 deletions

View File

@@ -5,6 +5,7 @@ import { logger } from "../utils/logger.utils";
import path from "path";
import fs from "fs";
import { IExtractionOptions, IFolderData } from "threetwo-ui-typings";
import IO from "socket.io";
export default class ApiService extends Service {
public constructor(broker: ServiceBroker) {
super(broker);
@@ -15,7 +16,6 @@ export default class ApiService extends Service {
// More info about settings: https://moleculer.services/docs/0.14/moleculer-web.html
settings: {
port: process.env.PORT || 3000,
routes: [
{
path: "/api",
@@ -77,10 +77,67 @@ export default class ApiService extends Service {
options: {},
},
},
events: {},
events: {
"**"(payload, sender, event) {
if (this.io)
this.io.emit("event", {
sender,
event,
payload,
});
},
},
methods: {},
started(): any {
// Socket gateway-ish
// Create a Socket.IO instance, passing it our server
this.io = new IO.Server(3001);
// Add a connect listener
this.io.on("connection", (client) => {
this.logger.info("Client connected via websocket!");
client.on("action", (action, done) => {
console.log(action);
switch (action.type) {
case "LS_IMPORT":
this.broker
.call(
"import.processAndImportToDB",
action.data,
{}
)
.then((res) => {
if (done) done(res);
})
.catch((err) => this.logger.error(err));
break;
}
});
// client.on("call", ({ action, params, opts }, done) => {
// this.logger.info(
// "Received request from client! Action:",
// action,
// ", Params:",
// params
// );
// this.broker
// .call(action, params, opts)
// .then((res) => {
// if (done) done(res);
// })
// .catch((err) => this.logger.error(err));
// });
client.on("disconnect", () => {
this.logger.info("Client disconnected");
});
});
// Filewatcher
const fileWatcher = chokidar.watch(path.resolve("./comics"), {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
@@ -94,38 +151,66 @@ export default class ApiService extends Service {
const fileCopyDelaySeconds = 10;
const checkFileCopyComplete = (path, previousPath) => {
fs.stat(path, async (err, stat) => {
if (err) { throw err; }
if (stat.mtime.getTime() === previousPath.mtime.getTime()) {
logger.info('File copy complete, starting import...');
const walkedFolders: IFolderData = await broker.call("import.walkFolders", { basePathToWalk: path });
if (err) {
throw err;
}
if (
stat.mtime.getTime() ===
previousPath.mtime.getTime()
) {
logger.info(
"File copy complete, starting import..."
);
const walkedFolders: IFolderData =
await broker.call("import.walkFolders", {
basePathToWalk: path,
});
const extractionOptions: IExtractionOptions = {
extractTarget: "cover",
targetExtractionFolder: "./userdata/covers",
extractionMode: "single",
};
await this.broker.call("import.processAndImportToDB", { walkedFolders, extractionOptions });
await this.broker.call(
"import.processAndImportToDB",
{ walkedFolders, extractionOptions }
);
} else {
setTimeout(checkFileCopyComplete, fileCopyDelaySeconds * 1000, path, stat);
setTimeout(
checkFileCopyComplete,
fileCopyDelaySeconds * 1000,
path,
stat
);
}
})
}
});
};
fileWatcher
.on("add", async (path, stats) => {
logger.info("Watcher detected new files.")
logger.info("Watcher detected new files.");
logger.info(
`File ${path} has been added with stats: ${JSON.stringify(
stats
)}`
);
logger.info('File copy started...');
logger.info("File copy started...");
fs.stat(path, function (err, stat) {
if (err) {
logger.error('Error watching file for copy completion. ERR: ' + err.message);
logger.error('Error file not processed. PATH: ' + path);
logger.error(
"Error watching file for copy completion. ERR: " +
err.message
);
logger.error(
"Error file not processed. PATH: " + path
);
throw err;
}
setTimeout(checkFileCopyComplete, fileCopyDelaySeconds * 1000, path, stat);
setTimeout(
checkFileCopyComplete,
fileCopyDelaySeconds * 1000,
path,
stat
);
});
})
.on("change", (path, stats) =>