question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem to cancel a periodic job

See original GitHub issue

android-job version: 1.2.4

We have an use case when basically we need to start a Job immediately - startNow() - (based on a button click). If an error occur (e.g. failed to sync), another job must be schedule to run every 30min - setPeriodic() -. When our data is successfully sync, this job can be canceled.

What have we tried to do:

1 - We create a simple job using startNow() 2 - If an error occur in this job, we schedule a new periodic job (with the same TAG). The periodic job is only scheduled if the job that receives the error when syncing data is the one created using startNow(), that is the reason we use this: if (!params.isPeriodic()) 3 - If an error occur when a periodic job is running, we just ignore and return Result.FAILURE. Because that job is already automatically rescheduled by the library 4 - If the periodic job finishes correctly, we cancel all Jobs with the same tag (cancelAllJobsForTag()) and then we return Result.SUCCESS

Here is the code:

public class DemoSyncJob extends Job {
    public static final String TAG = "demo_sync_job";
    private static final String DEBUG_LOG_TAG = "LOG::DemoSyncJob";
    private static final long PERIODIC_TIME_MS = BuildConfig.DEBUG
            ? TimeUnit.SECONDS.toMillis(60)
            : TimeUnit.MINUTES.toMillis(30);
    private static final long FLEX_MS = BuildConfig.DEBUG
            ? TimeUnit.SECONDS.toMillis(30)
            : TimeUnit.MINUTES.toMillis(5);

    @NonNull
    @Override
    protected Result onRunJob(@NonNull final Params params) {
        Log.d(DEBUG_LOG_TAG, "onRunJob(params)");
        try {
            final Context context = getContext();
            if (AndroidUtils.isNetworkAvailable(context)) {
                final DemoRepositorio repo = Injection.provideIntervaloRepositorio();
                new DemoSyncEngine().sync(repo, context);
                cancelAllJobsForTag();
                return Result.SUCCESS;
            } else {
                Log.d(DEBUG_LOG_TAG, "No internet connection");
                if (!params.isPeriodic()) {
                    runPeriodic();
                }
                return Result.FAILURE;
            }
        } catch (Throwable throwable) {
            Log.e(DEBUG_LOG_TAG, "sync job error", throwable);
            if (!params.isPeriodic()) {
                runPeriodic();
            }
            return Result.FAILURE;
        }
    }

    public static void runJobImmediately() {
        Log.d(DEBUG_LOG_TAG, "scheduleJob");
        startNow();
    }

    private static void startNow() {
        Log.d(DEBUG_LOG_TAG, "startNow");

        new JobRequest.Builder(TAG)
                .startNow()
                .build()
                .schedule();
    }

    private static void runPeriodic() {
        Log.d(DEBUG_LOG_TAG, "runPeriodic");

        new JobRequest.Builder(TAG)
                .setPeriodic(PERIODIC_TIME_MS, FLEX_MS)
                .build()
                .schedule();
    }

    private static void cancelAllJobsForTag() {
        Log.d(DEBUG_LOG_TAG, "Cancel job TAG: " + TAG);
        JobManager.instance().cancelAllForTag(TAG);
    }
}

Our problem is that when we cancel a Periodic Job (e.g. when successfully sync data), it strangely does not get cancelled and is rescheduled indefinitely (runs every 30min).

What are we doing wrong? There is any other way to accomplish this use case?

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
vRallevcommented, Mar 19, 2018

This issue has been fixed in version 1.2.5. Thanks for reporting it and the great help!

0reactions
luizfpcommented, Mar 19, 2018

That was fast! Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cancelling a periodic job - SAP Community
If you want to cancel the current job then you can do so from SM50 by selecting that particualr background process and then...
Read more >
android - Stop the scheduled Periodic Job? - Stack Overflow
I mean if you want to cancel the job after application re-starts. According to the Wiki, the job can also be canceled with...
Read more >
SAP How To Cancel Periodic Job Transaction Codes
# TCODE Description 1 SM37 Overview of job selection 2 SM36 Schedule Background job 3 SE38 ABAP Editor
Read more >
How to forever delete a user job that is scheduled through sm36
His ID in sapis deleted but the jobs that created by him through sm36 is still trying to run every day. The result...
Read more >
Stop the Batch Job through SM37 for Indefinite - STechies
How to stop the batch job through sm37 for indefinite, you can cancel the active job from sm37. From the job list in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found