本文整理汇总了TypeScript中cluster.on函数的典型用法代码示例。如果您正苦于以下问题:TypeScript on函数的具体用法?TypeScript on怎么用?TypeScript on使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了on函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: setup
export async function inParallel<Item, Result, Acc>(args: ParallelArgs<Item, Result, Acc>) {
let { queue } = args;
let items = queue.map((item, i) => {
return { item, i };
});
if (cluster.isMaster) {
let { setup, workers, map } = args;
await setup();
cluster.on("message", worker => {
if (items.length) {
worker.send(items.shift());
} else {
worker.kill();
}
});
cluster.on("exit", (_worker, code, _signal) => {
if (code && code !== 0) {
// Kill workers and exit if any worker dies
_.forIn(cluster.workers, w => {
if (w) {
w.kill();
}
});
exit(code);
}
});
console.error(`* Forking ${workers} workers ${guys(workers)}`);
if (workers < 2) {
// We run everything on the master process if only one worker
for (let { item, i } of items) {
await map(item, i);
}
} else {
_.range(workers).forEach(i =>
cluster.fork({
worker: i,
// https://github.com/TypeStrong/ts-node/issues/367
TS_NODE_PROJECT: "test/tsconfig.json"
})
);
}
} else {
// Setup a worker
let { map } = args;
// master sends a { fixtureName, sample } to run
process.on("message", async ({ item, i }) => {
(process.send as any)({
result: await map(item, i)
});
});
// Ask master for work
(process.send as any)("ready");
}
}
示例2: forkListenerProcesses
private static forkListenerProcesses (numberOfCores: number): void {
for (let i = 0; i < numberOfCores; i++) {
cluster.fork()
}
cluster.on('exit', (worker, code, signal) => {
logger.info(`Worker ${worker.process.pid} exited with ${code ? code : signal}`)
})
}
示例3: resolve
return new Promise<cluster.Worker[]>(((resolve, reject) => {
const onlineWorkers: cluster.Worker[] = [];
cluster.on('online', (worker: cluster.Worker) => {
onlineWorkers.push(worker);
if (onlineWorkers.length === numCPUs) {
resolve(onlineWorkers);
}
});
}));
示例4: spawn
spawn(() => {
console.log(chalk.bold.green(`\nMisskey Core is now running. [port:${conf.port}]`));
// Listen new workers
cluster.on('fork', worker => {
console.log(`Process forked: [${worker.id}]`);
});
// Listen online workers
cluster.on('online', worker => {
console.log(`Process is now online: [${worker.id}]`);
});
// Listen for dying workers
cluster.on('exit', worker => {
// Replace the dead worker,
// we're not sentimental
console.log(chalk.red(`[${worker.id}] died :(`));
cluster.fork();
});
});
示例5: start
export function start(workerCount, servicePort, serviceAddress): void {
cluster.on("listening", (worker, address) => {
if (
(address.addressType === 4 || address.addressType === 6) &&
address.address === serviceAddress &&
address.port === servicePort
) {
logger.info({
message: "Worker listening",
pid: worker.process.pid,
address: address.address,
port: address.port
});
}
});
cluster.on("exit", restartWorker);
if (!workerCount) workerCount = Math.max(2, cpus().length);
for (let i = 0; i < workerCount; ++i) cluster.fork();
}
示例6: runAsClusterMode
export function runAsClusterMode(options: clusterOptions, callback: () => void) {
if (cluster.isMaster) {
os.cpus().forEach(() => {
cluster.fork();
});
cluster.on('exit', () => cluster.fork());
return callback();
}
options.users.forEach(o => new App(o));
if (options.management) require('./management/index');
if (options.user) try { process.setuid(options.user) } catch(ex) { console.error(ex.message); }
}
示例7: Promise
return new Promise((resolve, reject) => {
dotenv.config();
var cpuCount = opts.cpuCores || cpus().length;
var stopForking = false;
// if this is process
if (cluster.isMaster) {
var onClusterMsg = (msg) => {
if (msg == "fork")
cluster.fork().on('message', onClusterMsg);
};
onClusterMsg('fork');
cluster.on('exit', function (worker) {
console.error('Worker %s has died! Creating a new one.', worker.id);
if (!stopForking)
cluster.fork();
});
} else {
Server.bootstrap(opts, cluster.worker, (err) => {
if (err)
return reject(err);
if (cluster.worker.id == cpuCount) {
resolve(cluster.workers);
}
else if (cluster.worker.id < cpuCount)
process.send('fork');
});
}
});
示例8: main
async function main() {
if (cluster.isMaster) {
// tslint:disable-next-line
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on("exit", (worker, code, signal) => {
// tslint:disable-next-line
console.log(`worker ${worker.process.pid} died`);
});
} else {
const db = await dbClient.connect();
Container.set(Db, db);
// Workers can share any TCP connection
// In this case it is an HTTP server
const playground = {
settings: {
"editor.cursorShape": "line",
} as any,
};
const server = new ApolloServer({
schema,
playground,
});
server.listen(config.PORT, () =>
// tslint:disable-next-line
console.log(
`Dictionary service is running on http://localhost:${
config.PORT
}`,
),
);
// tslint:disable-next-line
console.log(`Worker ${process.pid} started`);
}
}
示例9: require
const instanceInfo = cluster.worker
? ' [Instance:' + cluster.worker.id + ']'
: '';
log.info('BWS running ' + instanceInfo);
return;
});
}
if (config.cluster && cluster.isMaster) {
// Count the machine's CPUs
const instances = config.clusterInstances || require('os').cpus().length;
log.info('Starting ' + instances + ' instances');
// Create a worker for each CPU
for (let i = 0; i < instances; i += 1) {
cluster.fork();
}
// Listen for dying workers
cluster.on('exit', (worker) => {
// Replace the dead worker,
log.error('Worker ' + worker.id + ' died :(');
cluster.fork();
});
// Code to run if we're in a worker process
} else {
log.info('Listening on port: ' + port);
startInstance();
}
示例10: require
if (process.env.NODE_ENV === 'production') {
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log('CPU count', numCPUs);
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('online', function(worker) {
console.log('Worker ' + worker.process.pid + ' is online');
});
cluster.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
console.log('Starting a new worker');
cluster.fork();
});
} else {
require('./node');
}
} else {
require('./node');
}