Skip to content

Commit d16f542

Browse files
(fix)wrap schedule in a try catch in case of too many scheduled jobs 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
1 parent 8332886 commit d16f542

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

shared/src/main/java/com/optimizely/ab/android/shared/ServiceScheduler.java

+21-16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.support.annotation.RequiresApi;
3232

3333
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
3435

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

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

132133
builder.setExtras(persistableBundle);
133134

134-
if (jobScheduler.schedule(builder.build()) != RESULT_SUCCESS) {
135-
logger.error("ServiceScheduler", "Some error while scheduling the job");
135+
try {
136+
if (jobScheduler.schedule(builder.build()) != RESULT_SUCCESS) {
137+
logger.error("ServiceScheduler", "Some error while scheduling the job");
138+
}
139+
}
140+
catch (Exception e) {
141+
logger.error(String.format("Problem scheduling job %s", intent.getComponent().toShortString()), e);
136142
}
137-
138143
}
139144
else {
140145
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
@@ -155,11 +160,7 @@ private void cancelRepeating(PendingIntent pendingIntent, Intent intent) {
155160
if (ServiceScheduler.isScheduled(context, id)) {
156161
jobScheduler.cancel(id);
157162
}
158-
} catch (IllegalAccessException e) {
159-
logger.error("Error in Cancel ", e);
160-
} catch (NoSuchFieldException e) {
161-
logger.error("Error in Cancel ", e);
162-
} catch (ClassNotFoundException e) {
163+
} catch (Exception e) {
163164
logger.error("Error in Cancel ", e);
164165
}
165166
}
@@ -171,16 +172,14 @@ private void cancelRepeating(PendingIntent pendingIntent, Intent intent) {
171172
}
172173

173174
private int getJobId(Intent intent) {
174-
String clazz = intent.getComponent().getClassName();
175+
String clazz = "unknown";
175176
Integer id = null;
177+
176178
try {
179+
clazz = intent.getComponent().getClassName();
177180
id = (Integer) Class.forName(clazz).getDeclaredField("JOB_ID").get(null);
178-
} catch (IllegalAccessException e) {
181+
} catch (Exception e) {
179182
logger.error("Error getting JOB_ID from " + clazz, e);
180-
} catch (NoSuchFieldException e) {
181-
logger.error("Error getting JOB_ID from " + clazz, e);
182-
} catch (ClassNotFoundException e) {
183-
logger.error("Error getting JOB_ID from " + clazz, e);
184183
}
185184

186185
return id == null ? -1 : id;
@@ -197,7 +196,7 @@ public void unschedule(Intent intent) {
197196
try {
198197
PendingIntent pendingIntent = pendingIntentFactory.getPendingIntent(intent);
199198
cancelRepeating(pendingIntent, intent);
200-
logger.info("Unscheduled {}", intent.getComponent().toShortString());
199+
logger.info("Unscheduled {}", intent.getComponent()!=null ? intent.getComponent().toShortString() : "intent");
201200
} catch (Exception e) {
202201
logger.debug("Failed to unschedule service", e);
203202
}
@@ -274,7 +273,13 @@ public static void startService(Context context, Integer jobId, Intent intent) {
274273
.build();
275274
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
276275

277-
jobScheduler.enqueue(jobInfo, new JobWorkItem(intent));
276+
JobWorkItem jobWorkItem = new JobWorkItem(intent);
277+
try {
278+
jobScheduler.enqueue(jobInfo, jobWorkItem);
279+
}
280+
catch (Exception e) {
281+
LoggerFactory.getLogger("ServiceScheduler").error("Problem enqueuing work item ", e);
282+
}
278283

279284
}
280285
else {

0 commit comments

Comments
 (0)