Search in sources :

Example 1 with ConnectableTask

use of org.apache.nifi.controller.tasks.ConnectableTask in project nifi by apache.

the class QuartzSchedulingAgent method doSchedule.

@Override
public synchronized void doSchedule(final Connectable connectable, final LifecycleState scheduleState) {
    final List<AtomicBoolean> existingTriggers = canceledTriggers.get(connectable);
    if (existingTriggers != null) {
        throw new IllegalStateException("Cannot schedule " + connectable + " because it is already scheduled to run");
    }
    final String cronSchedule = connectable.getSchedulingPeriod();
    final CronExpression cronExpression;
    try {
        cronExpression = new CronExpression(cronSchedule);
    } catch (final Exception pe) {
        throw new IllegalStateException("Cannot schedule " + connectable + " to run because its scheduling period is not valid");
    }
    final List<AtomicBoolean> triggers = new ArrayList<>();
    for (int i = 0; i < connectable.getMaxConcurrentTasks(); i++) {
        final ConnectableTask continuallyRunTask = new ConnectableTask(this, connectable, flowController, contextFactory, scheduleState, encryptor);
        final AtomicBoolean canceled = new AtomicBoolean(false);
        final Runnable command = new Runnable() {

            @Override
            public void run() {
                if (canceled.get()) {
                    return;
                }
                try {
                    continuallyRunTask.invoke();
                } catch (final RuntimeException re) {
                    throw re;
                } catch (final Exception e) {
                    throw new ProcessException(e);
                }
                if (canceled.get()) {
                    return;
                }
                final Date date = cronExpression.getTimeAfter(new Date());
                final long delay = date.getTime() - System.currentTimeMillis();
                logger.debug("Finished task for {}; next scheduled time is at {} after a delay of {} milliseconds", connectable, date, delay);
                flowEngine.schedule(this, delay, TimeUnit.MILLISECONDS);
            }
        };
        final Date initialDate = cronExpression.getTimeAfter(new Date());
        final long initialDelay = initialDate.getTime() - System.currentTimeMillis();
        flowEngine.schedule(command, initialDelay, TimeUnit.MILLISECONDS);
        triggers.add(canceled);
    }
    canceledTriggers.put(connectable, triggers);
    logger.info("Scheduled {} to run with {} threads on schedule {}", connectable, connectable.getMaxConcurrentTasks(), cronSchedule);
}
Also used : ArrayList(java.util.ArrayList) ProcessException(org.apache.nifi.processor.exception.ProcessException) Date(java.util.Date) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProcessException(org.apache.nifi.processor.exception.ProcessException) ConnectableTask(org.apache.nifi.controller.tasks.ConnectableTask) CronExpression(org.quartz.CronExpression)

Example 2 with ConnectableTask

use of org.apache.nifi.controller.tasks.ConnectableTask in project nifi by apache.

the class TimerDrivenSchedulingAgent method doSchedule.

@Override
public void doSchedule(final Connectable connectable, final LifecycleState scheduleState) {
    final List<ScheduledFuture<?>> futures = new ArrayList<>();
    final ConnectableTask connectableTask = new ConnectableTask(this, connectable, flowController, contextFactory, scheduleState, encryptor);
    for (int i = 0; i < connectable.getMaxConcurrentTasks(); i++) {
        // Determine the task to run and create it.
        final AtomicReference<ScheduledFuture<?>> futureRef = new AtomicReference<>();
        final Runnable trigger = createTrigger(connectableTask, scheduleState, futureRef);
        // Schedule the task to run
        final ScheduledFuture<?> future = flowEngine.scheduleWithFixedDelay(trigger, 0L, connectable.getSchedulingPeriod(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
        // now that we have the future, set the atomic reference so that if the component is yielded we
        // are able to then cancel this future.
        futureRef.set(future);
        // Keep track of the futures so that we can update the ScheduleState.
        futures.add(future);
    }
    scheduleState.setFutures(futures);
    logger.info("Scheduled {} to run with {} threads", connectable, connectable.getMaxConcurrentTasks());
}
Also used : ArrayList(java.util.ArrayList) ConnectableTask(org.apache.nifi.controller.tasks.ConnectableTask) AtomicReference(java.util.concurrent.atomic.AtomicReference) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Aggregations

ArrayList (java.util.ArrayList)2 ConnectableTask (org.apache.nifi.controller.tasks.ConnectableTask)2 Date (java.util.Date)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 CronExpression (org.quartz.CronExpression)1