| 45 | namespace minifi { |
| 46 | |
| 47 | void ThreadedSchedulingAgent::schedule(std::shared_ptr<core::Processor> processor) { |
| 48 | std::lock_guard<std::mutex> lock(mutex_); |
| 49 | |
| 50 | admin_yield_duration_ = 100; // We should prevent burning CPU in case of rollbacks |
| 51 | std::string yieldValue; |
| 52 | |
| 53 | if (configure_->get(Configure::nifi_administrative_yield_duration, yieldValue)) { |
| 54 | utils::optional<core::TimePeriodValue> value = core::TimePeriodValue::fromString(yieldValue); |
| 55 | if (value) { |
| 56 | admin_yield_duration_ = value->getMilliseconds(); |
| 57 | logger_->log_debug("nifi_administrative_yield_duration: [%" PRId64 "] ms", admin_yield_duration_); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | bored_yield_duration_ = 0; |
| 62 | if (configure_->get(Configure::nifi_bored_yield_duration, yieldValue)) { |
| 63 | utils::optional<core::TimePeriodValue> value = core::TimePeriodValue::fromString(yieldValue); |
| 64 | if (value) { |
| 65 | bored_yield_duration_ = value->getMilliseconds(); |
| 66 | logger_->log_debug("nifi_bored_yield_duration: [%" PRId64 "] ms", bored_yield_duration_); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (processor->getScheduledState() != core::RUNNING) { |
| 71 | logger_->log_debug("Can not schedule threads for processor %s because it is not running", processor->getName()); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if (thread_pool_.isTaskRunning(processor->getUUIDStr())) { |
| 76 | logger_->log_warn("Can not schedule threads for processor %s because there are existing threads running", processor->getName()); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | std::shared_ptr<core::ProcessorNode> processor_node = std::make_shared<core::ProcessorNode>(processor); |
| 81 | |
| 82 | auto contextBuilder = core::ClassLoader::getDefaultClassLoader().instantiate<core::ProcessContextBuilder>("ProcessContextBuilder"); |
| 83 | |
| 84 | contextBuilder = contextBuilder->withContentRepository(content_repo_)->withFlowFileRepository(flow_repo_)->withProvider(controller_service_provider_)->withProvenanceRepository(repo_) |
| 85 | ->withConfiguration(configure_); |
| 86 | |
| 87 | auto processContext = contextBuilder->build(processor_node); |
| 88 | |
| 89 | auto sessionFactory = std::make_shared<core::ProcessSessionFactory>(processContext); |
| 90 | |
| 91 | processor->onSchedule(processContext, sessionFactory); |
| 92 | |
| 93 | std::vector<std::thread *> threads; |
| 94 | |
| 95 | ThreadedSchedulingAgent *agent = this; |
| 96 | for (int i = 0; i < processor->getMaxConcurrentTasks(); i++) { |
| 97 | // reference the disable function from serviceNode |
| 98 | processor->incrementActiveTasks(); |
| 99 | |
| 100 | std::function<utils::TaskRescheduleInfo()> f_ex = [agent, processor, processContext, sessionFactory] () { |
| 101 | return agent->run(processor, processContext, sessionFactory); |
| 102 | }; |
| 103 | |
| 104 | // create a functor that will be submitted to the thread pool. |