Zemian Deng avatar Zemian Deng committed 07151d1

Allow deleteDeadSchedule maxCount set < 0 to remove all schedules.

Comments (0)

Files changed (2)

timemachine-hibernate/src/main/java/timemachine/scheduler/hibernate/HibernateDataStore.java

 			}
 		});
 	}
+	/** Set maxCount < 0 to delete all dead schedules! */
 	@Override
 	public void deleteDeadSchedules(final Long schedulerId, final int maxCount) {
 		hbmSessionTemplate.withSession(new SessionAction() {
 				// TODO: Can we improve this DELETE query? the sub-select might cause performance issue.
 				// Remove any dead Schedule
 				String query = "delete Schedule e where e.deleted = :deleted and e.jobDefId in (from JobDef e2 where e2.schedulerId = :schedulerId) order by e.nextRun asc";
-				int removedCount = session.createQuery(query)
+				Query hbmQuery = session.createQuery(query)
 						.setParameter("schedulerId", schedulerId)
-						.setParameter("deleted", true)
-						.setMaxResults(maxCount)
-						.executeUpdate();
+						.setParameter("deleted", true);
+				if (maxCount > 0)
+					hbmQuery.setMaxResults(maxCount);
+				int removedCount = hbmQuery.executeUpdate();
 				logger.debug("Removed {} Schedules that were marked for deletion.", removedCount);
 				
 			}

timemachine-scheduler/src/main/java/timemachine/scheduler/service/MemoryDataStore.java

 		Schedule schedule = nodeStore.getSchedules().get(scheduleId);
 		return new Tuple<Schedule, JobDef>(schedule, jobDef);
 	}
-	
+
+	/** Set maxCount < 0 to delete all dead schedules! */
 	@Override
 	synchronized public void deleteDeadSchedules(Long schedulerId, int maxCount) {
 		SchedulerNodeStore nodeStore = getSchedulerNodeStore(schedulerId);
 
 		List<Schedule> schedulesToDel = new ArrayList<Schedule>();
 		Iterator<Schedule> scheduleItr = nodeStore.getSortedSchedules().iterator();
-		while (schedulesToDel.size() < maxCount && scheduleItr.hasNext()) {
+		while ((maxCount > 0 && schedulesToDel.size() < maxCount) && scheduleItr.hasNext()) {
 			Schedule schedule = scheduleItr.next();
 			Date nextRun = schedule.getNextRun();
 			if ((nextRun == null && schedule.isDeleted()) && schedule.getState() == State.WAITING) {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.