Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 162 lines (104 sloc) 4.785 kB
bf1f72e beta
andris9 authored
1 # Gearman-Worker
2
3 **Gearman-Worker** is a Node.JS client/worker module for Gearman.
4
5 ## Usage
6
7 ### Worker
8
adaa65b should be quite usable by now
andris9 authored
9 var Gearman = require("./gearnode");
bf1f72e beta
andris9 authored
10
11 worker= new Gearman();
12 worker.addServer("localhost", 7003);
0a59f26 README update
andris9 authored
13 worker.addFunction("upper", function(payload, job){
14 var response = payload.toString("utf-8").toUpperCase();
15 job.complete(response);
bf1f72e beta
andris9 authored
16 });
17
18 ### Client
19
adaa65b should be quite usable by now
andris9 authored
20 var Gearman = require("./gearnode");
bf1f72e beta
andris9 authored
21
22 client = new Gearman();
23 client.addServer("localhost", 7003);
24
c2d2e5e fixes
andris9 authored
25 var job = client.submitJob("upper", "hello world!", {encoding:'utf-8'});
bf1f72e beta
andris9 authored
26
c2d2e5e fixes
andris9 authored
27 job.on("complete", function(handle, response){
28 console.log(response); // HELLO WORLD!
bf1f72e beta
andris9 authored
29 client.end();
30 });
0a59f26 README update
andris9 authored
31
32 ## API
33
34 ### Require Gearman library
35
adaa65b should be quite usable by now
andris9 authored
36 var Gearman = require("./gearnode");
0a59f26 README update
andris9 authored
37
38 ### Create a new Gearman worker/client
39
40 var gearman = new Gearman();
41
42 ### Add a server
43
44 gearman.addServer([host][, port])
45
46 Where
47
48 * **host** is the hostname of the Gearman server (defaults to localhost);
49 * **port** is the Gearman port (default is 4730)
50
51 Example
52
53 worker.addServer(); // use default values
54 worker.addServer("gearman.lan", 7003);
55
56 ### Register for exceptions
57
58 Exceptions are not sent to the client by default. To turn these on use the following command.
59
60 client.getExceptions([callback])
61
62 Where
63
64 * **callback** is an optional callback function with params *error* if an error occured and *success* which is true if the command succeeded
65
66 Example
67
68 client = new Gearman();
69 client.addServer(); // use default values
70 worker.getExceptions();
71
72 job = client.submitJob("reverse", "Hello world!");
73
74 job.on("error", function(exception){
75 console.log(exception);
76 });
77
78 ### Submit a job
79
80 client.submitJob(func, payload[, options])
81
82 Where
83
84 * **func** is the function name
85 * **payload** is either a String or a Buffer value
86 * **options** is an optional options object (see below)
87
88 Possible option values
89
adaa65b should be quite usable by now
andris9 authored
90 * **encoding** - indicates the encoding for the job response (default is Buffer). Can be "utf-8", "ascii", "base64", "number" or "buffer"
0a59f26 README update
andris9 authored
91 * **background** - if set to true, detach the job from the client (complete and error events will not be sent to the client)
92 * **priority** - indicates the priority of the job. Possible values "low", "normal" (default) and "high"
93
94 Returns a Gearman Job object with the following events
95
96 * **created** - when the function is queued by the server (params: handle value)
97 * **complete** - when the function returns (params: response data in encoding specified by the options value)
98 * **fail** - when the function fails (params: none)
99 * **error** - when an exception is thrown (params: error string)
100 * **warning** - when a warning is sent (params: warning string)
101 * **status** - when the status of a long running function is updated (params: numerator numbber, denominator number)
102 * **data** - when partial data is available (params: response data in encoding specified by the options value)
103
104 Example
105
106 client = new Gearman();
107 client.addServer(); // use default values
108 worker.getExceptions();
109
110 job = client.submitJob("reverse", "Hello world!", {encoding:"utf-8"});
111
112 job.on("complete", function(response){
113 console.log(response); // !dlrow olleH
114 });
115
116 job.on("fail", function(){
117 console.log("Job failed :S");
118 });
119
120 ### Create a worker function
121
adaa65b should be quite usable by now
andris9 authored
122 worker.addFunction(func_name[, encoding], worker_func)
123
124 Where
125
126 * **func_name** is the name of the function to be created
127 * **endocing** is the input encoding (default is buffer)
128 * **worker_func** is the actual worker function
129
130 #### Worker function
131
132 worker_func = function(payload, job)
133
134 Where
135
136 * **payload** is the data sent by the client and in the encoding specified with *addFunction*
137 * **job** is a Gearman Job object that can be used to send data back
138
139 #### Job object
140
141 Job object has the following methods
142
143 * **complete(response)** - send the result of the function back to the client
144 * **error(error)** - throw an exception (and end the job with *failed* status)
145 * **fail()** - end the function without response data when the function failed
146 * **warning(warning)** - send a warning message to the client
147 * **data(response)** - send a partial response data to the client
148 * **setStatus(numerator, denominator)** - send a progress event to the client
149
150 #### Example
151
152 var Gearman = require("./gearnode");
153
154 var worker = new Gearman();
155 worker.addServer();
156
157 worker.addFunction("sqr", "number", function(payload, job){
158 if(payload < 0){
159 job.warning("Used number is smaller than zero!");
160 }
161 job.complete(payload * payload);
162 });
Something went wrong with that request. Please try again.