@Override
@RequiredPermission(Permission.MANAGE_SETTINGS)
public void scheduleSnapshotManagementOperationsForStorageNode(Subject subject, StorageNode node,
StorageClusterSettings settings) {
Resource resource = node.getResource();
RegularSnapshots rs = settings.getRegularSnapshots();
log.info("Updating snapshot management schedules for " + node);
try {
List<ResourceOperationSchedule> schedules = operationManager.findScheduledResourceOperations(subject,
resource.getId());
if (log.isDebugEnabled()) {
log.debug("Removing original scheduled operations on " + node);
}
for (ResourceOperationSchedule schedule : schedules) {
if (REGULAR_SNAPSHOTS_SCHEDULE_DESCRIPTION.equals(schedule.getDescription())) {
if (log.isDebugEnabled()) {
log.debug("Found operation schedule, unscheduling " + schedule);
}
operationManager.unscheduleResourceOperation(subject, schedule.getJobId().toString(),
resource.getId());
// delete history items that have been scheduled but not yet started
ResourceOperationHistoryCriteria criteria = new ResourceOperationHistoryCriteria();
criteria.setPageControl(PageControl.getUnlimitedInstance());
criteria.addFilterJobId(schedule.getJobId());
criteria.addFilterStatus(OperationRequestStatus.INPROGRESS);
criteria.addFilterStartTime(Long.valueOf(0));
PageList<ResourceOperationHistory> historyItems = operationManager
.findResourceOperationHistoriesByCriteria(subject, criteria);
Iterator<ResourceOperationHistory> iter = historyItems.iterator();
if (iter.hasNext()) {
if (log.isDebugEnabled()) {
log.debug("Wiping out " + historyItems.getTotalSize()
+ " scheduled but not yet started history items");
}
while (iter.hasNext()) {
operationManager.deleteOperationHistory(subject, iter.next().getId(), true);
}
}
}
}
if (rs != null && rs.getEnabled().booleanValue()) {
Configuration parameters = Configuration.builder().addSimple("retentionStrategy", rs.getRetention())
.addSimple("count", rs.getCount()).addSimple("deletionStrategy", rs.getDeletion())
.addSimple("location", rs.getLocation()).build();
ResourceOperationSchedule schedule = operationManager.scheduleResourceOperationUsingCron(subject,
resource.getId(), "takeSnapshot",
rs.getSchedule(), 0, parameters, REGULAR_SNAPSHOTS_SCHEDULE_DESCRIPTION);
if (log.isDebugEnabled()) {
log.debug("Created new " + schedule);
}
}