Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
Eventos de Nodejs capturados en cualquier proceso de clúster enviado desde el archivo de creación de clúster

Estoy atascado aquí debido a un simple problema relacionado con un evento. Aquí está el problema:

  • Creé un clúster usando cluster.js y server.js bifurcado de cluster.js.
  • He puesto un temporizador de cluster.js y después de cada 1 minuto estoy activando un evento 'testTimer'. He usado un archivo de eventos para hacer
    eso.
  • Estoy tratando de capturar este evento 'testTimer' del niño
    proceso usando el mismo archivo que he importado en server.js y haciendo un .on('testTimer', devolución de llamada)

Sin embargo, los eventos no se capturan en ninguno de los procesos. Intenté hacer que el evento sea global y asignar el evento globalmente a un símbolo, pero tampoco pude hacer que funcionara/capturara el evento.

Aquí están los códigos:

cluster.js (creador de procesos secundarios)

 ...require > events.js... ... create cluster logic... setInterval(function () { evt.emit('testTimer', {tester: 'test'}); evt.tester(); }, 1000);

server.js (proceso secundario)

 ...require > events.js... evt.on('testTimer', function (data) { console.log('Starting Sync ', data); });

events.js (archivo común para eventos)

 var util = require("util"); var EventEmitter = require("events").EventEmitter; function test () { EventEmitter.call(this); } test.prototype.tester = function (){ this.emit('testTimer', {missed: 'this'}) } util.inherits(test, EventEmitter); module.exports = test;
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Las instancias de EventEmitter no pueden ir más allá de los límites de un proceso. Si desea comunicarse entre padres e hijos, use worker.send() :

 // cluster.js setInterval(function () { for (const id in cluster.workers) { cluster.workers[id].send({ type : 'testTimer', data : { tester : 'test' }}); } }, 1000); // server.js process.on('message', function(message) { if (message.type === 'testTimer') { console.log('Starting Sync ', message.data); } })
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!