// keep a copy of the current time
Date currentTime = new Date();
// query for existing lease record first
TaskLock taskLock = null;
try {
taskLock = getTaskLockByName(task.getName());
if(taskLock == null) {
log.warn("Cannot acquire lease when no tasklock record exists for task - "+task.getName());
}
} catch (WebloggerException ex) {
log.warn("Error getting TaskLock", ex);
return false;
}
// try to acquire lease
if(taskLock != null) try {
// calculate lease expiration time
Date leaseExpiration = taskLock.getLeaseExpiration();
// calculate run time for task, this is expected time, not actual time
// i.e. if a task is meant to run daily at midnight this should
// reflect 00:00:00 on the current day
Date runTime = currentTime;
if("startOfDay".equals(task.getStartTimeDesc())) {
// start of today
runTime = DateUtil.getStartOfDay(currentTime);
} else if("startOfHour".equals(task.getStartTimeDesc())) {
// start of this hour
runTime = DateUtil.getStartOfHour(currentTime);
} else {
// start of this minute
runTime = DateUtil.getStartOfMinute(currentTime);
}
if(log.isDebugEnabled()) {
log.debug("last run = "+taskLock.getLastRun());
log.debug("new run time = "+runTime);
log.debug("last acquired = "+taskLock.getTimeAquired());
log.debug("time leased = "+taskLock.getTimeLeased());
log.debug("lease expiration = "+leaseExpiration);
}
Query q = strategy.getNamedUpdate(
"TaskLock.updateClient&Timeacquired&Timeleased&LastRunByName&Timeacquired");
q.setParameter(1, task.getClientId());
q.setParameter(2, Integer.valueOf(task.getLeaseTime()));
q.setParameter(3, new Timestamp(runTime.getTime()));
q.setParameter(4, task.getName());
q.setParameter(5, taskLock.getTimeAquired());
q.setParameter(6, new Timestamp(leaseExpiration.getTime()));
int result = q.executeUpdate();
if(result == 1) {
return true;