Skip to content

(fix)wrap schedule in a try catch in case of too many scheduled jobs which… #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.support.annotation.RequiresApi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static android.app.job.JobScheduler.RESULT_SUCCESS;

Expand Down Expand Up @@ -131,10 +132,14 @@ private void setRepeating(long interval, PendingIntent pendingIntent, Intent int

builder.setExtras(persistableBundle);

if (jobScheduler.schedule(builder.build()) != RESULT_SUCCESS) {
logger.error("ServiceScheduler", "Some error while scheduling the job");
try {
if (jobScheduler.schedule(builder.build()) != RESULT_SUCCESS) {
logger.error("ServiceScheduler", "Some error while scheduling the job");
}
}
catch (Exception e) {
logger.error(String.format("Problem scheduling job %s", intent.getComponent().toShortString()), e);
}

}
else {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Expand All @@ -155,11 +160,7 @@ private void cancelRepeating(PendingIntent pendingIntent, Intent intent) {
if (ServiceScheduler.isScheduled(context, id)) {
jobScheduler.cancel(id);
}
} catch (IllegalAccessException e) {
logger.error("Error in Cancel ", e);
} catch (NoSuchFieldException e) {
logger.error("Error in Cancel ", e);
} catch (ClassNotFoundException e) {
} catch (Exception e) {
logger.error("Error in Cancel ", e);
}
}
Expand All @@ -171,16 +172,14 @@ private void cancelRepeating(PendingIntent pendingIntent, Intent intent) {
}

private int getJobId(Intent intent) {
String clazz = intent.getComponent().getClassName();
String clazz = "unknown";
Integer id = null;

try {
clazz = intent.getComponent().getClassName();
id = (Integer) Class.forName(clazz).getDeclaredField("JOB_ID").get(null);
} catch (IllegalAccessException e) {
} catch (Exception e) {
logger.error("Error getting JOB_ID from " + clazz, e);
} catch (NoSuchFieldException e) {
logger.error("Error getting JOB_ID from " + clazz, e);
} catch (ClassNotFoundException e) {
logger.error("Error getting JOB_ID from " + clazz, e);
}

return id == null ? -1 : id;
Expand All @@ -197,7 +196,7 @@ public void unschedule(Intent intent) {
try {
PendingIntent pendingIntent = pendingIntentFactory.getPendingIntent(intent);
cancelRepeating(pendingIntent, intent);
logger.info("Unscheduled {}", intent.getComponent().toShortString());
logger.info("Unscheduled {}", intent.getComponent()!=null ? intent.getComponent().toShortString() : "intent");
} catch (Exception e) {
logger.debug("Failed to unschedule service", e);
}
Expand Down Expand Up @@ -274,7 +273,13 @@ public static void startService(Context context, Integer jobId, Intent intent) {
.build();
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

jobScheduler.enqueue(jobInfo, new JobWorkItem(intent));
JobWorkItem jobWorkItem = new JobWorkItem(intent);
try {
jobScheduler.enqueue(jobInfo, jobWorkItem);
}
catch (Exception e) {
LoggerFactory.getLogger("ServiceScheduler").error("Problem enqueuing work item ", e);
}

}
else {
Expand Down