vsivsi / meteor-job

Job class for use with Meteor job-collection distributed job queue.
https://www.npmjs.org/package/meteor-job
Other
40 stars 20 forks source link

Empty exception when manually trying to reconnect #14

Closed bjarketrux closed 7 years ago

bjarketrux commented 7 years ago

The option for DDP auto_reconnect (the ddp package) seems to only be trying to reconnect once. I would like it to keep trying indefinitely and try to reconnect if connection is lost again. In this way we can start our services independently, and we can close our meteor server and start it again without the worker service crashing.

This is basically the code:

const ddp = new DDP({ "host": "127.0.0.1", "port": 3000});

Job.setDDP(ddp);

const connectCb = (error, wasReconnect) => {
    if (!error) {
        onConnected(ddp); // Register workers
    }
};

ddp.connect(connectCb);

ddp.on("socket-close", () => {
    ddp.close(); // To remove all listeners
    setTimeout(() => ddp.connect(connectCb), 5000);
});

This approach works, but I get the following error when a connection is lost:

JobQueue:  Error: Received error from getWork(): 
    at node_modules/meteor-job/lib/job_class.js:249:44
    at Object.<anonymous> (node_modules/meteor-job/lib/job_class.js:23:20)
    at Object.self._callbacks.(anonymous function) (node_modules/ddp/lib/ddp-client.js:434:16)
    at node_modules/ddp/lib/ddp-client.js:339:26
    at Array.forEach (native)
    at DDPClient._endPendingMethodCalls (node_modules/ddp/lib/ddp-client.js:337:7)
    at Client.<anonymous> (node_modules/ddp/lib/ddp-client.js:91:10)
    at emitOne (events.js:96:13)
    at Client.emit (events.js:191:7)
    at Client.dispatchEvent (node_modules/faye-websocket/lib/faye/websocket/api/event_target.js:24:10)

Steps to reproduce:

  1. Start meteor server
  2. Start job worker
  3. Kill meteor server
  4. Observe: message is shown

I investigated a bit and found that the actual exception is:

Error: DDPClient: Disconnected from DDP server

I found this by inserting a console.log in job_class.coffe:_getWork: () -> before @errorCallback new Error "Received error from getWork(): ", err

I have tried to new up a new DDP, but Job.setDDP(ddp); complains if it is called again.

  1. Is the approach the correct one?
  2. Should the error message be silenced (in meteor-job) as it is expected not to always have a connection?

Thanks

vsivsi commented 7 years ago

Hi, the processJobs() function's options object can accept an attribute called errorCallback. If you supply a function for this attribute, you can use it to redefine the default behavior for cases like this. Its only parameter is the error object.

See: https://github.com/vsivsi/meteor-job#q--jobprocessjobsroot-type-options-worker

bjarketrux commented 7 years ago

D'oh simple solution! Thanks

bjarketrux commented 7 years ago

Oh by the way the actual error message is still not shown. Is that a bug or...? Actual: JobQueue: Error: Received error from getWork(): Expected: JobQueue: Error: Received error from getWork(): Error: DDPClient: Disconnected from DDP server

We can reopen the issue or create a new one for that.

vsivsi commented 7 years ago

Hmm, yes, that's a bug. I'll fix it.