Merge branch 'julius-dev' of Software_Engineering_I/greenvironment-server into master

pull/5/head
Trivernis 5 years ago committed by Gitea
commit d5a622dea9

@ -94,11 +94,10 @@ class App {
}); });
this.app.use((req, res, next) => { this.app.use((req, res, next) => {
logger.verbose(`${req.method} ${req.url}`); logger.verbose(`${req.method} ${req.url}`);
process.send({cmd: "notifyRequest"});
next(); next();
}); });
this.app.use(routes.router); this.app.use(routes.router);
// listen for graphql requrest // listen for graphql requests
this.app.use("/graphql", graphqlHTTP((request, response) => { this.app.use("/graphql", graphqlHTTP((request, response) => {
return { return {
// @ts-ignore all // @ts-ignore all

@ -1,6 +1,7 @@
import {GraphQLError} from "graphql"; import {GraphQLError} from "graphql";
import * as status from "http-status"; import * as status from "http-status";
import * as yaml from "js-yaml"; import * as yaml from "js-yaml";
import {Op} from "sequelize";
import dataaccess from "../lib/dataAccess"; import dataaccess from "../lib/dataAccess";
import {NotLoggedInGqlError, PostNotFoundGqlError} from "../lib/errors/graphqlErrors"; import {NotLoggedInGqlError, PostNotFoundGqlError} from "../lib/errors/graphqlErrors";
import globals from "../lib/globals"; import globals from "../lib/globals";
@ -15,6 +16,17 @@ import {is} from "../lib/regex";
*/ */
export function resolver(req: any, res: any): any { export function resolver(req: any, res: any): any {
return { 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() { async getSelf() {
if (req.session.userId) { if (req.session.userId) {
return models.User.findByPk(req.session.userId); return models.User.findByPk(req.session.userId);

@ -17,11 +17,8 @@ type Query {
"returns the request object for its id" "returns the request object for its id"
getRequest(requestId: ID!): Request 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" "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." "returns the post filtered by the sort type with pagination."
getPosts(first: Int=20, offset: Int=0, sort: SortType = NEW): [Post] getPosts(first: Int=20, offset: Int=0, sort: SortType = NEW): [Post]

@ -16,61 +16,19 @@ interface IClusterData {
if (cluster.isMaster) { if (cluster.isMaster) {
console.log(`[CLUSTER-M] Master ${process.pid} is running`); 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.settings.silent = true;
cluster.on("exit", (worker, code, signal) => { cluster.on("exit", (worker, code) => {
log(`[CLUSTER-M] Worker ${worker.process.pid} died!\n`); console.error(`[CLUSTER-M] Worker ${worker.id} died! (code: ${code})`);
delete clusterData.workerRes[worker.id]; console.log("[CLUSTER-M] Starting new worker");
log("[CLUSTER-M] Starting new worker\n");
cluster.fork(); cluster.fork();
}); });
cluster.on("online", (worker) => { cluster.on("online", (worker) => {
worker.process.stdout.on("data", (data) => { 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++) { for (let i = 0; i < numCPUs; i++) {
cluster.fork(); cluster.fork();
@ -81,15 +39,15 @@ if (cluster.isMaster) {
* async main function wrapper. * async main function wrapper.
*/ */
(async () => { (async () => {
setInterval(() => { try {
process.send({cmd: "notifyResources", data: {
cpu: process.cpuUsage(),
mem: process.memoryUsage(),
}});
}, 1000);
const app = new App(cluster.worker.id); const app = new App(cluster.worker.id);
await app.init(); await app.init();
app.start(); app.start();
} catch (err) {
console.error(err.message);
console.error(err.stack);
process.exit(1);
}
})(); })();
console.log(`[CLUSTER] Worker ${process.pid} started`); console.log(`[CLUSTER] Worker ${process.pid} started`);

Loading…
Cancel
Save