Search in sources :

Example 1 with CancelMessage

use of com.birbit.android.jobqueue.messaging.message.CancelMessage in project android-priority-jobqueue by yigit.

the class JobManagerThread method run.

@Override
public void run() {
    messageQueue.consume(new MessageQueueConsumer() {

        @Override
        public void handleMessage(Message message) {
            canScheduleConstraintChangeOnIdle = true;
            switch(message.type) {
                case ADD_JOB:
                    handleAddJob((AddJobMessage) message);
                    break;
                case JOB_CONSUMER_IDLE:
                    boolean busy = consumerManager.handleIdle((JobConsumerIdleMessage) message);
                    if (!busy) {
                        invokeSchedulersIfIdle();
                    }
                    break;
                case RUN_JOB_RESULT:
                    handleRunJobResult((RunJobResultMessage) message);
                    break;
                case CONSTRAINT_CHANGE:
                    boolean handled = consumerManager.handleConstraintChange();
                    ConstraintChangeMessage constraintChangeMessage = (ConstraintChangeMessage) message;
                    canScheduleConstraintChangeOnIdle = handled || !constraintChangeMessage.isForNextJob();
                    break;
                case CANCEL:
                    handleCancel((CancelMessage) message);
                    break;
                case PUBLIC_QUERY:
                    handlePublicQuery((PublicQueryMessage) message);
                    break;
                case COMMAND:
                    handleCommand((CommandMessage) message);
                    break;
                case SCHEDULER:
                    handleSchedulerMessage((SchedulerMessage) message);
                    break;
            }
        }

        @Override
        public void onIdle() {
            JqLog.v("joq idle. running:? %s", running);
            if (!running) {
                return;
            }
            if (!canScheduleConstraintChangeOnIdle) {
                JqLog.v("skipping scheduling a new idle callback because looks like last one" + " did not do anything");
                return;
            }
            Long nextJobTimeNs = getNextWakeUpNs(true);
            // TODO check network should be another message which goes idle if network is the
            // same as now
            JqLog.d("Job queue idle. next job at: %s", nextJobTimeNs);
            if (nextJobTimeNs != null) {
                ConstraintChangeMessage constraintMessage = messageFactory.obtain(ConstraintChangeMessage.class);
                constraintMessage.setForNextJob(true);
                messageQueue.postAt(constraintMessage, nextJobTimeNs);
            } else if (scheduler != null) {
                // if we have a scheduler but the queue is empty, just clean them all.
                if (shouldCancelAllScheduledWhenEmpty && persistentJobQueue.count() == 0) {
                    shouldCancelAllScheduledWhenEmpty = false;
                    scheduler.cancelAll();
                }
            }
        }
    });
}
Also used : SchedulerMessage(com.birbit.android.jobqueue.messaging.message.SchedulerMessage) PublicQueryMessage(com.birbit.android.jobqueue.messaging.message.PublicQueryMessage) SchedulerMessage(com.birbit.android.jobqueue.messaging.message.SchedulerMessage) AddJobMessage(com.birbit.android.jobqueue.messaging.message.AddJobMessage) 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) CancelMessage(com.birbit.android.jobqueue.messaging.message.CancelMessage) ConstraintChangeMessage(com.birbit.android.jobqueue.messaging.message.ConstraintChangeMessage) CancelMessage(com.birbit.android.jobqueue.messaging.message.CancelMessage) AddJobMessage(com.birbit.android.jobqueue.messaging.message.AddJobMessage) PublicQueryMessage(com.birbit.android.jobqueue.messaging.message.PublicQueryMessage) ConstraintChangeMessage(com.birbit.android.jobqueue.messaging.message.ConstraintChangeMessage) MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) JobConsumerIdleMessage(com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage) RunJobResultMessage(com.birbit.android.jobqueue.messaging.message.RunJobResultMessage) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage)

Example 2 with CancelMessage

use of com.birbit.android.jobqueue.messaging.message.CancelMessage in project android-priority-jobqueue by yigit.

the class JobManager method cancelJobs.

/**
     * Cancels jobs that match the given criteria. This method blocks until the cancellation is
     * handled, which might be a long time if a Job that matches the given criteria is currently
     * running. Consider using
     * {@link #cancelJobsInBackground(CancelResult.AsyncCancelCallback, TagConstraint, String...)}
     * if possible.
     * <p>
     * You cannot call this method on the main thread because it may potentially block it for a long
     * time.
     *
     * @param constraint The constraints to be used for tags
     * @param tags The list of tags
     *
     * @return A cancel result that has the list of cancelled and failed to cancel Jobs. A job
     * might fail to cancel if it already started before cancel request is handled.
     */
public CancelResult cancelJobs(TagConstraint constraint, String... tags) {
    assertNotInMainThread("Cannot call this method on main thread. Use cancelJobsInBackground" + " instead");
    assertNotInJobManagerThread("Cannot call this method on JobManager's thread. Use" + "cancelJobsInBackground instead");
    if (constraint == null) {
        throw new IllegalArgumentException("must provide a TagConstraint");
    }
    final CountDownLatch latch = new CountDownLatch(1);
    final CancelResult[] result = new CancelResult[1];
    CancelResult.AsyncCancelCallback myCallback = new CancelResult.AsyncCancelCallback() {

        @Override
        public void onCancelled(CancelResult cancelResult) {
            result[0] = cancelResult;
            latch.countDown();
        }
    };
    CancelMessage message = messageFactory.obtain(CancelMessage.class);
    message.setConstraint(constraint);
    message.setTags(tags);
    message.setCallback(myCallback);
    messageQueue.post(message);
    try {
        latch.await();
    } catch (InterruptedException ignored) {
    }
    return result[0];
}
Also used : CancelMessage(com.birbit.android.jobqueue.messaging.message.CancelMessage) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with CancelMessage

use of com.birbit.android.jobqueue.messaging.message.CancelMessage in project android-priority-jobqueue by yigit.

the class JobManager method cancelJobsInBackground.

/**
     * Cancels the Jobs that match the given criteria. If a Job that matches the criteria is
     * currently running, JobManager waits until it finishes its {@link Job#onRun()} method before
     * calling the callback.
     *
     * @param cancelCallback The callback to call once cancel is handled
     * @param constraint The constraint to be used to match tags
     * @param tags The list of tags
     */
public void cancelJobsInBackground(final CancelResult.AsyncCancelCallback cancelCallback, final TagConstraint constraint, final String... tags) {
    if (constraint == null) {
        throw new IllegalArgumentException("must provide a TagConstraint");
    }
    CancelMessage message = messageFactory.obtain(CancelMessage.class);
    message.setCallback(cancelCallback);
    message.setConstraint(constraint);
    message.setTags(tags);
    messageQueue.post(message);
}
Also used : CancelMessage(com.birbit.android.jobqueue.messaging.message.CancelMessage)

Aggregations

CancelMessage (com.birbit.android.jobqueue.messaging.message.CancelMessage)3 Message (com.birbit.android.jobqueue.messaging.Message)1 MessageQueueConsumer (com.birbit.android.jobqueue.messaging.MessageQueueConsumer)1 AddJobMessage (com.birbit.android.jobqueue.messaging.message.AddJobMessage)1 CommandMessage (com.birbit.android.jobqueue.messaging.message.CommandMessage)1 ConstraintChangeMessage (com.birbit.android.jobqueue.messaging.message.ConstraintChangeMessage)1 JobConsumerIdleMessage (com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage)1 PublicQueryMessage (com.birbit.android.jobqueue.messaging.message.PublicQueryMessage)1 RunJobResultMessage (com.birbit.android.jobqueue.messaging.message.RunJobResultMessage)1 SchedulerMessage (com.birbit.android.jobqueue.messaging.message.SchedulerMessage)1 CountDownLatch (java.util.concurrent.CountDownLatch)1