🔌 reworked socket.io freeform with moleculer-io
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -71,3 +71,4 @@ userdata/
|
||||
erl_crash.dump
|
||||
temp
|
||||
test
|
||||
.nova
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { Server } from "socket.io";
|
||||
import { createServer } from "http";
|
||||
|
||||
export const SocketIOMixin = () => {
|
||||
const socketServer = createServer();
|
||||
socketServer.listen(3001, `0.0.0.0`);
|
||||
const socketIOConnection = new Server(socketServer, {
|
||||
cors: {
|
||||
origin: "*",
|
||||
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
|
||||
preflightContinue: false,
|
||||
optionsSuccessStatus: 204,
|
||||
},
|
||||
});
|
||||
return socketIOConnection;
|
||||
}
|
||||
119
package-lock.json
generated
119
package-lock.json
generated
@@ -45,6 +45,7 @@
|
||||
"p7zip-threetwo": "^1.0.4",
|
||||
"sharp": "^0.30.3",
|
||||
"socket.io": "^4.4.0",
|
||||
"socket.io-redis": "^6.1.1",
|
||||
"threetwo-ui-typings": "^1.0.14",
|
||||
"through2": "^4.0.2",
|
||||
"unrar": "^0.2.0",
|
||||
@@ -60,6 +61,7 @@
|
||||
"install": "^0.13.0",
|
||||
"jest": "^27.2.5",
|
||||
"jest-cli": "^27.2.5",
|
||||
"moleculer-io": "^2.0.0",
|
||||
"moleculer-repl": "^0.7.0",
|
||||
"npm": "^8.4.1",
|
||||
"ts-jest": "^25.3.0",
|
||||
@@ -9228,6 +9230,23 @@
|
||||
"mongoose": "^5.6.3"
|
||||
}
|
||||
},
|
||||
"node_modules/moleculer-io": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/moleculer-io/-/moleculer-io-2.0.0.tgz",
|
||||
"integrity": "sha512-de/t7cG5at5KYPiukkR+cEnZ0nb0xRmSHkCoLrd7mmHfNI8+FwoUbg1fE2+Yt5aoEnVhpvY7UE2x8qcBOT6T3w==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"kleur": "^4.1.4",
|
||||
"lodash": "^4.17.21",
|
||||
"socket.io": "^4.4.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 10.x.x"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"moleculer": "^0.13.0 || ^0.14.0"
|
||||
}
|
||||
},
|
||||
"node_modules/moleculer-repl": {
|
||||
"version": "0.7.0",
|
||||
"resolved": "https://registry.npmjs.org/moleculer-repl/-/moleculer-repl-0.7.0.tgz",
|
||||
@@ -9625,6 +9644,11 @@
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/notepack.io": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/notepack.io/-/notepack.io-2.2.0.tgz",
|
||||
"integrity": "sha512-9b5w3t5VSH6ZPosoYnyDONnUTF8o0UkBw7JLA6eBlYJWyGT1Q3vQa8Hmuj1/X6RYvHjjygBDgw6fJhe0JEojfw=="
|
||||
},
|
||||
"node_modules/npm": {
|
||||
"version": "8.4.1",
|
||||
"resolved": "https://registry.npmjs.org/npm/-/npm-8.4.1.tgz",
|
||||
@@ -13275,6 +13299,24 @@
|
||||
"recursive-watch": "bin.js"
|
||||
}
|
||||
},
|
||||
"node_modules/redis": {
|
||||
"version": "3.1.2",
|
||||
"resolved": "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz",
|
||||
"integrity": "sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw==",
|
||||
"dependencies": {
|
||||
"denque": "^1.5.0",
|
||||
"redis-commands": "^1.7.0",
|
||||
"redis-errors": "^1.2.0",
|
||||
"redis-parser": "^3.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/node-redis"
|
||||
}
|
||||
},
|
||||
"node_modules/redis-commands": {
|
||||
"version": "1.7.0",
|
||||
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz",
|
||||
@@ -13971,6 +14013,27 @@
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-redis": {
|
||||
"version": "6.1.1",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-redis/-/socket.io-redis-6.1.1.tgz",
|
||||
"integrity": "sha512-jeaXe3TGKC20GMSlPHEdwTUIWUpay/L7m5+S9TQcOf22p9Llx44/RkpJV08+buXTZ8E+aivOotj2RdeFJJWJJQ==",
|
||||
"deprecated": "This package has been renamed to '@socket.io/redis-adapter', please see the migration guide here: https://socket.io/docs/v4/redis-adapter/#migrating-from-socketio-redis",
|
||||
"dependencies": {
|
||||
"debug": "~4.3.1",
|
||||
"notepack.io": "~2.2.0",
|
||||
"redis": "^3.0.0",
|
||||
"socket.io-adapter": "~2.2.0",
|
||||
"uid2": "0.0.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-redis/node_modules/socket.io-adapter": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.2.0.tgz",
|
||||
"integrity": "sha512-rG49L+FwaVEwuAdeBRq49M97YI3ElVabJPzvHT9S6a2CWhDKnjSFasvwAwSYPRhQzfn4NtDIbCaGYgOCOU/rlg=="
|
||||
},
|
||||
"node_modules/source-map": {
|
||||
"version": "0.6.1",
|
||||
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
|
||||
@@ -14817,6 +14880,11 @@
|
||||
"node": ">=4.2.0"
|
||||
}
|
||||
},
|
||||
"node_modules/uid2": {
|
||||
"version": "0.0.3",
|
||||
"resolved": "https://registry.npmjs.org/uid2/-/uid2-0.0.3.tgz",
|
||||
"integrity": "sha1-SDEm4Rd03y9xuLY53NeZw3YWK4I="
|
||||
},
|
||||
"node_modules/unbox-primitive": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz",
|
||||
@@ -22287,6 +22355,17 @@
|
||||
"lodash": "^4.17.21"
|
||||
}
|
||||
},
|
||||
"moleculer-io": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/moleculer-io/-/moleculer-io-2.0.0.tgz",
|
||||
"integrity": "sha512-de/t7cG5at5KYPiukkR+cEnZ0nb0xRmSHkCoLrd7mmHfNI8+FwoUbg1fE2+Yt5aoEnVhpvY7UE2x8qcBOT6T3w==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"kleur": "^4.1.4",
|
||||
"lodash": "^4.17.21",
|
||||
"socket.io": "^4.4.1"
|
||||
}
|
||||
},
|
||||
"moleculer-repl": {
|
||||
"version": "0.7.0",
|
||||
"resolved": "https://registry.npmjs.org/moleculer-repl/-/moleculer-repl-0.7.0.tgz",
|
||||
@@ -22577,6 +22656,11 @@
|
||||
"resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
|
||||
"integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA=="
|
||||
},
|
||||
"notepack.io": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/notepack.io/-/notepack.io-2.2.0.tgz",
|
||||
"integrity": "sha512-9b5w3t5VSH6ZPosoYnyDONnUTF8o0UkBw7JLA6eBlYJWyGT1Q3vQa8Hmuj1/X6RYvHjjygBDgw6fJhe0JEojfw=="
|
||||
},
|
||||
"npm": {
|
||||
"version": "8.4.1",
|
||||
"resolved": "https://registry.npmjs.org/npm/-/npm-8.4.1.tgz",
|
||||
@@ -25197,6 +25281,17 @@
|
||||
"ttl": "^1.3.0"
|
||||
}
|
||||
},
|
||||
"redis": {
|
||||
"version": "3.1.2",
|
||||
"resolved": "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz",
|
||||
"integrity": "sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw==",
|
||||
"requires": {
|
||||
"denque": "^1.5.0",
|
||||
"redis-commands": "^1.7.0",
|
||||
"redis-errors": "^1.2.0",
|
||||
"redis-parser": "^3.0.0"
|
||||
}
|
||||
},
|
||||
"redis-commands": {
|
||||
"version": "1.7.0",
|
||||
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz",
|
||||
@@ -25708,6 +25803,25 @@
|
||||
"debug": "~4.3.1"
|
||||
}
|
||||
},
|
||||
"socket.io-redis": {
|
||||
"version": "6.1.1",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-redis/-/socket.io-redis-6.1.1.tgz",
|
||||
"integrity": "sha512-jeaXe3TGKC20GMSlPHEdwTUIWUpay/L7m5+S9TQcOf22p9Llx44/RkpJV08+buXTZ8E+aivOotj2RdeFJJWJJQ==",
|
||||
"requires": {
|
||||
"debug": "~4.3.1",
|
||||
"notepack.io": "~2.2.0",
|
||||
"redis": "^3.0.0",
|
||||
"socket.io-adapter": "~2.2.0",
|
||||
"uid2": "0.0.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"socket.io-adapter": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.2.0.tgz",
|
||||
"integrity": "sha512-rG49L+FwaVEwuAdeBRq49M97YI3ElVabJPzvHT9S6a2CWhDKnjSFasvwAwSYPRhQzfn4NtDIbCaGYgOCOU/rlg=="
|
||||
}
|
||||
}
|
||||
},
|
||||
"source-map": {
|
||||
"version": "0.6.1",
|
||||
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
|
||||
@@ -26374,6 +26488,11 @@
|
||||
"integrity": "sha512-w6fIxVE/H1PkLKcCPsFqKE7Kv7QUwhU8qQY2MueZXWx5cPZdwFupLgKK3vntcK98BtNHZtAF4LA/yl2a7k8R6Q==",
|
||||
"dev": true
|
||||
},
|
||||
"uid2": {
|
||||
"version": "0.0.3",
|
||||
"resolved": "https://registry.npmjs.org/uid2/-/uid2-0.0.3.tgz",
|
||||
"integrity": "sha1-SDEm4Rd03y9xuLY53NeZw3YWK4I="
|
||||
},
|
||||
"unbox-primitive": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz",
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
"install": "^0.13.0",
|
||||
"jest": "^27.2.5",
|
||||
"jest-cli": "^27.2.5",
|
||||
"moleculer-io": "^2.0.0",
|
||||
"moleculer-repl": "^0.7.0",
|
||||
"npm": "^8.4.1",
|
||||
"ts-jest": "^25.3.0",
|
||||
@@ -73,6 +74,7 @@
|
||||
"p7zip-threetwo": "^1.0.4",
|
||||
"sharp": "^0.30.3",
|
||||
"socket.io": "^4.4.0",
|
||||
"socket.io-redis": "^6.1.1",
|
||||
"threetwo-ui-typings": "^1.0.14",
|
||||
"through2": "^4.0.2",
|
||||
"unrar": "^0.2.0",
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
import { Service, ServiceBroker, Context } from "moleculer";
|
||||
import ApiGateway from "moleculer-web";
|
||||
import chokidar from "chokidar";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import { IExtractionOptions, IFolderData } from "threetwo-ui-typings";
|
||||
import { SocketIOMixin } from "../mixins/socket.io.mixin";
|
||||
import { debounce } from "lodash";
|
||||
export const io = SocketIOMixin();
|
||||
import { Service, ServiceBroker } from "moleculer";
|
||||
import ApiGateway from "moleculer-web";
|
||||
import path from "path";
|
||||
import { IFolderData } from "threetwo-ui-typings";
|
||||
|
||||
export default class ApiService extends Service {
|
||||
public constructor(broker: ServiceBroker) {
|
||||
super(broker);
|
||||
this.parseServiceSchema({
|
||||
name: "api",
|
||||
mixins: [ApiGateway, SocketIOMixin],
|
||||
mixins: [ApiGateway],
|
||||
// More info about settings: https://moleculer.services/docs/0.14/moleculer-web.html
|
||||
settings: {
|
||||
port: process.env.PORT || 3000,
|
||||
@@ -81,148 +78,114 @@ export default class ApiService extends Service {
|
||||
},
|
||||
},
|
||||
events: {
|
||||
"**"(payload, sender, event) {
|
||||
if (io)
|
||||
io.emit("event", {
|
||||
sender,
|
||||
event,
|
||||
payload,
|
||||
});
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
methods: {},
|
||||
started(): any {
|
||||
// Add a connect listener
|
||||
io.on("connection", (client) => {
|
||||
console.log("Client connected via websocket!");
|
||||
|
||||
client.on("action", async (action) => {
|
||||
switch (action.type) {
|
||||
case "LS_IMPORT":
|
||||
// 1. Send task to queue
|
||||
console.log(`Recieved ${action.type} event.`);
|
||||
await this.broker.call(
|
||||
"library.newImport",
|
||||
action.data,
|
||||
{}
|
||||
);
|
||||
break;
|
||||
case "LS_TOGGLE_IMPORT_QUEUE":
|
||||
await this.broker.call(
|
||||
"importqueue.toggleImportQueue",
|
||||
action.data,
|
||||
{}
|
||||
);
|
||||
break;
|
||||
}
|
||||
});
|
||||
// Add a disconnect listener
|
||||
client.on("disconnect", () => {
|
||||
console.log("Client disconnected");
|
||||
});
|
||||
|
||||
// Filewatcher
|
||||
const fileWatcher = chokidar.watch(
|
||||
path.resolve("/comics"),
|
||||
{
|
||||
ignored: (filePath) =>
|
||||
path.extname(filePath) === ".dctmp",
|
||||
persistent: true,
|
||||
usePolling: true,
|
||||
interval: 5000,
|
||||
ignoreInitial: true,
|
||||
followSymlinks: true,
|
||||
atomic: true,
|
||||
awaitWriteFinish: {
|
||||
stabilityThreshold: 2000,
|
||||
pollInterval: 100,
|
||||
},
|
||||
}
|
||||
);
|
||||
const fileCopyDelaySeconds = 3;
|
||||
const checkEnd = (path, prev) => {
|
||||
fs.stat(path, async (err, stat) => {
|
||||
// Filewatcher
|
||||
const fileWatcher = chokidar.watch(
|
||||
path.resolve("/comics"),
|
||||
{
|
||||
ignored: (filePath) =>
|
||||
path.extname(filePath) === ".dctmp",
|
||||
persistent: true,
|
||||
usePolling: true,
|
||||
interval: 5000,
|
||||
ignoreInitial: true,
|
||||
followSymlinks: true,
|
||||
atomic: true,
|
||||
awaitWriteFinish: {
|
||||
stabilityThreshold: 2000,
|
||||
pollInterval: 100,
|
||||
},
|
||||
}
|
||||
);
|
||||
const fileCopyDelaySeconds = 3;
|
||||
const checkEnd = (path, prev) => {
|
||||
fs.stat(path, async (err, stat) => {
|
||||
// Replace error checking with something appropriate for your app.
|
||||
if (err) throw err;
|
||||
if (stat.mtime.getTime() === prev.mtime.getTime()) {
|
||||
console.log("finished");
|
||||
// Move on: call whatever needs to be called to process the file.
|
||||
console.log(
|
||||
"File detected, starting import..."
|
||||
);
|
||||
const walkedFolder: IFolderData =
|
||||
await broker.call("library.walkFolders", {
|
||||
basePathToWalk: path,
|
||||
});
|
||||
await this.broker.call(
|
||||
"importqueue.processImport",
|
||||
{
|
||||
fileObject: {
|
||||
filePath: path,
|
||||
fileSize: walkedFolder[0].fileSize,
|
||||
},
|
||||
}
|
||||
);
|
||||
} else
|
||||
setTimeout(
|
||||
checkEnd,
|
||||
fileCopyDelaySeconds,
|
||||
path,
|
||||
stat
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
fileWatcher
|
||||
.once("add", (path, stats) => {
|
||||
console.log("Watcher detected new files.");
|
||||
console.log(
|
||||
`File ${path} has been added with stats: ${JSON.stringify(
|
||||
stats,
|
||||
null,
|
||||
2
|
||||
)}`
|
||||
);
|
||||
|
||||
console.log("File", path, "has been added");
|
||||
|
||||
fs.stat(path, function(err, stat) {
|
||||
// Replace error checking with something appropriate for your app.
|
||||
if (err) throw err;
|
||||
if (stat.mtime.getTime() === prev.mtime.getTime()) {
|
||||
console.log("finished");
|
||||
// Move on: call whatever needs to be called to process the file.
|
||||
console.log(
|
||||
"File detected, starting import..."
|
||||
);
|
||||
const walkedFolder: IFolderData =
|
||||
await broker.call("library.walkFolders", {
|
||||
basePathToWalk: path,
|
||||
});
|
||||
await this.broker.call(
|
||||
"importqueue.processImport",
|
||||
{
|
||||
fileObject: {
|
||||
filePath: path,
|
||||
fileSize: walkedFolder[0].fileSize,
|
||||
},
|
||||
}
|
||||
);
|
||||
} else
|
||||
setTimeout(
|
||||
checkEnd,
|
||||
fileCopyDelaySeconds,
|
||||
path,
|
||||
stat
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
fileWatcher
|
||||
.once("add", (path, stats) => {
|
||||
console.log("Watcher detected new files.");
|
||||
console.log(
|
||||
`File ${path} has been added with stats: ${JSON.stringify(
|
||||
stats,
|
||||
null,
|
||||
2
|
||||
)}`
|
||||
setTimeout(
|
||||
checkEnd,
|
||||
fileCopyDelaySeconds,
|
||||
path,
|
||||
stat
|
||||
);
|
||||
});
|
||||
})
|
||||
// .once(
|
||||
// "change",
|
||||
|
||||
console.log("File", path, "has been added");
|
||||
// (path, stats) =>
|
||||
// console.log(
|
||||
// `File ${path} has been changed. Stats: ${JSON.stringify(
|
||||
// stats,
|
||||
// null,
|
||||
// 2
|
||||
// )}`
|
||||
// )
|
||||
// )
|
||||
.once(
|
||||
"unlink",
|
||||
|
||||
fs.stat(path, function (err, stat) {
|
||||
// Replace error checking with something appropriate for your app.
|
||||
if (err) throw err;
|
||||
setTimeout(
|
||||
checkEnd,
|
||||
fileCopyDelaySeconds,
|
||||
path,
|
||||
stat
|
||||
);
|
||||
});
|
||||
})
|
||||
// .once(
|
||||
// "change",
|
||||
(path) =>
|
||||
console.log(`File ${path} has been removed`)
|
||||
)
|
||||
.once(
|
||||
"addDir",
|
||||
|
||||
// (path, stats) =>
|
||||
// console.log(
|
||||
// `File ${path} has been changed. Stats: ${JSON.stringify(
|
||||
// stats,
|
||||
// null,
|
||||
// 2
|
||||
// )}`
|
||||
// )
|
||||
// )
|
||||
.once(
|
||||
"unlink",
|
||||
(path) =>
|
||||
console.log(`Directory ${path} has been added`)
|
||||
);
|
||||
|
||||
(path) =>
|
||||
console.log(`File ${path} has been removed`)
|
||||
)
|
||||
.once(
|
||||
"addDir",
|
||||
|
||||
(path) =>
|
||||
console.log(`Directory ${path} has been added`)
|
||||
);
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
@@ -33,24 +33,21 @@ SOFTWARE.
|
||||
|
||||
"use strict";
|
||||
|
||||
import { refineQuery } from "filename-parser";
|
||||
import {
|
||||
Context,
|
||||
Service,
|
||||
ServiceBroker,
|
||||
ServiceSchema,
|
||||
Errors,
|
||||
ServiceSchema
|
||||
} from "moleculer";
|
||||
|
||||
import BullMQMixin from "moleculer-bull";
|
||||
import { SandboxedJob } from "moleculer-bull";
|
||||
import BullMQMixin, { SandboxedJob } from "moleculer-bull";
|
||||
import { DbMixin } from "../mixins/db.mixin";
|
||||
import Comic from "../models/comic.model";
|
||||
import { extractFromArchive } from "../utils/uncompression.utils";
|
||||
import { refineQuery } from "filename-parser";
|
||||
import { io } from "./api.service";
|
||||
import { USERDATA_DIRECTORY } from "../constants/directories";
|
||||
import { IExtractedComicBookCoverFile } from "threetwo-ui-typings";
|
||||
|
||||
const REDIS_URI = process.env.REDIS_URI || `redis://localhost:6379`;
|
||||
const EventEmitter = require("events");
|
||||
EventEmitter.defaultMaxListeners = 20;
|
||||
|
||||
console.log(`REDIS -> ${REDIS_URI}`);
|
||||
export default class QueueService extends Service {
|
||||
@@ -59,9 +56,13 @@ export default class QueueService extends Service {
|
||||
schema: ServiceSchema<{}> = { name: "importqueue" }
|
||||
) {
|
||||
super(broker);
|
||||
console.log(this.io);
|
||||
this.parseServiceSchema({
|
||||
name: "importqueue",
|
||||
mixins: [BullMQMixin(REDIS_URI), DbMixin("comics", Comic)],
|
||||
mixins: [
|
||||
BullMQMixin(REDIS_URI),
|
||||
DbMixin("comics", Comic),
|
||||
],
|
||||
settings: {},
|
||||
hooks: {},
|
||||
queues: {
|
||||
@@ -75,7 +76,7 @@ export default class QueueService extends Service {
|
||||
const result = await extractFromArchive(
|
||||
job.data.fileObject.filePath
|
||||
);
|
||||
|
||||
|
||||
const {
|
||||
name,
|
||||
filePath,
|
||||
@@ -177,45 +178,42 @@ export default class QueueService extends Service {
|
||||
unarchiveComicBook: {
|
||||
rest: "POST /unarchiveComicBook",
|
||||
params: {},
|
||||
handler: async (ctx: Context<{}>) => {},
|
||||
handler: async (ctx: Context<{}>) => { },
|
||||
},
|
||||
},
|
||||
methods: {},
|
||||
async started(): Promise<any> {
|
||||
io.on("connection", async (client) => {
|
||||
await this.getQueue("process.import").on(
|
||||
"failed",
|
||||
async (job, error) => {
|
||||
console.error(
|
||||
`An error occured in 'process.import' queue on job id '${job.id}': ${error.message}`
|
||||
);
|
||||
console.error(job.data);
|
||||
}
|
||||
);
|
||||
await this.getQueue("process.import").on(
|
||||
"completed",
|
||||
async (job, res) => {
|
||||
client.emit("action", {
|
||||
type: "LS_COVER_EXTRACTED",
|
||||
result: res,
|
||||
});
|
||||
console.info(
|
||||
`Job with the id '${job.id}' completed.`
|
||||
);
|
||||
}
|
||||
);
|
||||
await this.getQueue("process.import").on(
|
||||
"stalled",
|
||||
async (job) => {
|
||||
console.warn(
|
||||
`The job with the id '${job.id} got stalled!`
|
||||
);
|
||||
console.log(`${JSON.stringify(job, null, 2)}`);
|
||||
console.log(`is stalled.`);
|
||||
}
|
||||
);
|
||||
await this.getQueue("process.import").on(
|
||||
"failed",
|
||||
async (job, error) => {
|
||||
console.error(
|
||||
`An error occured in 'process.import' queue on job id '${job.id}': ${error.message}`
|
||||
);
|
||||
console.error(job.data);
|
||||
}
|
||||
);
|
||||
await this.getQueue("process.import").on(
|
||||
"completed",
|
||||
async (job, res) => {
|
||||
await this.broker.call('socket.broadcast', {
|
||||
namespace: '/', //optional
|
||||
event: "action",
|
||||
args: [{ type: "LS_COVER_EXTRACTED", result: res }], //optional
|
||||
|
||||
});
|
||||
})
|
||||
console.info(`Job with the id '${job.id}' completed.`);
|
||||
}
|
||||
);
|
||||
await this.getQueue("process.import").on(
|
||||
"stalled",
|
||||
async (job) => {
|
||||
console.warn(
|
||||
`The job with the id '${job.id} got stalled!`
|
||||
);
|
||||
console.log(`${JSON.stringify(job, null, 2)}`);
|
||||
console.log(`is stalled.`);
|
||||
}
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
78
services/socket.service.ts
Normal file
78
services/socket.service.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
"use strict";
|
||||
import {
|
||||
Service,
|
||||
ServiceBroker,
|
||||
ServiceSchema
|
||||
} from "moleculer";
|
||||
const redisAdapter = require("socket.io-redis");
|
||||
const SocketIOService = require("moleculer-io");
|
||||
|
||||
export default class SocketService extends Service {
|
||||
// @ts-ignore
|
||||
public constructor(
|
||||
public broker: ServiceBroker,
|
||||
schema: ServiceSchema<{}> = { name: "socket" }
|
||||
) {
|
||||
super(broker);
|
||||
this.parseServiceSchema(
|
||||
Service.mergeSchemas(
|
||||
{
|
||||
name: "socket",
|
||||
mixins: [SocketIOService],
|
||||
settings: {
|
||||
port: process.env.PORT || 3001,
|
||||
io: {
|
||||
namespaces: {
|
||||
"/": {
|
||||
events: {
|
||||
call: {
|
||||
// whitelist: ["math.*", "say.*", "accounts.*", "rooms.*", "io.*"],
|
||||
},
|
||||
action: async (data, ack) => {
|
||||
// write your handler function here.
|
||||
console.log(
|
||||
JSON.stringify(data, null, 2)
|
||||
);
|
||||
|
||||
switch (data.type) {
|
||||
case "LS_IMPORT":
|
||||
console.log(
|
||||
`Recieved ${data.type} event.`
|
||||
);
|
||||
// 1. Send task to queue
|
||||
await this.broker.call(
|
||||
"library.newImport",
|
||||
data.data,
|
||||
{}
|
||||
);
|
||||
break;
|
||||
case "LS_TOGGLE_IMPORT_QUEUE":
|
||||
await this.broker.call(
|
||||
"importqueue.toggleImportQueue",
|
||||
data.data,
|
||||
{}
|
||||
);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
options: {
|
||||
adapter: redisAdapter({ host: 'localhost', port: 6379 }),
|
||||
},
|
||||
},
|
||||
},
|
||||
hooks: {},
|
||||
actions: {},
|
||||
methods: {},
|
||||
async started() {
|
||||
this.io.on("connection", (data) => console.log("Connected to socket.io server."))
|
||||
}
|
||||
},
|
||||
schema
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
70
yarn.lock
70
yarn.lock
@@ -795,9 +795,9 @@
|
||||
"version" "1.0.2"
|
||||
|
||||
"@seald-io/nedb@^2.0.4":
|
||||
"integrity" "sha512-whkcx3hpcowNhoSEbIsrfe8TXxDwyj8SJJut2EqF7DSX2GGqQlL7Ix/vzwwOo4FJolzDhZD2DaUTVKmTQS3Rog=="
|
||||
"resolved" "https://registry.npmjs.org/@seald-io/nedb/-/nedb-2.2.0.tgz"
|
||||
"version" "2.2.0"
|
||||
"integrity" "sha512-z91J3pem4ENzHuu9BilOSdlGL2S14OQYePPdvBcPHgHr+s51VIUwQARcOjx21KvtkA27vEpgPqzrVKh7nSlIfw=="
|
||||
"resolved" "https://registry.npmjs.org/@seald-io/nedb/-/nedb-2.2.2.tgz"
|
||||
"version" "2.2.2"
|
||||
dependencies:
|
||||
"@seald-io/binary-search-tree" "^1.0.2"
|
||||
"async" "0.2.10"
|
||||
@@ -845,13 +845,6 @@
|
||||
"resolved" "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.2.tgz"
|
||||
"version" "1.0.2"
|
||||
|
||||
"@types/axios@^0.14.0":
|
||||
"integrity" "sha1-7CMA++fX3d1+udOr+HmZlkyvzkY="
|
||||
"resolved" "https://registry.npmjs.org/@types/axios/-/axios-0.14.0.tgz"
|
||||
"version" "0.14.0"
|
||||
dependencies:
|
||||
"axios" "*"
|
||||
|
||||
"@types/babel__core@^7.0.0", "@types/babel__core@^7.1.14":
|
||||
"integrity" "sha512-S7unDjm/C7z2A2R9NzfKCK1I+BAALDtxEmsJBwlB3EzNfb929ykjL++1CK9LO++EIp2fQrC8O+BwjKvz6UeDyQ=="
|
||||
"resolved" "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.18.tgz"
|
||||
@@ -1405,7 +1398,7 @@
|
||||
"@babel/runtime" "^7.15.4"
|
||||
"is-retry-allowed" "^2.2.0"
|
||||
|
||||
"axios@*", "axios@^0.25.0":
|
||||
"axios@^0.25.0":
|
||||
"integrity" "sha512-cD8FOb0tRH3uuEe6+evtAbgJtfxr7ly3fQjYcMcuPlgkwVS9xboaVIpcDV+cYQe+yGykgwZCs1pzjntcGa6l5g=="
|
||||
"resolved" "https://registry.npmjs.org/axios/-/axios-0.25.0.tgz"
|
||||
"version" "0.25.0"
|
||||
@@ -2373,7 +2366,7 @@
|
||||
"resolved" "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz"
|
||||
"version" "1.0.0"
|
||||
|
||||
"denque@^1.1.0", "denque@^1.4.1":
|
||||
"denque@^1.1.0", "denque@^1.4.1", "denque@^1.5.0":
|
||||
"integrity" "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw=="
|
||||
"resolved" "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz"
|
||||
"version" "1.5.1"
|
||||
@@ -5017,7 +5010,7 @@
|
||||
"fakerator" "^0.3.4"
|
||||
"mongodb" "^3.6.7"
|
||||
|
||||
"moleculer-db-adapter-mongoose@^0.8.9":
|
||||
"moleculer-db-adapter-mongoose@^0.8.12":
|
||||
"integrity" "sha512-bWxpkHH7YFLoqBYD12Nl7hd/fmF+gVbC9Xc+dtW8QF68hm+iul6yTTcJVVWm7OZ4zRykBuuj6sssqPzsie+MfQ=="
|
||||
"resolved" "https://registry.npmjs.org/moleculer-db-adapter-mongoose/-/moleculer-db-adapter-mongoose-0.8.12.tgz"
|
||||
"version" "0.8.12"
|
||||
@@ -5025,7 +5018,7 @@
|
||||
"bluebird" "^3.7.2"
|
||||
"lodash" "^4.17.21"
|
||||
|
||||
"moleculer-db@^0.8.13":
|
||||
"moleculer-db@^0.8.17":
|
||||
"integrity" "sha512-LygIZhhYgqK0UQ9TWOfBdl0iQg2+vRBmTgu7aK3HTDSEVF0qYfxV2HEOB6SEiJXiCnINvZHl9fuIvbdfAHZCnw=="
|
||||
"resolved" "https://registry.npmjs.org/moleculer-db/-/moleculer-db-0.8.17.tgz"
|
||||
"version" "0.8.17"
|
||||
@@ -5035,6 +5028,15 @@
|
||||
"flat" "^5.0.2"
|
||||
"lodash" "^4.17.21"
|
||||
|
||||
"moleculer-io@^2.0.0":
|
||||
"integrity" "sha512-de/t7cG5at5KYPiukkR+cEnZ0nb0xRmSHkCoLrd7mmHfNI8+FwoUbg1fE2+Yt5aoEnVhpvY7UE2x8qcBOT6T3w=="
|
||||
"resolved" "https://registry.npmjs.org/moleculer-io/-/moleculer-io-2.0.0.tgz"
|
||||
"version" "2.0.0"
|
||||
dependencies:
|
||||
"kleur" "^4.1.4"
|
||||
"lodash" "^4.17.21"
|
||||
"socket.io" "^4.4.1"
|
||||
|
||||
"moleculer-repl@^0.7.0":
|
||||
"integrity" "sha512-Spb1OzUSjt/NJ6Y/rfB644j4UXdx3d9iX8gO0EXlIimsf9Fnz/U4A8GojV+Z8+tTgaG1sXquWCqVNwxEqYeuqw=="
|
||||
"resolved" "https://registry.npmjs.org/moleculer-repl/-/moleculer-repl-0.7.0.tgz"
|
||||
@@ -5312,6 +5314,11 @@
|
||||
"resolved" "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz"
|
||||
"version" "3.0.0"
|
||||
|
||||
"notepack.io@^2.0.0", "notepack.io@~2.2.0":
|
||||
"integrity" "sha512-9b5w3t5VSH6ZPosoYnyDONnUTF8o0UkBw7JLA6eBlYJWyGT1Q3vQa8Hmuj1/X6RYvHjjygBDgw6fJhe0JEojfw=="
|
||||
"resolved" "https://registry.npmjs.org/notepack.io/-/notepack.io-2.2.0.tgz"
|
||||
"version" "2.2.0"
|
||||
|
||||
"npm-audit-report@*":
|
||||
"version" "2.1.5"
|
||||
dependencies:
|
||||
@@ -6086,7 +6093,7 @@
|
||||
dependencies:
|
||||
"ttl" "^1.3.0"
|
||||
|
||||
"redis-commands@1.7.0":
|
||||
"redis-commands@^1.7.0", "redis-commands@1.7.0":
|
||||
"integrity" "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ=="
|
||||
"resolved" "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz"
|
||||
"version" "1.7.0"
|
||||
@@ -6103,6 +6110,16 @@
|
||||
dependencies:
|
||||
"redis-errors" "^1.0.0"
|
||||
|
||||
"redis@^3.0.0":
|
||||
"integrity" "sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw=="
|
||||
"resolved" "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz"
|
||||
"version" "3.1.2"
|
||||
dependencies:
|
||||
"denque" "^1.5.0"
|
||||
"redis-commands" "^1.7.0"
|
||||
"redis-errors" "^1.2.0"
|
||||
"redis-parser" "^3.0.0"
|
||||
|
||||
"regenerator-runtime@^0.13.4":
|
||||
"integrity" "sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA=="
|
||||
"resolved" "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz"
|
||||
@@ -6475,6 +6492,11 @@
|
||||
"smart-buffer@^4.1.0":
|
||||
"version" "4.2.0"
|
||||
|
||||
"socket.io-adapter@~2.2.0":
|
||||
"integrity" "sha512-rG49L+FwaVEwuAdeBRq49M97YI3ElVabJPzvHT9S6a2CWhDKnjSFasvwAwSYPRhQzfn4NtDIbCaGYgOCOU/rlg=="
|
||||
"resolved" "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.2.0.tgz"
|
||||
"version" "2.2.0"
|
||||
|
||||
"socket.io-adapter@~2.3.3":
|
||||
"integrity" "sha512-Qd/iwn3VskrpNO60BeRyCyr8ZWw9CPZyitW4AQwmRZ8zCiyDiL+znRnWX6tDHXnWn1sJrM1+b6Mn6wEDJJ4aYQ=="
|
||||
"resolved" "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.3.3.tgz"
|
||||
@@ -6489,7 +6511,18 @@
|
||||
"component-emitter" "~1.3.0"
|
||||
"debug" "~4.3.1"
|
||||
|
||||
"socket.io@^4.4.0":
|
||||
"socket.io-redis@^6.1.1":
|
||||
"integrity" "sha512-jeaXe3TGKC20GMSlPHEdwTUIWUpay/L7m5+S9TQcOf22p9Llx44/RkpJV08+buXTZ8E+aivOotj2RdeFJJWJJQ=="
|
||||
"resolved" "https://registry.npmjs.org/socket.io-redis/-/socket.io-redis-6.1.1.tgz"
|
||||
"version" "6.1.1"
|
||||
dependencies:
|
||||
"debug" "~4.3.1"
|
||||
"notepack.io" "~2.2.0"
|
||||
"redis" "^3.0.0"
|
||||
"socket.io-adapter" "~2.2.0"
|
||||
"uid2" "0.0.3"
|
||||
|
||||
"socket.io@^4.4.0", "socket.io@^4.4.1":
|
||||
"integrity" "sha512-s04vrBswdQBUmuWJuuNTmXUVJhP0cVky8bBDhdkf8y0Ptsu7fKU2LuLbts9g+pdmAdyMMn8F/9Mf1/wbtUN0fg=="
|
||||
"resolved" "https://registry.npmjs.org/socket.io/-/socket.io-4.4.1.tgz"
|
||||
"version" "4.4.1"
|
||||
@@ -7172,6 +7205,11 @@
|
||||
"resolved" "https://registry.npmjs.org/typescript/-/typescript-4.5.4.tgz"
|
||||
"version" "4.5.4"
|
||||
|
||||
"uid2@0.0.3":
|
||||
"integrity" "sha1-SDEm4Rd03y9xuLY53NeZw3YWK4I="
|
||||
"resolved" "https://registry.npmjs.org/uid2/-/uid2-0.0.3.tgz"
|
||||
"version" "0.0.3"
|
||||
|
||||
"unbox-primitive@^1.0.1":
|
||||
"integrity" "sha512-tZU/3NqK3dA5gpE1KtyiJUrEB0lxnGkMFHptJ7q6ewdZ8s12QrODwNbhIJStmJkd1QDXa1NRA8aF2A1zk/Ypyw=="
|
||||
"resolved" "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz"
|
||||
|
||||
Reference in New Issue
Block a user