-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: pulling cluster services to top level and removing handler …
…naming as isnt just everything?
- Loading branch information
Showing
112 changed files
with
694 additions
and
802 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// @ts-ignore | ||
import { Command } from 'commander' | ||
import * as cluster from 'cluster' | ||
import { EVENT } from '../src/constants' | ||
|
||
export const command = (program: Command) => { | ||
program | ||
.command('cluster') | ||
.description('start a daemon for deepstream server') | ||
|
||
.option('-c, --config [file]', 'configuration file, parent directory will be used as prefix for other config files') | ||
.option('-l, --lib-dir [directory]', 'path where to lookup for plugins like connectors and logger') | ||
|
||
.option('--cluster-size <amount>', 'the amount of nodes to run in the cluster') | ||
.option('--host <host>', 'host for the HTTP/websocket server') | ||
.option('--port <port>', 'port for the HTTP/websocket server') | ||
.option('--http-host <host>', 'host for the HTTP server') | ||
.option('--http-port <port>', 'port for the HTTP server') | ||
.option('--disable-auth', 'Force deepstream to use "none" auth type') | ||
.option('--disable-permissions', 'Force deepstream to use "none" permissions') | ||
.option('--log-level <level>', 'Log messages with this level and above') | ||
.action(action) | ||
} | ||
|
||
function action () { | ||
// @ts-ignore | ||
global.deepstreamCLI = this | ||
const workers = new Set<any>() | ||
|
||
const setupWorkerProcesses = () => { | ||
console.log('Master cluster setting up ' + global.deepstreamCLI.clusterSize + ' deepstream nodes') | ||
|
||
for (let i = 0; i < global.deepstreamCLI.clusterSize; i++) { | ||
workers.add(cluster.fork()) | ||
} | ||
|
||
// process is clustered on a core and process id is assigned | ||
cluster.on('online', (worker) => { | ||
console.log(`Deepstream ${worker.process.pid} is listening`) | ||
}) | ||
|
||
// if any of the worker process dies then start a new one by simply forking another one | ||
cluster.on('exit', (worker, code, signal) => { | ||
console.log(`Deepstream ${worker.process.pid} died with code: ${code}, and signal: ${signal}`) | ||
console.log('Starting a new worker') | ||
workers.delete(worker) | ||
workers.add(cluster.fork()) | ||
}) | ||
} | ||
|
||
// if it is a master process then call setting up worker process | ||
if (cluster.isMaster) { | ||
setupWorkerProcesses() | ||
} else { | ||
const { Deepstream } = require('../src/deepstream.io') | ||
try { | ||
const ds = new Deepstream(null) | ||
ds.on(EVENT.PLUGIN_INITIALIZATION_ERROR, () => process.exit(1)) | ||
ds.start() | ||
process | ||
.removeAllListeners('SIGINT').on('SIGINT', () => { | ||
ds.on('stopped', () => process.exit(0)) | ||
ds.stop() | ||
}) | ||
} catch (err) { | ||
console.error(err.toString()) | ||
process.exit(1) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.