本文整理汇总了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");
}
}
开发者ID:nrkn,项目名称:quicktype,代码行数:60,代码来源:multicore.ts
示例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}`)
})
}
开发者ID:hmcts,项目名称:cmc-citizen-frontend,代码行数:8,代码来源:applicationCluster.ts
示例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);
}
});
}));
开发者ID:swanest,项目名称:micromessaging,代码行数:9,代码来源:Master.ts
示例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();
});
});
开发者ID:syuilo,项目名称:misskey-core,代码行数:21,代码来源:index.ts
示例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();
}
开发者ID:zaidka,项目名称:genieacs,代码行数:22,代码来源:cluster.ts
示例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); }
}
开发者ID:52M,项目名称:LightSword,代码行数:15,代码来源:cluster.ts
示例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');
});
}
});
开发者ID:vrwebdesign,项目名称:serendip,代码行数:47,代码来源:Start.ts
示例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`);
}
}
开发者ID:vnenkpet,项目名称:japanese,代码行数:44,代码来源:index.ts
示例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();
}
开发者ID:bitpay,项目名称:bitcore,代码行数:30,代码来源:bws.ts
示例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');
}
开发者ID:gdi2290,项目名称:angular2-falcor-starter,代码行数:26,代码来源:server.ts
注:本文中的cluster.on函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论