Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
165 lines (105 sloc) 4.74 KB


Gearnode is a Node.JS client/worker module for Gearman.

NB! this is usable beta, I'll have to do a lot of refactoring and optimization



var Gearman = require("./gearnode");

worker= new Gearman();
worker.addServer("localhost", 7003);
worker.addFunction("upper", function(payload, job){
    var response =  payload.toString("utf-8").toUpperCase();


var Gearman = require("./gearnode");

client = new Gearman();
client.addServer("localhost", 7003);

var job = client.submitJob("upper", "hello world!", {encoding:'utf-8'});

job.on("complete", function(handle, response){
    console.log(response); // HELLO WORLD!


Require Gearman library

var Gearman = require("./gearnode");

Create a new Gearman worker/client

var gearman = new Gearman();

Add a server

gearman.addServer([host][, port])


  • host is the hostname of the Gearman server (defaults to localhost);
  • port is the Gearman port (default is 4730)


worker.addServer(); // use default values
worker.addServer("gearman.lan", 7003);

Register for exceptions

Exceptions are not sent to the client by default. To turn these on use the following command.



  • callback is an optional callback function with params error if an error occured and success which is true if the command succeeded


client = new Gearman();
client.addServer(); // use default values

job = client.submitJob("reverse", "Hello world!");

job.on("error", function(exception){

Submit a job

client.submitJob(func, payload[, options])


  • func is the function name
  • payload is either a String or a Buffer value
  • options is an optional options object (see below)

Possible option values

  • encoding - indicates the encoding for the job response (default is Buffer). Can be "utf-8", "ascii", "base64", "number" or "buffer"
  • background - if set to true, detach the job from the client (complete and error events will not be sent to the client)
  • priority - indicates the priority of the job. Possible values "low", "normal" (default) and "high"

Returns a Gearman Job object with the following events

  • created - when the function is queued by the server (params: handle value)
  • complete - when the function returns (params: response data in encoding specified by the options value)
  • fail - when the function fails (params: none)
  • error - when an exception is thrown (params: error string)
  • warning - when a warning is sent (params: warning string)
  • status - when the status of a long running function is updated (params: numerator numbber, denominator number)
  • data - when partial data is available (params: response data in encoding specified by the options value)


client = new Gearman();
client.addServer(); // use default values

job = client.submitJob("reverse", "Hello world!", {encoding:"utf-8"});

job.on("complete", function(response){
    console.log(response); // !dlrow olleH

job.on("fail", function(){
    console.log("Job failed :S");

Create a worker function

worker.addFunction(func_name[, encoding], worker_func)


  • func_name is the name of the function to be created
  • endocing is the input encoding (default is buffer)
  • worker_func is the actual worker function

Worker function

worker_func = function(payload, job)


  • payload is the data sent by the client and in the encoding specified with addFunction
  • job is a Gearman Job object that can be used to send data back

Job object

Job object has the following methods

  • complete(response) - send the result of the function back to the client
  • error(error) - throw an exception (and end the job with failed status)
  • fail() - end the function without response data when the function failed
  • warning(warning) - send a warning message to the client
  • data(response) - send a partial response data to the client
  • setStatus(numerator, denominator) - send a progress event to the client


var Gearman = require("./gearnode");

var worker = new Gearman();

worker.addFunction("sqr", "number", function(payload, job){
    if(payload < 0){
        job.warning("Used number is smaller than zero!");
    job.complete(payload * payload);
Something went wrong with that request. Please try again.