smartproxy/ts/smartproxy.classes.proxymaster.ts

51 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-08-22 10:49:29 +00:00
import { expose } from '@pushrocks/smartspawn';
2019-08-22 11:20:41 +00:00
import * as plugins from './smartproxy.plugins';
import * as cluster from 'cluster';
2019-08-22 10:49:29 +00:00
class ProxyMaster {
2019-08-22 11:20:41 +00:00
public hostCandidates: plugins.tsclass.network.IReverseProxyConfig[] = [];
2019-08-22 10:49:29 +00:00
public clusterChilds: any[] = [];
2019-08-22 11:20:41 +00:00
public smartsystem = new plugins.smartsystem.Smartsystem();
2019-08-22 10:49:29 +00:00
2019-08-22 11:20:41 +00:00
public async start() {
if (cluster.isMaster) {
console.log('ProxyMaster registered as cluster master');
console.log(`should spawn ${this.smartsystem.cpus.length} workers`);
for (const cpu of this.smartsystem.cpus) {
cluster.fork();
}
} else {
const proxyworkerMod = await import('./smartproxy.classes.proxyworker');
const proxyWorkerInstance = new proxyworkerMod.ProxyWorker();
console.log('Proxymaster registered a ProxyWorker!');
const data = new Uint8Array(Buffer.from('Hello Node.js'));
fs.writeFile('message.txt', data, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
}
}
2019-08-22 10:49:29 +00:00
}
const defaultProxyMaster = new ProxyMaster();
2019-08-22 11:20:41 +00:00
if (!cluster.isMaster) {
defaultProxyMaster.start();
}
console.log('hi');
2019-08-22 10:49:29 +00:00
2019-08-22 11:20:41 +00:00
// the following is interesting for the master process only
2019-08-22 10:49:29 +00:00
const proxyMasterCalls = {
terminateMaster: async () => {
process.kill(0);
},
2019-08-22 11:20:41 +00:00
start: async () => {
await defaultProxyMaster.start();
},
updateReverseConfigs: async (configArray: plugins.tsclass.network.IReverseProxyConfig[]) => {
}
2019-08-22 10:49:29 +00:00
};
export type TProxyMasterCalls = typeof proxyMasterCalls;
expose (proxyMasterCalls);