Problem to cancel a periodic job
See original GitHub issueandroid-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:
- Created 6 years ago
- Comments:13
Top GitHub Comments
This issue has been fixed in version
1.2.5
. Thanks for reporting it and the great help!That was fast! Thanks.