Search in sources :

Example 1 with RunJobMessage

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;
    }
}
Also used : MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) RunJobMessage(com.birbit.android.jobqueue.messaging.message.RunJobMessage) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage)

Example 2 with RunJobMessage

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));
}
Also used : PriorityMessageQueue(com.birbit.android.jobqueue.messaging.PriorityMessageQueue) JobHolder(com.birbit.android.jobqueue.JobHolder) MessageFactory(com.birbit.android.jobqueue.messaging.MessageFactory) MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) Consumer(com.birbit.android.jobqueue.ConsumerManager.Consumer) Message(com.birbit.android.jobqueue.messaging.Message) RunJobResultMessage(com.birbit.android.jobqueue.messaging.message.RunJobResultMessage) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) JobConsumerIdleMessage(com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage) RunJobMessage(com.birbit.android.jobqueue.messaging.message.RunJobMessage) SafeMessageQueue(com.birbit.android.jobqueue.messaging.SafeMessageQueue) RunJobMessage(com.birbit.android.jobqueue.messaging.message.RunJobMessage) RunJobResultMessage(com.birbit.android.jobqueue.messaging.message.RunJobResultMessage) Test(org.junit.Test)

Example 3 with RunJobMessage

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);
}
Also used : PriorityMessageQueue(com.birbit.android.jobqueue.messaging.PriorityMessageQueue) JobHolder(com.birbit.android.jobqueue.JobHolder) MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) MessageFactory(com.birbit.android.jobqueue.messaging.MessageFactory) MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) Consumer(com.birbit.android.jobqueue.ConsumerManager.Consumer) SafeMessageQueue(com.birbit.android.jobqueue.messaging.SafeMessageQueue) RunJobMessage(com.birbit.android.jobqueue.messaging.message.RunJobMessage) Test(org.junit.Test)

Example 4 with RunJobMessage

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));
}
Also used : RunJobMessage(com.birbit.android.jobqueue.messaging.message.RunJobMessage) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) Test(org.junit.Test)

Aggregations

RunJobMessage (com.birbit.android.jobqueue.messaging.message.RunJobMessage)4 MessageQueueConsumer (com.birbit.android.jobqueue.messaging.MessageQueueConsumer)3 CommandMessage (com.birbit.android.jobqueue.messaging.message.CommandMessage)3 Test (org.junit.Test)3 Consumer (com.birbit.android.jobqueue.ConsumerManager.Consumer)2 JobHolder (com.birbit.android.jobqueue.JobHolder)2 MessageFactory (com.birbit.android.jobqueue.messaging.MessageFactory)2 PriorityMessageQueue (com.birbit.android.jobqueue.messaging.PriorityMessageQueue)2 SafeMessageQueue (com.birbit.android.jobqueue.messaging.SafeMessageQueue)2 Message (com.birbit.android.jobqueue.messaging.Message)1 JobConsumerIdleMessage (com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage)1 RunJobResultMessage (com.birbit.android.jobqueue.messaging.message.RunJobResultMessage)1 MockTimer (com.birbit.android.jobqueue.test.timer.MockTimer)1