Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry connection to gearmand server when connection lost #24

Open
recunius opened this issue Mar 18, 2015 · 6 comments
Open

Retry connection to gearmand server when connection lost #24

recunius opened this issue Mar 18, 2015 · 6 comments
Assignees

Comments

@recunius
Copy link

When losing a connection to the Gearman server currently GearmaNode does not have an option to reestablish the connection.

I thought of implementing such a solution outside of GearmanNode (for a worker), but that would be difficult when load balancing between multiple servers. The only straightforward option would be to discard the Worker instance and create a new one, but in doing so either the old worker would remain connected to the other server(s) which remained up, or the old Worker could be closed in which case there would be a time without a Worker connection to any server.

So would you be open to including an optional connection retry mechanism inside of GearmaNode? I haven't thought much about the client connections, but for the worker connections I think all that would be needed is to

  1. listen to 'socketDisconnect'
  2. retry connecting with options for interval and retryCount
  3. once connected add the server back into the JobManager jobServer list
  4. re-register all known worker functions in Worker.functions

I could give it a shot, but does such a plan make sense or is anyone aware of a work around or pitfalls to consider first?

@veny veny self-assigned this Mar 19, 2015
@hay-wire
Copy link

hay-wire commented May 5, 2015

@veny @recunius I'm also facing the same issue. Any updates on this?

@janober
Copy link

janober commented Jun 2, 2015

Did also just run into the same issue...

@veny
Copy link
Owner

veny commented Jun 18, 2015

hi all,
I put this task on my roadmap. should be done in Juni.

veny added a commit that referenced this issue Jun 26, 2015
veny added a commit that referenced this issue Jun 27, 2015
veny added a commit that referenced this issue Jun 27, 2015
veny added a commit that referenced this issue Jun 27, 2015
@vatson
Copy link

vatson commented Nov 26, 2015

@veny What's status of this feature?

@vatson
Copy link

vatson commented Nov 27, 2015

Just got to find a solution for workers without having to change the library itself

var gearmanode = require('gearmanode');
var protocol = require('gearmanode/lib/gearmanode/protocol');

var failoverStrategy = function(server, added_functions) {
  server.clientOrWorker.once('socketDisconnect', function() {
    var retry = setInterval(function () {
      server.connect(function(e){
        if(!e) {
          //Let server know about functions that registered workers can do
          for (var i in added_functions) {
            server.send(protocol.encodePacket(protocol.PACKET_TYPES.CAN_DO, [added_functions[i]]));
          }
          server.send(protocol.encodePacket(protocol.PACKET_TYPES.PRE_SLEEP));

          // Add failoverStrategy to a next fail
          failoverStrategy(server, workers);
          clearInterval(retry);
        }
      });
    }, 1000);
  });
};

var servers = [];
var added_functions = [];
var worker = gearmanode.worker({servers: servers});

worker.addFunction('ping', function(job){
  job.workComplete('pong')
});
added_functions.push('ping');

for (var i in worker.jobServers) {
  failoverStrategy(worker.jobServers[i], added_functions);
}

Perhaps, it can be useful for someone until we don't have a built-in implementation

FYI @veny @recunius @hay-wire

@janober
Copy link

janober commented Apr 5, 2017

No updates for a while so was wondering if that still gets fixed or if the work around of vason should be used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants