use of com.birbit.android.jobqueue.messaging.message.RunJobMessage in project android-priority-jobqueue by yigit.
the class ConsumerManager method handleIdle.
/**
* @return true if consumer received a job or busy, false otherwise
*/
boolean handleIdle(@NonNull JobConsumerIdleMessage message) {
Consumer consumer = (Consumer) message.getWorker();
if (consumer.hasJob) {
// ignore, it has a job to process.
return true;
}
JobHolder nextJob = null;
final boolean running = jobManagerThread.isRunning();
if (running) {
nextJob = jobManagerThread.getNextJob(runningJobGroups.getSafe());
}
if (nextJob != null) {
consumer.hasJob = true;
runningJobGroups.add(nextJob.getGroupId());
RunJobMessage runJobMessage = factory.obtain(RunJobMessage.class);
runJobMessage.setJobHolder(nextJob);
runningJobHolders.put(nextJob.getJob().getId(), nextJob);
if (nextJob.getGroupId() != null) {
runningJobGroups.add(nextJob.getGroupId());
}
consumer.messageQueue.post(runJobMessage);
return true;
} else {
long keepAliveTimeout = message.getLastJobCompleted() + consumerKeepAliveNs;
JqLog.v("keep alive: %s", keepAliveTimeout);
final boolean tooMany = consumers.size() > minConsumerCount;
boolean kill = !running || (tooMany && keepAliveTimeout < timer.nanoTime());
JqLog.v("Consumer idle, will kill? %s. isRunning: %s. too many? %s timeout: %s now: %s", kill, running, tooMany, keepAliveTimeout, timer.nanoTime());
if (kill) {
CommandMessage command = factory.obtain(CommandMessage.class);
command.set(CommandMessage.QUIT);
consumer.messageQueue.post(command);
waitingConsumers.remove(consumer);
consumers.remove(consumer);
JqLog.d("killed consumers. remaining consumers %d", consumers.size());
if (consumers.isEmpty() && internalZeroConsumersListeners != null) {
for (Runnable runnable : internalZeroConsumersListeners) {
runnable.run();
}
}
} else {
if (!waitingConsumers.contains(consumer)) {
waitingConsumers.add(consumer);
}
if (tooMany || !jobManagerThread.canListenToNetwork()) {
CommandMessage cm = factory.obtain(CommandMessage.class);
cm.set(CommandMessage.POKE);
if (!tooMany) {
keepAliveTimeout = timer.nanoTime() + consumerKeepAliveNs;
}
consumer.messageQueue.postAt(cm, keepAliveTimeout);
JqLog.d("poke consumer manager at %s", keepAliveTimeout);
}
}
return false;
}
}
use of com.birbit.android.jobqueue.messaging.message.RunJobMessage in project android-priority-jobqueue by yigit.
the class ConsumerTest method runJobMessage.
@Test
public void runJobMessage() {
MessageFactory factory = new MessageFactory();
PriorityMessageQueue pmq = new PriorityMessageQueue(timer, factory);
setRunning(pmq);
SafeMessageQueue mq = new SafeMessageQueue(timer, factory, "test");
setRunning(mq);
timer.setNow(2001);
Consumer consumer = new Consumer(pmq, mq, factory, timer);
RunJobMessage rjm = factory.obtain(RunJobMessage.class);
JobHolder mockHolder = mock(JobHolder.class);
when(mockHolder.safeRun(0, timer)).thenReturn(JobHolder.RUN_RESULT_SUCCESS);
rjm.setJobHolder(mockHolder);
timer.setNow(3001);
consumer.queueConsumer.handleMessage(rjm);
Message message = pmq.next(dummyConsumer);
assertThat(message, CoreMatchers.instanceOf(RunJobResultMessage.class));
RunJobResultMessage result = (RunJobResultMessage) message;
assertThat(result.getResult(), CoreMatchers.is(JobHolder.RUN_RESULT_SUCCESS));
assertThat(result.getJobHolder(), CoreMatchers.is(mockHolder));
assertThat(consumer.lastJobCompleted, CoreMatchers.is(3001L));
}
use of com.birbit.android.jobqueue.messaging.message.RunJobMessage in project android-priority-jobqueue by yigit.
the class ConsumerTest method removePokesAfterJobTest.
@Test
public void removePokesAfterJobTest() {
MessageFactory factory = new MessageFactory();
PriorityMessageQueue pmq = new PriorityMessageQueue(timer, factory);
setRunning(pmq);
SafeMessageQueue mq = spy(new SafeMessageQueue(timer, factory, "test"));
setRunning(mq);
timer.setNow(2001);
Consumer consumer = new Consumer(pmq, mq, factory, timer);
RunJobMessage rjm = factory.obtain(RunJobMessage.class);
JobHolder mockHolder = mock(JobHolder.class);
when(mockHolder.safeRun(0, new MockTimer())).thenReturn(JobHolder.RUN_RESULT_SUCCESS);
rjm.setJobHolder(mockHolder);
timer.setNow(3001);
verify(mq, times(0)).cancelMessages(Consumer.pokeMessagePredicate);
consumer.queueConsumer.handleMessage(rjm);
verify(mq, times(1)).cancelMessages(Consumer.pokeMessagePredicate);
}
use of com.birbit.android.jobqueue.messaging.message.RunJobMessage in project android-priority-jobqueue by yigit.
the class ConsumerTest method pokePredicateTest.
@Test
public void pokePredicateTest() {
CommandMessage cm = new CommandMessage();
cm.set(CommandMessage.POKE);
assertThat(Consumer.pokeMessagePredicate.onMessage(cm), CoreMatchers.is(true));
cm.set(CommandMessage.QUIT);
assertThat(Consumer.pokeMessagePredicate.onMessage(cm), CoreMatchers.is(false));
assertThat(Consumer.pokeMessagePredicate.onMessage(new RunJobMessage()), CoreMatchers.is(false));
}
Aggregations