2
* Copyright (C) 2009 by Robert Stewart <robert@wombatnation.com>
3
* Copyright (C) 2009 by Eric Herman <eric@freesa.org>
4
* Use and distribution licensed under the
5
* GNU Lesser General Public License (LGPL) version 2.1.
6
* See the COPYING file in the parent directory for full text.
8
package org.gearman.worker;
11
import org.gearman.common.PacketConnection;
12
import java.util.EnumSet;
13
import java.util.List;
16
import org.gearman.common.PacketType;
17
import org.gearman.util.IORuntimeException;
20
* A Worker grabs a {@link Job} from a job server, performs the
21
* {@link JobFunction} specified on the data in the Job, and returns the results
22
* of the processing to the server. The server relays the results to the client
23
* that submitted the job. The worker may also return status updates or partial
24
* results to the job server.
26
public interface Worker {
27
// These enums were copied over from the C library.
28
public enum WorkerOption {
29
NON_BLOCKING, PACKET_INIT, GRAB_JOB_IN_USE, PRE_SLEEP_IN_USE, WORK_JOB_IN_USE, CHANGE, GRAB_UNIQ
33
PACKET_IN_USE, CHANGE, REMOVE
37
START, STATE_FUNCTION_SEND, STATE_CONNECT, STATE_GRAB_JOB_SEND, STATE_GRAB_JOB_RECV, STATE_PRE_SLEEP
41
GRAB_JOB, FUNCTION, COMPLETE, FAIL
45
* Wait for a job and call the appropriate callback function when it gets
51
* Clears all {@link WorkerOption}s.
53
void clearWorkerOptions();
56
* Returns {@link java.util.EnumSet} of {@link WorkerOption}s.
58
* @return EnumSet of WorkerOptions
60
EnumSet<WorkerOption> getWorkerOptions();
63
* Removes each specified WorkerOption from the current set of Worker
66
* @param workerOptions
67
* one or more WorkerOptions
69
void removeWorkerOptions(WorkerOption... workerOptions);
72
* Adds each specified WorkerOption to the current set of Worker options.
74
* <code>worker.setWorkerOptions(WorkerOption.NON_BLOCKING, WorkerOption.GRAB_JOB_IN_USE))</code>
76
* @param workerOptions
77
* one or more WorkerOptions
79
void setWorkerOptions(WorkerOption... workerOptions);
82
* Adds a {@link PacketConnection} to a job server.
85
* connection to a job server
87
void addServer(PacketConnection conn);
90
* Sends <code>text</code> to a job server with expectation of receiving the
91
* same data echoed back.
96
* connection to a job server
97
* @throws IORuntimeException
99
String echo(String text, PacketConnection conn);
102
* Registers a JobFunction that a Worker can perform on a Job. If the worker
103
* does not respond with a result within the given timeout period in
104
* seconds, the job server will assume the work will not be performed by
105
* that worker and will again make the work available to be performed by any
106
* worker capable of performing this function.
109
* JobFunction for a function a Worker can perform
111
* time in seconds after job server will consider job to be
114
void registerFunction(JobFunction function, int timeout);
117
* Registers a JobFunction that a Worker can perform on a Job.
120
* JobFunction for a function a Worker can perform
122
void registerFunction(JobFunction function);
125
* Registers a JobFunction that a Worker can perform on a Job. If the worker
126
* does not respond with a result within the given timeout period in
127
* seconds, the job server will assume the work will not be performed by
128
* that worker and will again make the work available to be performed by any
129
* worker capable of performing this function.
132
* JobFunction Class for a function a Worker can perform
134
* time in seconds after job server will consider job to be
137
void registerFunction(Class<? extends JobFunction> function, int timeout);
140
* Registers a JobFunction that a Worker can perform on a Job.
143
* JobFunction Class for a function a Worker can perform
145
void registerFunction(Class<? extends JobFunction> function);
148
* Registers a JobFunctionFactory that a Worker will use to create a
149
* JobFunction object to execute a Job.If the worker does not respond with a
150
* result within the given timeout period in seconds, the job server will
151
* assume the work will not be performed by that worker and will again make
152
* the work available to be performed by any worker capable of performing
156
* Factory that will be called to create a JobFunction instance
157
* for a function a Worker can perform
159
* time in seconds after job server will consider job to be
162
void registerFunctionFactory(JobFunctionFactory factory, int timeout);
165
* Registers a JobFunctionFactory that a Worker will use to create a
166
* JobFunction object to execute a Job.
169
* Factory that will be called to create a JobFunction instance
170
* for a function a Worker can perform
172
void registerFunctionFactory(JobFunctionFactory factory);
175
* Sets the worker ID in a job server so monitoring and reporting commands
176
* can uniquely identify the connected workers.
179
* ID that job server should use for an instance of a worker
181
void setWorkerID(String id);
184
* Sets the worker ID in a job server so monitoring and reporting commands
185
* can uniquely identify the connected workers. If a different ID is set
186
* with each job server, and connections can more easily be monitored and
187
* reported on independently.
190
* ID that job server should use for an instance of a worker
192
* connection to the job server
194
void setWorkerID(String id, PacketConnection conn);
197
* Unregisters with the Connection a function that a worker can perform on a
200
* @param functionName
201
* Name for a function a Worker can no longer perform
203
void unregisterFunction(String functionName);
206
* Unregisters all functions on all Connections.
208
void unregisterAll();
211
* Attempts to grab and then execute a Job on each connection.
213
* @return a Map indicating for each connection whether a Job was grabbed
215
Map<PacketConnection, PacketType> grabJob();
218
* Attempts to grab and then execute a Job on the specified connection.
221
* connection to a job server
222
* @return a PacketType indicating with a job was grabbed
224
PacketType grabJob(PacketConnection conn);
227
* Stops the work loop; requests to shutdown
232
* Stops the work loop and closes all open connections.
234
* @return a List of any Exceptions thrown when closing connections
236
List<Exception> shutdown();