yigit / android-priority-jobqueue

A Job Queue specifically written for Android to easily schedule jobs (tasks) that run in the background, improving UX and application stability.
3.4k stars 395 forks source link

Job#isCancelled() always returns false in versions 2.0.1 and 1.3.5 #420

Closed isabsent closed 6 years ago

isabsent commented 7 years ago

I call jobManager.cancelJobs(TagConstraint.ANY, NetRequestJob.JOB_TAG) in UI thread an control how does it work in the job background thread:

public class NetRequestJob extends BaseParitetJob {
    public static final String JOB_TAG = "NetRequestJob";

    public NetRequestJob() {
        getParams().addTags(JOB_TAG);
    }

    @Override
    public void onRun() throws Throwable {
        Thread.sleep(5000);

        //I am pressing button on the UI thread at this moment to call
        //jobManager.cancelJobs(TagConstraint.ANY, NetRequestJob.JOB_TAG)

        if (isCancelled()) {
            System.out.println("Cancelled!!!");
            throw new RuntimeException("Cancelled by user!");
        }

        //Do some network call...
    }

isCancelled() is always false. Am I trying to cancel job by a correct way? If not what should I do to cancel it? Usage of jobManager.cancelJobsInBackground(null, TagConstraint.ANY, jobTag) does not change a situation. I have attached demo project which demonstrates the problem - cancellation-fault-demo.zip. When you click on a FAB button job will cancelled, but a breakpoint under

if  (isCancelled()){
    ...
}

will never reached. Migration to 2.0.1 didn't helped me too.

Other classes are:

public abstract class BaseParitetJob extends BaseJob {

    public BaseParitetJob() {
        super(new Params(BACKGROUND).groupBy(Groups.MAIN_CONTENT));
    }
}

abstract public class BaseJob extends Job {
    private static final int RETRY_LIMIT = 1;

    public static final int UI_HIGH = 10;
    public static final int BACKGROUND = 1;

    private Params params;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({UI_HIGH, BACKGROUND})
    public @interface Priority {

    }

    private NetworkException exception;

    public BaseJob(Params params) {
        super(params);
        this.params = params;
    }

    public Params getParams() {
        return params;
    }

    protected boolean shouldRetry(Throwable throwable) {
        if (throwable instanceof NetworkException) {
            exception = (NetworkException) throwable;
            return exception.shouldRetry();
        }
        return true;
    }

    public NetworkException getNetworkException() {
        return exception;
    }

    @Override
    public void onAdded() {
        Timber.d("%s onAdded()", getClass().getName());
    }

    @Override
    protected void onCancel() {
        Timber.e("onCancel %s", getNetworkException());
    }

    @Override
    protected RetryConstraint shouldReRunOnThrowable(Throwable throwable, int runCount, int maxRunCount) {
        if (shouldRetry(throwable)) {
            return RetryConstraint.createExponentialBackoff(runCount, 1000);
        }
        return RetryConstraint.CANCEL;
    }

    @Override
    protected int getRetryLimit() {
        return RETRY_LIMIT;
    }
}
isabsent commented 6 years ago

The same issue is mentioned here.

isabsent commented 6 years ago

The problem was solved. The library works fine. I have made a mistake extending Job. A correct extending is below:.

public abstract public class BaseJob extends Job {
    private static final int RETRY_LIMIT = 3;

    public static final int UI_HIGH = 10;
    public static final int BACKGROUND = 1;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({UI_HIGH, BACKGROUND})
    public @interface Priority {

    }

    public BaseJob(Params params) {
        super(params);
    }
    ...
}

public abstract class BaseNewsJob extends BaseJob {

    public BaseNewsJob() {
        super(new Params(BACKGROUND).groupBy(Groups.MAIN_CONTENT));
    }

    public BaseNewsJob(String... jobTags) {
        super(new Params(BACKGROUND).groupBy(Groups.MAIN_CONTENT).addTags(jobTags));
    }
    ...
}

public class GetNewsJob extends BaseNewsJob {
    public static final String JOB_TAG = "NEWS_JOB_TAG";

    public GetNewsJob(NewsController mNewsController, boolean force) {
        super(JOB_TAG);
    }
    ...
}