Skip to content

Commit

Permalink
(fix)wrap schedule in a try catch in case of too many scheduled jobs …
Browse files Browse the repository at this point in the history
…which… (#236)

* wrap schedule in a try catch in case of too many scheduled jobs which throws an exception

* added better logging and exception handling
  • Loading branch information
thomaszurkan-optimizely committed Nov 8, 2018
1 parent 8332886 commit d16f542
Showing 1 changed file with 21 additions and 16 deletions.
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

0 comments on commit d16f542

Please sign in to comment.