summary refs log tree commit diff
path: root/src/start.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-04-24 12:49:12 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-04-24 12:49:12 +0200
commitfe8a1c10005cc3becacf0c9601ecdc17f2a86e30 (patch)
tree5b406a7f497d05eb137ea8a0374e813a7bb904e1 /src/start.ts
parent:bug: fix type in Channel Messages (diff)
parentUpdate channels.ts (diff)
downloadserver-fe8a1c10005cc3becacf0c9601ecdc17f2a86e30.tar.xz
Merge branch 'master' of https://github.com/fosscord/fosscord-api
Diffstat (limited to 'src/start.ts')
-rw-r--r--src/start.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/start.ts b/src/start.ts
new file mode 100644

index 00000000..11e15941 --- /dev/null +++ b/src/start.ts
@@ -0,0 +1,33 @@ +process.on("uncaughtException", console.error); +process.on("unhandledRejection", console.error); + +import "missing-native-js-functions"; +import { config } from "dotenv"; +config(); +import { FosscordServer } from "./Server"; +import cluster from "cluster"; +import os from "os"; +const cores = os.cpus().length; + +if (cluster.isMaster && process.env.production == "true") { + console.log(`Primary ${process.pid} is running`); + + // Fork workers. + for (let i = 0; i < cores; i++) { + cluster.fork(); + } + + cluster.on("exit", (worker, code, signal) => { + console.log(`worker ${worker.process.pid} died, restart worker`); + cluster.fork(); + }); +} else { + var port = Number(process.env.PORT); + if (isNaN(port)) port = 1000; + + const server = new FosscordServer({ port }); + server.start().catch(console.error); + + // @ts-ignore + global.server = server; +}