Skip to content

Commit

Permalink
Job deletion when no triggers reference them
Browse files Browse the repository at this point in the history
  • Loading branch information
vladvoic committed Oct 22, 2012
1 parent 70bdf3a commit 15e0415
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public interface Constants {

public static final String JOB_DESCRIPTION = "jobDescription";
public static final String JOB_CLASS = "jobClass";
public static final String JOB_DURABILITY = "durability";
public static final String TRIGGER_CALENDAR_NAME = "calendarName";
public static final String TRIGGER_DESCRIPTION = "description";
public static final String TRIGGER_END_TIME = "endTime";
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/novemberain/quartz/mongodb/MongoDBJobStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,29 @@ public void storeTrigger(OperableTrigger newTrigger, boolean replaceExisting) th
}
}

// If the removal of the Trigger results in an 'orphaned' Job that is not 'durable',
// then the job should be removed also.
public boolean removeTrigger(TriggerKey triggerKey) throws JobPersistenceException {
BasicDBObject dbObject = Keys.keyToDBObject(triggerKey);
DBCursor find = triggerCollection.find(dbObject);
if (find.count() > 0) {
DBCursor triggers = triggerCollection.find(dbObject);
if (triggers.count() > 0) {
DBObject trigger = triggers.next();
if (trigger.containsField( TRIGGER_JOB_ID )) {
// There is only 1 job per trigger so no need to look further than 1 job.
DBObject job = jobCollection.findOne(new BasicDBObject("_id", trigger.get(TRIGGER_JOB_ID)));
// Durability is not yet implemented in MongoDBJOBStore so next will always be true
if (!job.containsField( JOB_DURABILITY ) || job.get( JOB_DURABILITY ).toString() == "false") {
// Check if this job is referenced by other triggers.
BasicDBObject query = new BasicDBObject();
query.put(TRIGGER_JOB_ID, job.get( "_id" ));
DBCursor referencedTriggers = triggerCollection.find(query);
if (referencedTriggers != null && referencedTriggers.count() <= 1) {
jobCollection.remove( job );
}
}
} else {
log.debug("The triggger had no associated jobs");
}
triggerCollection.remove(dbObject);

return true;
Expand Down

0 comments on commit 15e0415

Please sign in to comment.