use of org.apache.nifi.controller.tasks.ReportingTaskWrapper in project nifi by apache.
the class QuartzSchedulingAgent method doSchedule.
@Override
public void doSchedule(final ReportingTaskNode taskNode, final LifecycleState scheduleState) {
final List<AtomicBoolean> existingTriggers = canceledTriggers.get(taskNode);
if (existingTriggers != null) {
throw new IllegalStateException("Cannot schedule " + taskNode.getReportingTask().getIdentifier() + " because it is already scheduled to run");
}
final String cronSchedule = taskNode.getSchedulingPeriod();
final CronExpression cronExpression;
try {
cronExpression = new CronExpression(cronSchedule);
} catch (final Exception pe) {
throw new IllegalStateException("Cannot schedule Reporting Task " + taskNode.getReportingTask().getIdentifier() + " to run because its scheduling period is not valid");
}
final ReportingTaskWrapper taskWrapper = new ReportingTaskWrapper(taskNode, scheduleState);
final AtomicBoolean canceled = new AtomicBoolean(false);
final Runnable command = new Runnable() {
@Override
public void run() {
if (canceled.get()) {
return;
}
taskWrapper.run();
if (canceled.get()) {
return;
}
final Date date = cronExpression.getTimeAfter(new Date());
final long delay = date.getTime() - System.currentTimeMillis();
logger.debug("Finished running Reporting Task {}; next scheduled time is at {} after a delay of {} milliseconds", taskNode, date, delay);
flowEngine.schedule(this, delay, TimeUnit.MILLISECONDS);
}
};
final List<AtomicBoolean> triggers = new ArrayList<>(1);
triggers.add(canceled);
canceledTriggers.put(taskNode, triggers);
final Date initialDate = cronExpression.getTimeAfter(new Date());
final long initialDelay = initialDate.getTime() - System.currentTimeMillis();
flowEngine.schedule(command, initialDelay, TimeUnit.MILLISECONDS);
scheduleState.setScheduled(true);
logger.info("Scheduled Reporting Task {} to run threads on schedule {}", taskNode, cronSchedule);
}
Aggregations