Skip to content

Commit

Permalink
Check job requirements are satisfied in between retry iterations.
Browse files Browse the repository at this point in the history
// FREEBIE
  • Loading branch information
moxie0 committed Oct 21, 2014
1 parent 4cdc0a3 commit c3eb0ea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
9 changes: 9 additions & 0 deletions jobqueue/src/main/java/org/whispersystems/jobqueue/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public abstract class Job implements Serializable {
private final JobParameters parameters;

private transient long persistentId;
private transient int runIteration;

public Job(JobParameters parameters) {
this.parameters = parameters;
Expand Down Expand Up @@ -71,6 +72,14 @@ public long getPersistentId() {
return persistentId;
}

public int getRunIteration() {
return runIteration;
}

public void setRunIteration(int runIteration) {
this.runIteration = runIteration;
}

public abstract void onAdded();
public abstract void onRun() throws Throwable;
public abstract void onCanceled();
Expand Down
36 changes: 25 additions & 11 deletions jobqueue/src/main/java/org/whispersystems/jobqueue/JobConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

public class JobConsumer extends Thread {

enum JobResult {
SUCCESS,
FAILURE,
DEFERRED
}

private final JobQueue jobQueue;
private final PersistentStorage persistentStorage;

Expand All @@ -34,12 +40,16 @@ public void run() {
while (true) {
Job job = jobQueue.getNext();

if (!runJob(job)) {
job.onCanceled();
}
JobResult result;

if (job.isPersistent()) {
persistentStorage.remove(job.getPersistentId());
if ((result = runJob(job)) != JobResult.DEFERRED) {
if (result == JobResult.FAILURE) {
job.onCanceled();
}

if (job.isPersistent()) {
persistentStorage.remove(job.getPersistentId());
}
}

if (job.getGroupId() != null) {
Expand All @@ -48,21 +58,25 @@ public void run() {
}
}

private boolean runJob(Job job) {
int retryCount = job.getRetryCount();
private JobResult runJob(Job job) {
int retryCount = job.getRetryCount();
int runIteration = job.getRunIteration();

for (int i=retryCount;i>0;i--) {
for (;runIteration<retryCount;runIteration++) {
try {
job.onRun();
return true;
return JobResult.SUCCESS;
} catch (Throwable throwable) {
if (!job.onShouldRetry(throwable)) {
return false;
return JobResult.FAILURE;
} else if (!job.isRequirementsMet()) {
job.setRunIteration(runIteration+1);
return JobResult.DEFERRED;
}
}
}

return false;
return JobResult.FAILURE;
}

}

0 comments on commit c3eb0ea

Please sign in to comment.