Home

Awesome

NB !!ABANDONWARE!!

I'm not maintaining this repo anymore. Sorry.

Gearnode

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

NB! check out another Gearman library of mine node-gearman which is somewhat more simple than this one.

Installation

npm install gearnode

Tests

Tests are run with nodeunit

./run_tests.sh

Tests expect a Gearman daemon running on port 7003

Usage

Worker

var Gearnode = require("gearnode");

worker = new Gearnode();
worker.addServer(); // use localhost

worker.addFunction("upper", "utf-8", function(payload, job){
    var response =  payload.toUpperCase();
    job.complete(response);
});

Client

var Gearnode = require("gearnode");

client = new Gearnode();
client.addServer();

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

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

API

Require Gearman library

var Gearnode = require("gearnode");

Create a new Gearnode worker/client

var gearnode = new Gearnode();

Add a server

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

Where

Example

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

Register for exceptions

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

gearnode.getExceptions([callback])

Where

Example

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

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

job.on("error", function(exception){
    console.log(exception);
});

Assign an ID for the Worker

Worker ID's identify unique workers for monitoring Gearman.

gearnode.setWorkerId(id)

Where

Example

worker = new Gearnode();
worker.addServer(); // use default values
worker.setWorkerId("my_worker");

Submit a job

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

Where

Possible option values

Returns a Client Job object with the following events

Example

client = new Gearnode();
client.addServer(); // use default values
worker.getExceptions();

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

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

Where

Worker function

worker_func = function(payload, job)

Where

Worker Job object

Worker Job object has the following methods

Example

var Gearnode = require("gearnode");

var worker = new Gearnode();
worker.addServer();

worker.addFunction("sqr", "number", function(payload, job){
    if(payload < 0){
        job.warning("Used number is smaller than zero!");
    }
    job.complete(payload * payload);
});

Detect connection errors

When the connection is lost a "disconnect" event is emitted to the client/worker

worker.addServer("gearman.lan");
worker.on("disconnect", function(server){
    console.log("Connection lost from "+server_name);
});

License

MIT