Java Code Examples for org.apache.nifi.controller.ReportingTaskNode#setScheduledState()

The following examples show how to use org.apache.nifi.controller.ReportingTaskNode#setScheduledState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StandardProcessScheduler.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public synchronized void enableReportingTask(final ReportingTaskNode taskNode) {
    if (taskNode.getScheduledState() != ScheduledState.DISABLED) {
        throw new IllegalStateException("Reporting Task cannot be enabled because it is not disabled");
    }

    taskNode.setScheduledState(ScheduledState.STOPPED);
}
 
Example 2
Source File: StandardProcessScheduler.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public synchronized void disableReportingTask(final ReportingTaskNode taskNode) {
    if (taskNode.getScheduledState() != ScheduledState.STOPPED) {
        throw new IllegalStateException("Reporting Task cannot be disabled because its state is set to " + taskNode.getScheduledState()
                + " but transition to DISABLED state is allowed only from the STOPPED state");
    }

    taskNode.setScheduledState(ScheduledState.DISABLED);
}
 
Example 3
Source File: StandardProcessScheduler.java    From nifi with Apache License 2.0 5 votes vote down vote up
public synchronized void enableReportingTask(final ReportingTaskNode taskNode) {
    if (taskNode.getScheduledState() != ScheduledState.DISABLED) {
        throw new IllegalStateException("Reporting Task cannot be enabled because it is not disabled");
    }

    taskNode.setScheduledState(ScheduledState.STOPPED);
}
 
Example 4
Source File: StandardProcessScheduler.java    From nifi with Apache License 2.0 5 votes vote down vote up
public synchronized void disableReportingTask(final ReportingTaskNode taskNode) {
    if (taskNode.getScheduledState() != ScheduledState.STOPPED) {
        throw new IllegalStateException("Reporting Task cannot be disabled because its state is set to " + taskNode.getScheduledState()
                + " but transition to DISABLED state is allowed only from the STOPPED state");
    }

    taskNode.setScheduledState(ScheduledState.DISABLED);
}
 
Example 5
Source File: StandardProcessScheduler.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void schedule(final ReportingTaskNode taskNode) {
    final ScheduleState scheduleState = getScheduleState(requireNonNull(taskNode));
    if (scheduleState.isScheduled()) {
        return;
    }

    final int activeThreadCount = scheduleState.getActiveThreadCount();
    if (activeThreadCount > 0) {
        throw new IllegalStateException("Reporting Task " + taskNode.getName() + " cannot be started because it has " + activeThreadCount + " threads still running");
    }

    if (!taskNode.isValid()) {
        throw new IllegalStateException("Reporting Task " + taskNode.getName() + " is not in a valid state for the following reasons: " + taskNode.getValidationErrors());
    }

    final SchedulingAgent agent = getSchedulingAgent(taskNode.getSchedulingStrategy());
    scheduleState.setScheduled(true);

    final Runnable startReportingTaskRunnable = new Runnable() {
        @Override
        public void run() {
            final long lastStopTime = scheduleState.getLastStopTime();
            final ReportingTask reportingTask = taskNode.getReportingTask();

            // Continually attempt to start the Reporting Task, and if we fail sleep for a bit each time.
            while (true) {
                try {
                    synchronized (scheduleState) {
                        // if no longer scheduled to run, then we're finished. This can happen, for example,
                        // if the @OnScheduled method throws an Exception and the user stops the reporting task
                        // while we're administratively yielded.
                        // we also check if the schedule state's last start time is equal to what it was before.
                        // if not, then means that the reporting task has been stopped and started again, so we should just
                        // bail; another thread will be responsible for invoking the @OnScheduled methods.
                        if (!scheduleState.isScheduled() || scheduleState.getLastStopTime() != lastStopTime) {
                            return;
                        }

                        try (final NarCloseable x = NarCloseable.withComponentNarLoader(reportingTask.getClass(), reportingTask.getIdentifier())) {
                            ReflectionUtils.invokeMethodsWithAnnotation(OnScheduled.class, reportingTask, taskNode.getConfigurationContext());
                        }

                        agent.schedule(taskNode, scheduleState);
                        return;
                    }
                } catch (final Exception e) {
                    final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
                    final ComponentLog componentLog = new SimpleProcessLogger(reportingTask.getIdentifier(), reportingTask);
                    componentLog.error("Failed to invoke @OnEnabled method due to {}", cause);

                    LOG.error("Failed to invoke the On-Scheduled Lifecycle methods of {} due to {}; administratively yielding this "
                            + "ReportingTask and will attempt to schedule it again after {}",
                            new Object[]{reportingTask, e.toString(), administrativeYieldDuration}, e);

                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnUnscheduled.class, reportingTask, taskNode.getConfigurationContext());
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, reportingTask, taskNode.getConfigurationContext());

                    try {
                        Thread.sleep(administrativeYieldMillis);
                    } catch (final InterruptedException ie) {
                    }
                }
            }
        }
    };

    componentLifeCycleThreadPool.execute(startReportingTaskRunnable);
    taskNode.setScheduledState(ScheduledState.RUNNING);
}
 
Example 6
Source File: StandardProcessScheduler.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void unschedule(final ReportingTaskNode taskNode) {
    final ScheduleState scheduleState = getScheduleState(requireNonNull(taskNode));
    if (!scheduleState.isScheduled()) {
        return;
    }

    taskNode.verifyCanStop();
    final SchedulingAgent agent = getSchedulingAgent(taskNode.getSchedulingStrategy());
    final ReportingTask reportingTask = taskNode.getReportingTask();
    taskNode.setScheduledState(ScheduledState.STOPPED);

    final Runnable unscheduleReportingTaskRunnable = new Runnable() {
        @Override
        public void run() {
            final ConfigurationContext configurationContext = taskNode.getConfigurationContext();

            synchronized (scheduleState) {
                scheduleState.setScheduled(false);

                try {
                    try (final NarCloseable x = NarCloseable.withComponentNarLoader(reportingTask.getClass(), reportingTask.getIdentifier())) {
                        ReflectionUtils.invokeMethodsWithAnnotation(OnUnscheduled.class, reportingTask, configurationContext);
                    }
                } catch (final Exception e) {
                    final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
                    final ComponentLog componentLog = new SimpleProcessLogger(reportingTask.getIdentifier(), reportingTask);
                    componentLog.error("Failed to invoke @OnUnscheduled method due to {}", cause);

                    LOG.error("Failed to invoke the @OnUnscheduled methods of {} due to {}; administratively yielding this ReportingTask and will attempt to schedule it again after {}",
                            reportingTask, cause.toString(), administrativeYieldDuration);
                    LOG.error("", cause);

                    try {
                        Thread.sleep(administrativeYieldMillis);
                    } catch (final InterruptedException ie) {
                    }
                }

                agent.unschedule(taskNode, scheduleState);

                if (scheduleState.getActiveThreadCount() == 0 && scheduleState.mustCallOnStoppedMethods()) {
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, reportingTask, configurationContext);
                }
            }
        }
    };

    componentLifeCycleThreadPool.execute(unscheduleReportingTaskRunnable);
}
 
Example 7
Source File: StandardProcessScheduler.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void schedule(final ReportingTaskNode taskNode) {
    final LifecycleState lifecycleState = getLifecycleState(requireNonNull(taskNode), true);
    if (lifecycleState.isScheduled()) {
        return;
    }

    final int activeThreadCount = lifecycleState.getActiveThreadCount();
    if (activeThreadCount > 0) {
        throw new IllegalStateException("Reporting Task " + taskNode.getName() + " cannot be started because it has " + activeThreadCount + " threads still running");
    }

    final SchedulingAgent agent = getSchedulingAgent(taskNode.getSchedulingStrategy());
    lifecycleState.setScheduled(true);

    final Runnable startReportingTaskRunnable = new Runnable() {
        @Override
        public void run() {
            final long lastStopTime = lifecycleState.getLastStopTime();
            final ReportingTask reportingTask = taskNode.getReportingTask();

            // Attempt to start the Reporting Task, and if we fail re-schedule the task again after #administrativeYielMillis milliseconds
            try {
                synchronized (lifecycleState) {
                    // if no longer scheduled to run, then we're finished. This can happen, for example,
                    // if the @OnScheduled method throws an Exception and the user stops the reporting task
                    // while we're administratively yielded.
                    // we also check if the schedule state's last start time is equal to what it was before.
                    // if not, then means that the reporting task has been stopped and started again, so we should just
                    // bail; another thread will be responsible for invoking the @OnScheduled methods.
                    if (!lifecycleState.isScheduled() || lifecycleState.getLastStopTime() != lastStopTime) {
                        LOG.debug("Did not complete invocation of @OnScheduled task for {} but Lifecycle State is no longer scheduled. Will not attempt to invoke task anymore", reportingTask);
                        return;
                    }

                    final ValidationStatus validationStatus = taskNode.getValidationStatus();
                    if (validationStatus != ValidationStatus.VALID) {
                        LOG.debug("Cannot schedule {} to run because it is currently invalid. Will try again in 5 seconds", taskNode);
                        componentLifeCycleThreadPool.schedule(this, 5, TimeUnit.SECONDS);
                        return;
                    }

                    try (final NarCloseable x = NarCloseable.withComponentNarLoader(flowController.getExtensionManager(), reportingTask.getClass(), reportingTask.getIdentifier())) {
                        ReflectionUtils.invokeMethodsWithAnnotation(OnScheduled.class, reportingTask, taskNode.getConfigurationContext());
                    }

                    agent.schedule(taskNode, lifecycleState);
                }
            } catch (final Exception e) {
                final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
                final ComponentLog componentLog = new SimpleProcessLogger(reportingTask.getIdentifier(), reportingTask);
                componentLog.error("Failed to invoke @OnScheduled method due to {}", cause);

                LOG.error("Failed to invoke the On-Scheduled Lifecycle methods of {} due to {}; administratively yielding this "
                        + "ReportingTask and will attempt to schedule it again after {}",
                        new Object[]{reportingTask, e.toString(), administrativeYieldDuration}, e);


                try (final NarCloseable x = NarCloseable.withComponentNarLoader(flowController.getExtensionManager(), reportingTask.getClass(), reportingTask.getIdentifier())) {
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnUnscheduled.class, reportingTask, taskNode.getConfigurationContext());
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, reportingTask, taskNode.getConfigurationContext());
                }

                componentLifeCycleThreadPool.schedule(this, administrativeYieldMillis, TimeUnit.MILLISECONDS);
            }
        }
    };

    componentLifeCycleThreadPool.execute(startReportingTaskRunnable);
    taskNode.setScheduledState(ScheduledState.RUNNING);
}
 
Example 8
Source File: StandardProcessScheduler.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void unschedule(final ReportingTaskNode taskNode) {
    final LifecycleState lifecycleState = getLifecycleState(requireNonNull(taskNode), false);
    if (!lifecycleState.isScheduled()) {
        return;
    }

    taskNode.verifyCanStop();
    final SchedulingAgent agent = getSchedulingAgent(taskNode.getSchedulingStrategy());
    final ReportingTask reportingTask = taskNode.getReportingTask();
    taskNode.setScheduledState(ScheduledState.STOPPED);

    final Runnable unscheduleReportingTaskRunnable = new Runnable() {
        @Override
        public void run() {
            final ConfigurationContext configurationContext = taskNode.getConfigurationContext();

            synchronized (lifecycleState) {
                lifecycleState.setScheduled(false);

                try (final NarCloseable x = NarCloseable.withComponentNarLoader(flowController.getExtensionManager(), reportingTask.getClass(), reportingTask.getIdentifier())) {
                    ReflectionUtils.invokeMethodsWithAnnotation(OnUnscheduled.class, reportingTask, configurationContext);
                } catch (final Exception e) {
                    final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
                    final ComponentLog componentLog = new SimpleProcessLogger(reportingTask.getIdentifier(), reportingTask);
                    componentLog.error("Failed to invoke @OnUnscheduled method due to {}", cause);

                    LOG.error("Failed to invoke the @OnUnscheduled methods of {} due to {}; administratively yielding this ReportingTask and will attempt to schedule it again after {}",
                            reportingTask, cause.toString(), administrativeYieldDuration);
                    LOG.error("", cause);
                }

                agent.unschedule(taskNode, lifecycleState);

                if (lifecycleState.getActiveThreadCount() == 0 && lifecycleState.mustCallOnStoppedMethods()) {
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, reportingTask, configurationContext);
                }
            }
        }
    };

    componentLifeCycleThreadPool.execute(unscheduleReportingTaskRunnable);
}