gearman-coffee
gearman-coffee is an implementation of the Gearman protocol in CoffeeScript. It exposes a conventional Node library for creating Gearman workers and clients, and listening for events related to both. It aims to be a very a lightweight wrapper around the protocol itself.
Installation
npm install gearman-coffee
Workers
Workers are created with the name and function that they perform:
worker = 'reverse' return workererror 'No payload' unless payload? reversed = payloadtoString"utf-8"split''reversejoin '' workercomplete reversed
The worker function itself is passed an object that contains the following convenience methods:
warning(warning)
: sends a 'WORK_WARNING' packetstatus(num,den)
: sends a 'WORK_STATUS' packetdata(data)
: sends a 'WORK_DATA' packeterror([warning])
: sends an optional 'WORK_WARNING' before 'WORK_FAIL'complete([data])
: sends an optional 'WORK_DATA' before 'WORK_COMPLETE'done([warning])
: callserror
if warning passed, otherwisecomplete
The exact meaning of these is best documented on the Gearman website itself: http://gearman.org/index.php?id=protocol.
Workers optionally take a hash of options. These options control the Gearman server connection settings as well as debug output and retry behavior:
default_options = host: 'localhost' port: 4730 debug: false max_retries: 0worker = 'unstable' return workererror if Mathrandom < 0.5 workerdonedefault_options
Clients
Clients are used to submit work to Gearman. By default they connect to Gearman at localhost:4730
:
default_options = host: 'localhost' port: 4730 debug: falseclient = default_options
The submitJob
method of the client takes in the name of the worker and the workload you'd like to send. It returns an EventEmitter that relays Gearman server notifications:
clientsubmitJob'reverse''kitteh' on 'created' # JOB_CREATED on 'data' # WORK_DATA on 'warning' # WORK_WARNING on 'status' # WORK_STATUS on 'complete' # WORK_COMPLETE on 'fail' # WORK_FAIL
License
MIT