🔌 reworked socket.io freeform with moleculer-io

This commit is contained in:
2022-04-16 15:56:24 -07:00
parent 4b9ba28739
commit 9548db5d63
8 changed files with 398 additions and 215 deletions

1
.gitignore vendored
View File

@@ -71,3 +71,4 @@ userdata/
erl_crash.dump erl_crash.dump
temp temp
test test
.nova

View File

@@ -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
View File

@@ -45,6 +45,7 @@
"p7zip-threetwo": "^1.0.4", "p7zip-threetwo": "^1.0.4",
"sharp": "^0.30.3", "sharp": "^0.30.3",
"socket.io": "^4.4.0", "socket.io": "^4.4.0",
"socket.io-redis": "^6.1.1",
"threetwo-ui-typings": "^1.0.14", "threetwo-ui-typings": "^1.0.14",
"through2": "^4.0.2", "through2": "^4.0.2",
"unrar": "^0.2.0", "unrar": "^0.2.0",
@@ -60,6 +61,7 @@
"install": "^0.13.0", "install": "^0.13.0",
"jest": "^27.2.5", "jest": "^27.2.5",
"jest-cli": "^27.2.5", "jest-cli": "^27.2.5",
"moleculer-io": "^2.0.0",
"moleculer-repl": "^0.7.0", "moleculer-repl": "^0.7.0",
"npm": "^8.4.1", "npm": "^8.4.1",
"ts-jest": "^25.3.0", "ts-jest": "^25.3.0",
@@ -9228,6 +9230,23 @@
"mongoose": "^5.6.3" "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": { "node_modules/moleculer-repl": {
"version": "0.7.0", "version": "0.7.0",
"resolved": "https://registry.npmjs.org/moleculer-repl/-/moleculer-repl-0.7.0.tgz", "resolved": "https://registry.npmjs.org/moleculer-repl/-/moleculer-repl-0.7.0.tgz",
@@ -9625,6 +9644,11 @@
"node": ">=0.10.0" "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": { "node_modules/npm": {
"version": "8.4.1", "version": "8.4.1",
"resolved": "https://registry.npmjs.org/npm/-/npm-8.4.1.tgz", "resolved": "https://registry.npmjs.org/npm/-/npm-8.4.1.tgz",
@@ -13275,6 +13299,24 @@
"recursive-watch": "bin.js" "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": { "node_modules/redis-commands": {
"version": "1.7.0", "version": "1.7.0",
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz", "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz",
@@ -13971,6 +14013,27 @@
"node": ">=10.0.0" "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": { "node_modules/source-map": {
"version": "0.6.1", "version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
@@ -14817,6 +14880,11 @@
"node": ">=4.2.0" "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": { "node_modules/unbox-primitive": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz",
@@ -22287,6 +22355,17 @@
"lodash": "^4.17.21" "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": { "moleculer-repl": {
"version": "0.7.0", "version": "0.7.0",
"resolved": "https://registry.npmjs.org/moleculer-repl/-/moleculer-repl-0.7.0.tgz", "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", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
"integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==" "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": { "npm": {
"version": "8.4.1", "version": "8.4.1",
"resolved": "https://registry.npmjs.org/npm/-/npm-8.4.1.tgz", "resolved": "https://registry.npmjs.org/npm/-/npm-8.4.1.tgz",
@@ -25197,6 +25281,17 @@
"ttl": "^1.3.0" "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": { "redis-commands": {
"version": "1.7.0", "version": "1.7.0",
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz", "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz",
@@ -25708,6 +25803,25 @@
"debug": "~4.3.1" "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": { "source-map": {
"version": "0.6.1", "version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
@@ -26374,6 +26488,11 @@
"integrity": "sha512-w6fIxVE/H1PkLKcCPsFqKE7Kv7QUwhU8qQY2MueZXWx5cPZdwFupLgKK3vntcK98BtNHZtAF4LA/yl2a7k8R6Q==", "integrity": "sha512-w6fIxVE/H1PkLKcCPsFqKE7Kv7QUwhU8qQY2MueZXWx5cPZdwFupLgKK3vntcK98BtNHZtAF4LA/yl2a7k8R6Q==",
"dev": true "dev": true
}, },
"uid2": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/uid2/-/uid2-0.0.3.tgz",
"integrity": "sha1-SDEm4Rd03y9xuLY53NeZw3YWK4I="
},
"unbox-primitive": { "unbox-primitive": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz",

View File

@@ -29,6 +29,7 @@
"install": "^0.13.0", "install": "^0.13.0",
"jest": "^27.2.5", "jest": "^27.2.5",
"jest-cli": "^27.2.5", "jest-cli": "^27.2.5",
"moleculer-io": "^2.0.0",
"moleculer-repl": "^0.7.0", "moleculer-repl": "^0.7.0",
"npm": "^8.4.1", "npm": "^8.4.1",
"ts-jest": "^25.3.0", "ts-jest": "^25.3.0",
@@ -73,6 +74,7 @@
"p7zip-threetwo": "^1.0.4", "p7zip-threetwo": "^1.0.4",
"sharp": "^0.30.3", "sharp": "^0.30.3",
"socket.io": "^4.4.0", "socket.io": "^4.4.0",
"socket.io-redis": "^6.1.1",
"threetwo-ui-typings": "^1.0.14", "threetwo-ui-typings": "^1.0.14",
"through2": "^4.0.2", "through2": "^4.0.2",
"unrar": "^0.2.0", "unrar": "^0.2.0",

View File

@@ -1,19 +1,16 @@
import { Service, ServiceBroker, Context } from "moleculer";
import ApiGateway from "moleculer-web";
import chokidar from "chokidar"; import chokidar from "chokidar";
import path from "path";
import fs from "fs"; import fs from "fs";
import { IExtractionOptions, IFolderData } from "threetwo-ui-typings"; import { Service, ServiceBroker } from "moleculer";
import { SocketIOMixin } from "../mixins/socket.io.mixin"; import ApiGateway from "moleculer-web";
import { debounce } from "lodash"; import path from "path";
export const io = SocketIOMixin(); import { IFolderData } from "threetwo-ui-typings";
export default class ApiService extends Service { export default class ApiService extends Service {
public constructor(broker: ServiceBroker) { public constructor(broker: ServiceBroker) {
super(broker); super(broker);
this.parseServiceSchema({ this.parseServiceSchema({
name: "api", name: "api",
mixins: [ApiGateway, SocketIOMixin], mixins: [ApiGateway],
// More info about settings: https://moleculer.services/docs/0.14/moleculer-web.html // More info about settings: https://moleculer.services/docs/0.14/moleculer-web.html
settings: { settings: {
port: process.env.PORT || 3000, port: process.env.PORT || 3000,
@@ -81,148 +78,114 @@ export default class ApiService extends Service {
}, },
}, },
events: { events: {
"**"(payload, sender, event) {
if (io)
io.emit("event", {
sender,
event,
payload,
});
},
}, },
methods: {}, methods: {},
started(): any { 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 // Filewatcher
const fileWatcher = chokidar.watch( const fileWatcher = chokidar.watch(
path.resolve("/comics"), path.resolve("/comics"),
{ {
ignored: (filePath) => ignored: (filePath) =>
path.extname(filePath) === ".dctmp", path.extname(filePath) === ".dctmp",
persistent: true, persistent: true,
usePolling: true, usePolling: true,
interval: 5000, interval: 5000,
ignoreInitial: true, ignoreInitial: true,
followSymlinks: true, followSymlinks: true,
atomic: true, atomic: true,
awaitWriteFinish: { awaitWriteFinish: {
stabilityThreshold: 2000, stabilityThreshold: 2000,
pollInterval: 100, pollInterval: 100,
}, },
} }
); );
const fileCopyDelaySeconds = 3; const fileCopyDelaySeconds = 3;
const checkEnd = (path, prev) => { const checkEnd = (path, prev) => {
fs.stat(path, async (err, stat) => { 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. // Replace error checking with something appropriate for your app.
if (err) throw err; if (err) throw err;
if (stat.mtime.getTime() === prev.mtime.getTime()) { setTimeout(
console.log("finished"); checkEnd,
// Move on: call whatever needs to be called to process the file. fileCopyDelaySeconds,
console.log( path,
"File detected, starting import..." stat
);
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
)}`
); );
});
})
// .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) { (path) =>
// Replace error checking with something appropriate for your app. console.log(`File ${path} has been removed`)
if (err) throw err; )
setTimeout( .once(
checkEnd, "addDir",
fileCopyDelaySeconds,
path,
stat
);
});
})
// .once(
// "change",
// (path, stats) => (path) =>
// console.log( console.log(`Directory ${path} has been added`)
// `File ${path} has been changed. Stats: ${JSON.stringify( );
// stats,
// null,
// 2
// )}`
// )
// )
.once(
"unlink",
(path) =>
console.log(`File ${path} has been removed`)
)
.once(
"addDir",
(path) =>
console.log(`Directory ${path} has been added`)
);
});
}, },
}); });
} }

View File

@@ -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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 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 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"; "use strict";
import { refineQuery } from "filename-parser";
import { import {
Context, Context,
Service, Service,
ServiceBroker, ServiceBroker,
ServiceSchema, ServiceSchema
Errors,
} from "moleculer"; } from "moleculer";
import BullMQMixin, { SandboxedJob } from "moleculer-bull";
import BullMQMixin from "moleculer-bull";
import { 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";
import { extractFromArchive } from "../utils/uncompression.utils"; 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 REDIS_URI = process.env.REDIS_URI || `redis://localhost:6379`;
const EventEmitter = require("events");
EventEmitter.defaultMaxListeners = 20;
console.log(`REDIS -> ${REDIS_URI}`); console.log(`REDIS -> ${REDIS_URI}`);
export default class QueueService extends Service { export default class QueueService extends Service {
@@ -59,9 +56,13 @@ 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: [BullMQMixin(REDIS_URI), DbMixin("comics", Comic)], mixins: [
BullMQMixin(REDIS_URI),
DbMixin("comics", Comic),
],
settings: {}, settings: {},
hooks: {}, hooks: {},
queues: { queues: {
@@ -75,7 +76,7 @@ export default class QueueService extends Service {
const result = await extractFromArchive( const result = await extractFromArchive(
job.data.fileObject.filePath job.data.fileObject.filePath
); );
const { const {
name, name,
filePath, filePath,
@@ -177,45 +178,42 @@ export default class QueueService extends Service {
unarchiveComicBook: { unarchiveComicBook: {
rest: "POST /unarchiveComicBook", rest: "POST /unarchiveComicBook",
params: {}, params: {},
handler: async (ctx: Context<{}>) => {}, handler: async (ctx: Context<{}>) => { },
}, },
}, },
methods: {}, methods: {},
async started(): Promise<any> { async started(): Promise<any> {
io.on("connection", async (client) => { await this.getQueue("process.import").on(
await this.getQueue("process.import").on( "failed",
"failed", async (job, error) => {
async (job, error) => { console.error(
console.error( `An error occured in 'process.import' queue on job id '${job.id}': ${error.message}`
`An error occured in 'process.import' queue on job id '${job.id}': ${error.message}` );
); console.error(job.data);
console.error(job.data); }
} );
); 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', {
client.emit("action", { namespace: '/', //optional
type: "LS_COVER_EXTRACTED", event: "action",
result: res, 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.`);
}
);
}); })
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.`);
}
);
}, },
}); });
} }

View 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
)
);
}
}

View File

@@ -795,9 +795,9 @@
"version" "1.0.2" "version" "1.0.2"
"@seald-io/nedb@^2.0.4": "@seald-io/nedb@^2.0.4":
"integrity" "sha512-whkcx3hpcowNhoSEbIsrfe8TXxDwyj8SJJut2EqF7DSX2GGqQlL7Ix/vzwwOo4FJolzDhZD2DaUTVKmTQS3Rog==" "integrity" "sha512-z91J3pem4ENzHuu9BilOSdlGL2S14OQYePPdvBcPHgHr+s51VIUwQARcOjx21KvtkA27vEpgPqzrVKh7nSlIfw=="
"resolved" "https://registry.npmjs.org/@seald-io/nedb/-/nedb-2.2.0.tgz" "resolved" "https://registry.npmjs.org/@seald-io/nedb/-/nedb-2.2.2.tgz"
"version" "2.2.0" "version" "2.2.2"
dependencies: dependencies:
"@seald-io/binary-search-tree" "^1.0.2" "@seald-io/binary-search-tree" "^1.0.2"
"async" "0.2.10" "async" "0.2.10"
@@ -845,13 +845,6 @@
"resolved" "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.2.tgz" "resolved" "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.2.tgz"
"version" "1.0.2" "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": "@types/babel__core@^7.0.0", "@types/babel__core@^7.1.14":
"integrity" "sha512-S7unDjm/C7z2A2R9NzfKCK1I+BAALDtxEmsJBwlB3EzNfb929ykjL++1CK9LO++EIp2fQrC8O+BwjKvz6UeDyQ==" "integrity" "sha512-S7unDjm/C7z2A2R9NzfKCK1I+BAALDtxEmsJBwlB3EzNfb929ykjL++1CK9LO++EIp2fQrC8O+BwjKvz6UeDyQ=="
"resolved" "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.18.tgz" "resolved" "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.18.tgz"
@@ -1405,7 +1398,7 @@
"@babel/runtime" "^7.15.4" "@babel/runtime" "^7.15.4"
"is-retry-allowed" "^2.2.0" "is-retry-allowed" "^2.2.0"
"axios@*", "axios@^0.25.0": "axios@^0.25.0":
"integrity" "sha512-cD8FOb0tRH3uuEe6+evtAbgJtfxr7ly3fQjYcMcuPlgkwVS9xboaVIpcDV+cYQe+yGykgwZCs1pzjntcGa6l5g==" "integrity" "sha512-cD8FOb0tRH3uuEe6+evtAbgJtfxr7ly3fQjYcMcuPlgkwVS9xboaVIpcDV+cYQe+yGykgwZCs1pzjntcGa6l5g=="
"resolved" "https://registry.npmjs.org/axios/-/axios-0.25.0.tgz" "resolved" "https://registry.npmjs.org/axios/-/axios-0.25.0.tgz"
"version" "0.25.0" "version" "0.25.0"
@@ -2373,7 +2366,7 @@
"resolved" "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz" "resolved" "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz"
"version" "1.0.0" "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==" "integrity" "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw=="
"resolved" "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz" "resolved" "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz"
"version" "1.5.1" "version" "1.5.1"
@@ -5017,7 +5010,7 @@
"fakerator" "^0.3.4" "fakerator" "^0.3.4"
"mongodb" "^3.6.7" "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==" "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" "resolved" "https://registry.npmjs.org/moleculer-db-adapter-mongoose/-/moleculer-db-adapter-mongoose-0.8.12.tgz"
"version" "0.8.12" "version" "0.8.12"
@@ -5025,7 +5018,7 @@
"bluebird" "^3.7.2" "bluebird" "^3.7.2"
"lodash" "^4.17.21" "lodash" "^4.17.21"
"moleculer-db@^0.8.13": "moleculer-db@^0.8.17":
"integrity" "sha512-LygIZhhYgqK0UQ9TWOfBdl0iQg2+vRBmTgu7aK3HTDSEVF0qYfxV2HEOB6SEiJXiCnINvZHl9fuIvbdfAHZCnw==" "integrity" "sha512-LygIZhhYgqK0UQ9TWOfBdl0iQg2+vRBmTgu7aK3HTDSEVF0qYfxV2HEOB6SEiJXiCnINvZHl9fuIvbdfAHZCnw=="
"resolved" "https://registry.npmjs.org/moleculer-db/-/moleculer-db-0.8.17.tgz" "resolved" "https://registry.npmjs.org/moleculer-db/-/moleculer-db-0.8.17.tgz"
"version" "0.8.17" "version" "0.8.17"
@@ -5035,6 +5028,15 @@
"flat" "^5.0.2" "flat" "^5.0.2"
"lodash" "^4.17.21" "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": "moleculer-repl@^0.7.0":
"integrity" "sha512-Spb1OzUSjt/NJ6Y/rfB644j4UXdx3d9iX8gO0EXlIimsf9Fnz/U4A8GojV+Z8+tTgaG1sXquWCqVNwxEqYeuqw==" "integrity" "sha512-Spb1OzUSjt/NJ6Y/rfB644j4UXdx3d9iX8gO0EXlIimsf9Fnz/U4A8GojV+Z8+tTgaG1sXquWCqVNwxEqYeuqw=="
"resolved" "https://registry.npmjs.org/moleculer-repl/-/moleculer-repl-0.7.0.tgz" "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" "resolved" "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz"
"version" "3.0.0" "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@*": "npm-audit-report@*":
"version" "2.1.5" "version" "2.1.5"
dependencies: dependencies:
@@ -6086,7 +6093,7 @@
dependencies: dependencies:
"ttl" "^1.3.0" "ttl" "^1.3.0"
"redis-commands@1.7.0": "redis-commands@^1.7.0", "redis-commands@1.7.0":
"integrity" "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ==" "integrity" "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ=="
"resolved" "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz" "resolved" "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz"
"version" "1.7.0" "version" "1.7.0"
@@ -6103,6 +6110,16 @@
dependencies: dependencies:
"redis-errors" "^1.0.0" "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": "regenerator-runtime@^0.13.4":
"integrity" "sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==" "integrity" "sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA=="
"resolved" "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz" "resolved" "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz"
@@ -6475,6 +6492,11 @@
"smart-buffer@^4.1.0": "smart-buffer@^4.1.0":
"version" "4.2.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": "socket.io-adapter@~2.3.3":
"integrity" "sha512-Qd/iwn3VskrpNO60BeRyCyr8ZWw9CPZyitW4AQwmRZ8zCiyDiL+znRnWX6tDHXnWn1sJrM1+b6Mn6wEDJJ4aYQ==" "integrity" "sha512-Qd/iwn3VskrpNO60BeRyCyr8ZWw9CPZyitW4AQwmRZ8zCiyDiL+znRnWX6tDHXnWn1sJrM1+b6Mn6wEDJJ4aYQ=="
"resolved" "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.3.3.tgz" "resolved" "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.3.3.tgz"
@@ -6489,7 +6511,18 @@
"component-emitter" "~1.3.0" "component-emitter" "~1.3.0"
"debug" "~4.3.1" "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==" "integrity" "sha512-s04vrBswdQBUmuWJuuNTmXUVJhP0cVky8bBDhdkf8y0Ptsu7fKU2LuLbts9g+pdmAdyMMn8F/9Mf1/wbtUN0fg=="
"resolved" "https://registry.npmjs.org/socket.io/-/socket.io-4.4.1.tgz" "resolved" "https://registry.npmjs.org/socket.io/-/socket.io-4.4.1.tgz"
"version" "4.4.1" "version" "4.4.1"
@@ -7172,6 +7205,11 @@
"resolved" "https://registry.npmjs.org/typescript/-/typescript-4.5.4.tgz" "resolved" "https://registry.npmjs.org/typescript/-/typescript-4.5.4.tgz"
"version" "4.5.4" "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": "unbox-primitive@^1.0.1":
"integrity" "sha512-tZU/3NqK3dA5gpE1KtyiJUrEB0lxnGkMFHptJ7q6ewdZ8s12QrODwNbhIJStmJkd1QDXa1NRA8aF2A1zk/Ypyw==" "integrity" "sha512-tZU/3NqK3dA5gpE1KtyiJUrEB0lxnGkMFHptJ7q6ewdZ8s12QrODwNbhIJStmJkd1QDXa1NRA8aF2A1zk/Ypyw=="
"resolved" "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz" "resolved" "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.1.tgz"