public class WorkerImpl extends Object implements Worker
JobExecutor.State| Modifier and Type | Field and Description |
|---|---|
protected Config |
config |
protected static long |
EMPTY_QUEUE_SLEEP_TIME |
protected redis.clients.jedis.Jedis |
jedis |
protected WorkerListenerDelegate |
listenerDelegate |
protected String |
namespace |
protected BlockingDeque<String> |
queueNames |
protected static int |
RECONNECT_ATTEMPTS |
protected static long |
RECONNECT_SLEEP_TIME |
protected AtomicReference<JobExecutor.State> |
state |
ALL_QUEUES| Constructor and Description |
|---|
WorkerImpl(Config config,
Collection<String> queues,
JobFactory jobFactory)
Creates a new WorkerImpl, which creates it's own connection to Redis
using values from the config.
|
WorkerImpl(Config config,
Collection<String> queues,
JobFactory jobFactory,
redis.clients.jedis.Jedis jedis)
Creates a new WorkerImpl, with the given connection to Redis.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addQueue(String queueName)
Poll the given queue.
|
protected void |
checkPaused()
Checks to see if worker is paused.
|
protected static void |
checkQueues(Iterable<String> queues)
Verify that the given queues are all valid.
|
protected String |
createName()
Creates a unique name, suitable for use with Resque.
|
void |
end(boolean now)
Shutdown this Worker.
The worker cannot be started again; create a new worker in this case. |
protected Object |
execute(Job job,
String curQueue,
Object instance)
Executes the given job.
|
protected String |
failMsg(Throwable t,
String queue,
Job job)
Create and serialize a JobFailure.
|
protected void |
failure(Throwable t,
Job job,
String curQueue)
Update the status in Redis on failure
|
ExceptionHandler |
getExceptionHandler()
The current exception handler.
|
JobFactory |
getJobFactory()
The job factory.
|
String |
getName()
Returns the name of this Worker.
|
Collection<String> |
getQueues()
The queues that this Worker will poll.
|
protected int |
getReconnectAttempts() |
WorkerEventEmitter |
getWorkerEventEmitter() |
long |
getWorkerId() |
boolean |
isPaused()
Returns whether this worker is paused.
|
boolean |
isProcessingJob()
Returns whether this JobExecutor is currently processing a job.
|
boolean |
isShutdown()
Returns whether this JobExecutor is either shutdown or in the process of shutting down.
|
static boolean |
isThreadNameChangingEnabled() |
void |
join(long millis)
Wait for this JobExecutor to complete.
|
protected String |
key(String... parts)
Builds a namespaced Redis key with the given arguments.
|
protected String |
lpoplpush(String from,
String to) |
protected String |
pauseMsg()
Create and serialize a WorkerStatus for a pause event.
|
protected void |
poll()
Polls the queues for jobs and executes them.
|
protected String |
pop(String curQueue)
Remove a job from the given queue.
|
protected void |
process(Job job,
String curQueue)
Materializes and executes the given job.
|
protected void |
recoverFromException(String curQueue,
Exception e)
Handle an exception that was thrown from inside
poll() |
void |
removeAllQueues()
Stop polling all queues.
|
void |
removeQueue(String queueName,
boolean all)
Stop polling the given queue.
|
protected void |
renameThread(String msg)
Rename the current thread with the given message.
|
void |
run()
Starts this worker.
|
void |
setExceptionHandler(ExceptionHandler exceptionHandler)
Set this JobExecutor's exception handler to the given handler.
|
void |
setQueues(Collection<String> queues)
Clear any current queues and poll the given queues.
|
static void |
setThreadNameChangingEnabled(boolean enabled)
Enable/disable worker thread renaming during normal operation.
|
protected String |
statusMsg(String queue,
Job job)
Create and serialize a WorkerStatus.
|
protected void |
success(Job job,
Object runner,
Object result,
String curQueue)
Update the status in Redis on success.
|
void |
togglePause(boolean paused)
Toggle whether this worker will process any new jobs.
|
String |
toString() |
protected static final long EMPTY_QUEUE_SLEEP_TIME
protected static final long RECONNECT_SLEEP_TIME
protected static final int RECONNECT_ATTEMPTS
protected final Config config
protected final redis.clients.jedis.Jedis jedis
protected final String namespace
protected final BlockingDeque<String> queueNames
protected final WorkerListenerDelegate listenerDelegate
protected final AtomicReference<JobExecutor.State> state
public WorkerImpl(Config config, Collection<String> queues, JobFactory jobFactory)
config - used to create a connection to Redis and the package prefix
for incoming jobsqueues - the list of queues to polljobFactory - the job factory that materializes the jobsIllegalArgumentException - if either config, queues or jobFactory is nullpublic WorkerImpl(Config config, Collection<String> queues, JobFactory jobFactory, redis.clients.jedis.Jedis jedis)
config - used to create a connection to Redis and the package prefix
for incoming jobsqueues - the list of queues to polljobFactory - the job factory that materializes the jobsjedis - the connection to RedisIllegalArgumentException - if either config, queues, jobFactory or jedis is nullpublic static boolean isThreadNameChangingEnabled()
public static void setThreadNameChangingEnabled(boolean enabled)
Warning: Enabling this feature is very expensive
CPU-wise!
This feature is designed to assist in debugging worker state but should
be disabled in production environments for performance reasons.
enabled - whether threads' names should change during normal operationprotected static void checkQueues(Iterable<String> queues)
queues - the given queuespublic long getWorkerId()
public void run()
public void end(boolean now)
end in interface JobExecutornow - if true, an effort will be made to stop any job in progresspublic boolean isShutdown()
isShutdown in interface JobExecutorpublic boolean isPaused()
public boolean isProcessingJob()
isProcessingJob in interface JobExecutorpublic void togglePause(boolean paused)
togglePause in interface Workerpaused - if true, the worker will not process any new jobs; if false,
the worker will process new jobspublic String getName()
public WorkerEventEmitter getWorkerEventEmitter()
getWorkerEventEmitter in interface Workerpublic Collection<String> getQueues()
public void addQueue(String queueName)
public void removeQueue(String queueName, boolean all)
all argument is true,
all instances of the queue will be removed, otherwise, only one instance
is removed.removeQueue in interface WorkerqueueName - the queue to stop pollingall - whether to remove all or only one of the instancespublic void removeAllQueues()
removeAllQueues in interface Workerpublic void setQueues(Collection<String> queues)
public JobFactory getJobFactory()
getJobFactory in interface JobExecutorpublic ExceptionHandler getExceptionHandler()
getExceptionHandler in interface JobExecutorpublic void setExceptionHandler(ExceptionHandler exceptionHandler)
setExceptionHandler in interface JobExecutorexceptionHandler - the exception handler to usepublic void join(long millis)
throws InterruptedException
JobExecutor.end(boolean).join in interface JobExecutormillis - the time to wait in millisecondsInterruptedException - if any thread has interrupted the current threadprotected int getReconnectAttempts()
protected void poll()
protected String pop(String curQueue)
curQueue - the queue to remove a job fromprotected void recoverFromException(String curQueue, Exception e)
poll()curQueue - the name of the queue that was being processed when the
exception was throwne - the exception that was thrownprotected void checkPaused()
throws IOException
IOException - if there was an error creating the pause messageprotected void process(Job job, String curQueue)
job - the Job to processcurQueue - the queue the payload came fromprotected Object execute(Job job, String curQueue, Object instance) throws Exception
protected void success(Job job, Object runner, Object result, String curQueue)
job - the Job that succeededrunner - the materialized Jobresult - the result of the successful execution of the JobcurQueue - the queue the Job came fromprotected void failure(Throwable t, Job job, String curQueue)
t - the Throwable that occurredjob - the Job that failedcurQueue - the queue the Job came fromprotected String failMsg(Throwable t, String queue, Job job) throws IOException
t - the Throwable that occurredqueue - the queue the job came fromjob - the Job that failedIOException - if there was an error serializing the JobFailureprotected String statusMsg(String queue, Job job) throws IOException
queue - the queue the Job came fromjob - the Job currently being processedIOException - if there was an error serializing the WorkerStatusprotected String pauseMsg() throws IOException
IOException - if there was an error serializing the WorkerStatusprotected String createName()
protected String key(String... parts)
parts - the key parts to be joinedprotected void renameThread(String msg)
msg - the message to add to the thread nameCopyright © 2011-2015. All Rights Reserved.