Search code examples
javaandroidpriority-queuejob-queueandroid-priority-jobqueue

Retrieve current list of active jobs in android-priority-jobqueue


I'd like to know if there's a recommended way of retrieving a list of active jobs in https://github.com/yigit/android-priority-jobqueue

That way, if I have persistent jobs still waiting, I can let the user know which ones.


Solution

  • We don't provide such an API because it may lead to mis-uses. e.g. checking a job if it exists to decide whether to add or not (because that is recipe for race conditions).

    If you really need this, you can override the persistent queue and provide your own queue where you can track job additions/removals. See the JobManager configuration WIKI for details:

    https://github.com/yigit/android-priority-jobqueue/wiki/Job-Manager-Configuration

    edit: An example race condition

    Someone may write a code like this:

    if (!jobManager.hasJobOfType(ImportantJob.class)) {
        jobManager.addJob(new ImportantJob());
    } else {
        // it is already added, no need to re add
    }
    

    There is a potential race condition here where hasJobOfType returns true but meanwhile Job is about to be canceled. Now, there is no job to run :/. Another race condition is, somewhere else the code might have called addJobInBackground so it will be added but it is not yet added at the time of the hasJobOfType call.

    The best solution for this kind of cases is to re-create another job in onCancel or manually handling the cancelation of previous jobs in onRun method (with a global counter etc). Another option is to use cancelAsync method with its callback version.

    http://yigit.github.io/android-priority-jobqueue/javadoc/index.html