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

The following examples show how to use org.apache.nifi.controller.ReportingTaskNode#verifyCanStop() . 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: StandardReportingTaskDAO.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
private void verifyUpdate(final ReportingTaskNode reportingTask, final ReportingTaskDTO reportingTaskDTO) {
    // ensure the state, if specified, is valid
    if (isNotNull(reportingTaskDTO.getState())) {
        try {
            final ScheduledState purposedScheduledState = ScheduledState.valueOf(reportingTaskDTO.getState());

            // only attempt an action if it is changing
            if (!purposedScheduledState.equals(reportingTask.getScheduledState())) {
                // perform the appropriate action
                switch (purposedScheduledState) {
                    case RUNNING:
                        reportingTask.verifyCanStart();
                        break;
                    case STOPPED:
                        switch (reportingTask.getScheduledState()) {
                            case RUNNING:
                                reportingTask.verifyCanStop();
                                break;
                            case DISABLED:
                                reportingTask.verifyCanEnable();
                                break;
                        }
                        break;
                    case DISABLED:
                        reportingTask.verifyCanDisable();
                        break;
                }
            }
        } catch (IllegalArgumentException iae) {
            throw new IllegalArgumentException(String.format(
                    "The specified reporting task state (%s) is not valid. Valid options are 'RUNNING', 'STOPPED', and 'DISABLED'.",
                    reportingTaskDTO.getState()));
        }
    }

    boolean modificationRequest = false;
    if (isAnyNotNull(reportingTaskDTO.getName(),
            reportingTaskDTO.getSchedulingStrategy(),
            reportingTaskDTO.getSchedulingPeriod(),
            reportingTaskDTO.getAnnotationData(),
            reportingTaskDTO.getProperties())) {
        modificationRequest = true;

        // validate the request
        final List<String> requestValidation = validateProposedConfiguration(reportingTask, reportingTaskDTO);

        // ensure there was no validation errors
        if (!requestValidation.isEmpty()) {
            throw new ValidationException(requestValidation);
        }
    }

    if (modificationRequest) {
        reportingTask.verifyCanUpdate();
    }
}
 
Example 2
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 3
Source File: StandardReportingTaskDAO.java    From nifi with Apache License 2.0 4 votes vote down vote up
private void verifyUpdate(final ReportingTaskNode reportingTask, final ReportingTaskDTO reportingTaskDTO) {
    // ensure the state, if specified, is valid
    if (isNotNull(reportingTaskDTO.getState())) {
        try {
            final ScheduledState purposedScheduledState = ScheduledState.valueOf(reportingTaskDTO.getState());

            // only attempt an action if it is changing
            if (!purposedScheduledState.equals(reportingTask.getScheduledState())) {
                // perform the appropriate action
                switch (purposedScheduledState) {
                    case RUNNING:
                        reportingTask.verifyCanStart();
                        break;
                    case STOPPED:
                        switch (reportingTask.getScheduledState()) {
                            case RUNNING:
                                reportingTask.verifyCanStop();
                                break;
                            case DISABLED:
                                reportingTask.verifyCanEnable();
                                break;
                        }
                        break;
                    case DISABLED:
                        reportingTask.verifyCanDisable();
                        break;
                }
            }
        } catch (IllegalArgumentException iae) {
            throw new IllegalArgumentException(String.format(
                    "The specified reporting task state (%s) is not valid. Valid options are 'RUNNING', 'STOPPED', and 'DISABLED'.",
                    reportingTaskDTO.getState()));
        }
    }

    boolean modificationRequest = false;
    if (isAnyNotNull(reportingTaskDTO.getName(),
            reportingTaskDTO.getSchedulingStrategy(),
            reportingTaskDTO.getSchedulingPeriod(),
            reportingTaskDTO.getAnnotationData(),
            reportingTaskDTO.getProperties(),
            reportingTaskDTO.getBundle())) {
        modificationRequest = true;

        // validate the request
        final List<String> requestValidation = validateProposedConfiguration(reportingTask, reportingTaskDTO);

        // ensure there was no validation errors
        if (!requestValidation.isEmpty()) {
            throw new ValidationException(requestValidation);
        }
    }

    final BundleDTO bundleDTO = reportingTaskDTO.getBundle();
    if (bundleDTO != null) {
        // ensures all nodes in a cluster have the bundle, throws exception if bundle not found for the given type
        final BundleCoordinate bundleCoordinate = BundleUtils.getBundle(
                reportingTaskProvider.getExtensionManager(), reportingTask.getCanonicalClassName(), bundleDTO);
        // ensure we are only changing to a bundle with the same group and id, but different version
        reportingTask.verifyCanUpdateBundle(bundleCoordinate);
    }

    if (modificationRequest) {
        reportingTask.verifyCanUpdate();
    }
}
 
Example 4
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);
}