From 569b4a67f9f750b30190c77e09aeae8bc5e6b532 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 23 Dec 2019 23:12:44 +0100 Subject: [PATCH 1/2] Added findUser implementation - added findUser to the graphql resolvers - changed default limit to 20 - removed special worker logging - added error handling for worker initialization --- src/app.ts | 5 ++- src/graphql/resolvers.ts | 12 +++++++ src/graphql/schema.graphql | 5 +-- src/index.ts | 68 ++++++++------------------------------ 4 files changed, 28 insertions(+), 62 deletions(-) diff --git a/src/app.ts b/src/app.ts index c21518a..7bd1139 100644 --- a/src/app.ts +++ b/src/app.ts @@ -35,7 +35,7 @@ class App { this.app = express(); this.server = new http.Server(this.app); this.io = socketIo(this.server); - this.sequelize = new Sequelize(globals.config.database.connectionUri ); + this.sequelize = new Sequelize(globals.config.database.connectionUri); } /** @@ -82,11 +82,10 @@ class App { } this.app.use((req, res, next) => { logger.verbose(`${req.method} ${req.url}`); - process.send({cmd: "notifyRequest"}); next(); }); this.app.use(routes.router); - // listen for graphql requrest + // listen for graphql requests this.app.use("/graphql", graphqlHTTP((request, response) => { return { // @ts-ignore all diff --git a/src/graphql/resolvers.ts b/src/graphql/resolvers.ts index eec84e4..162370e 100644 --- a/src/graphql/resolvers.ts +++ b/src/graphql/resolvers.ts @@ -7,6 +7,7 @@ import globals from "../lib/globals"; import {InternalEvents} from "../lib/InternalEvents"; import * as models from "../lib/models"; import {is} from "../lib/regex"; +import {Op} from "sequelize"; /** * Returns the resolvers for the graphql api. @@ -15,6 +16,17 @@ import {is} from "../lib/regex"; */ export function resolver(req: any, res: any): any { return { + async findUser({first, offset, name, handle}: + {first: number, offset: number, name: string, handle: string}) { + if (name) { + return models.User.findAll({where: {username: {[Op.like]: `%${name}%`}}, offset, limit: first}); + } else if (handle) { + return models.User.findAll({where: {handle: {[Op.like]: `%${handle}%`}}, offset, limit: first}); + } else { + res.status(status.BAD_REQUEST); + return new GraphQLError("No search parameters provided."); + } + }, async getSelf() { if (req.session.userId) { return models.User.findByPk(req.session.userId); diff --git a/src/graphql/schema.graphql b/src/graphql/schema.graphql index e061f49..3ca17d9 100644 --- a/src/graphql/schema.graphql +++ b/src/graphql/schema.graphql @@ -17,11 +17,8 @@ type Query { "returns the request object for its id" getRequest(requestId: ID!): Request - "find a post by the posted date or content" - findPost(first: Int, offset: Int, text: String!, postedDate: String): [Post] - "find a user by user name or handle" - findUser(first: Int, offset: Int, name: String, handle: String): [User] + findUser(first: Int = 20, offset: Int = 0, name: String, handle: String): [User] "returns the post filtered by the sort type with pagination." getPosts(first: Int=20, offset: Int=0, sort: SortType = NEW): [Post] diff --git a/src/index.ts b/src/index.ts index 96c7c0d..119f82c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,61 +16,19 @@ interface IClusterData { if (cluster.isMaster) { console.log(`[CLUSTER-M] Master ${process.pid} is running`); - const clusterData: IClusterData = { - reqCount: 0, - workerCount: () => Object.keys(cluster.workers).length, - // @ts-ignore - workerRes: {}, - }; - setInterval(() => { - clusterData.workerRes.M = { - cpu: process.cpuUsage(), - mem: process.memoryUsage(), - }; - }, 1000); - - const log = (msg: string) => { - process.stdout.write(" ".padEnd(100) + "\r"); - process.stdout.write(msg); - process.stdout.write( - `W: ${clusterData.workerCount()},R: ${clusterData.reqCount},M: ${(() => { - let usageString = ""; - for (const [key, value] of Object.entries(clusterData.workerRes)) { - usageString += `${ - Math.round((value as IResourceUsage).mem.heapUsed / 100000) / 10}MB,`.padEnd(8); - } - return usageString; - })()}`.padEnd(99) + "\r"); - }; cluster.settings.silent = true; - cluster.on("exit", (worker, code, signal) => { - log(`[CLUSTER-M] Worker ${worker.process.pid} died!\n`); - delete clusterData.workerRes[worker.id]; - log("[CLUSTER-M] Starting new worker\n"); + cluster.on("exit", (worker, code) => { + console.error(`[CLUSTER-M] Worker ${worker.id} died! (code: ${code})`); + console.log("[CLUSTER-M] Starting new worker"); cluster.fork(); }); cluster.on("online", (worker) => { worker.process.stdout.on("data", (data) => { - log(`[CLUSTER-${worker.id}] ${data}`); + process.stdout.write(`[CLUSTER-${worker.id}] ${data}`); }); }); - cluster.on("message", (worker, message) => { - switch (message.cmd) { - case "notifyRequest": - clusterData.reqCount++; - log(""); - break; - case "notifyResources": - // @ts-ignore - clusterData.workerRes[worker.id] = message.data; - log(""); - break; - default: - break; - } - }); for (let i = 0; i < numCPUs; i++) { cluster.fork(); @@ -81,15 +39,15 @@ if (cluster.isMaster) { * async main function wrapper. */ (async () => { - setInterval(() => { - process.send({cmd: "notifyResources", data: { - cpu: process.cpuUsage(), - mem: process.memoryUsage(), - }}); - }, 1000); - const app = new App(cluster.worker.id); - await app.init(); - app.start(); + try { + const app = new App(cluster.worker.id); + await app.init(); + app.start(); + } catch (err) { + console.error(err.message); + console.error(err.stack); + process.exit(1); + } })(); console.log(`[CLUSTER] Worker ${process.pid} started`); From f103be95b3540782e8bc9252a924106d915f9a78 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 23 Dec 2019 23:15:29 +0100 Subject: [PATCH 2/2] Fixed style issue --- src/graphql/resolvers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/graphql/resolvers.ts b/src/graphql/resolvers.ts index e07b797..5b302aa 100644 --- a/src/graphql/resolvers.ts +++ b/src/graphql/resolvers.ts @@ -1,13 +1,13 @@ import {GraphQLError} from "graphql"; import * as status from "http-status"; import * as yaml from "js-yaml"; +import {Op} from "sequelize"; import dataaccess from "../lib/dataAccess"; import {NotLoggedInGqlError, PostNotFoundGqlError} from "../lib/errors/graphqlErrors"; import globals from "../lib/globals"; import {InternalEvents} from "../lib/InternalEvents"; import * as models from "../lib/models"; import {is} from "../lib/regex"; -import {Op} from "sequelize"; /** * Returns the resolvers for the graphql api.