| 34 | |
| 35 | template <typename T> |
| 36 | class SQLProcessor: public core::Processor { |
| 37 | protected: |
| 38 | SQLProcessor(const std::string& name, utils::Identifier uuid) |
| 39 | : core::Processor(name, uuid), logger_(logging::LoggerFactory<T>::getLogger()) { |
| 40 | } |
| 41 | |
| 42 | void onSchedule(const std::shared_ptr<core::ProcessContext>& context, const std::shared_ptr<core::ProcessSessionFactory>& sessionFactory) override { |
| 43 | std::string controllerService; |
| 44 | context->getProperty(dbControllerService().getName(), controllerService); |
| 45 | |
| 46 | dbService_ = std::dynamic_pointer_cast<sql::controllers::DatabaseService>(context->getControllerService(controllerService)); |
| 47 | if (!dbService_) |
| 48 | throw minifi::Exception(PROCESSOR_EXCEPTION, "'DB Controller Service' must be defined"); |
| 49 | |
| 50 | static_cast<T*>(this)->processOnSchedule(*context); |
| 51 | } |
| 52 | |
| 53 | void onTrigger(const std::shared_ptr<core::ProcessContext>& context, const std::shared_ptr<core::ProcessSession>& session) override { |
| 54 | std::unique_lock<std::mutex> lock(onTriggerMutex_, std::try_to_lock); |
| 55 | if (!lock.owns_lock()) { |
| 56 | logger_->log_warn("'onTrigger' is called before previous 'onTrigger' call is finished."); |
| 57 | context->yield(); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | try { |
| 62 | if (!connection_) { |
| 63 | connection_ = dbService_->getConnection(); |
| 64 | } |
| 65 | static_cast<T*>(this)->processOnTrigger(*session); |
| 66 | } catch (std::exception& e) { |
| 67 | logger_->log_error("SQLProcessor: '%s'", e.what()); |
| 68 | if (connection_) { |
| 69 | std::string exp; |
| 70 | if (!connection_->connected(exp)) { |
| 71 | logger_->log_error("SQLProcessor: Connection exception: %s", exp.c_str()); |
| 72 | connection_.reset(); |
| 73 | } |
| 74 | } |
| 75 | context->yield(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void notifyStop() override { |
| 80 | connection_.reset(); |
| 81 | } |
| 82 | |
| 83 | protected: |
| 84 | static const core::Property& dbControllerService() { |
| 85 | static const core::Property s_dbControllerService = |
| 86 | core::PropertyBuilder::createProperty("DB Controller Service")-> |
| 87 | isRequired(true)-> |
| 88 | withDescription("Database Controller Service.")-> |
| 89 | supportsExpressionLanguage(true)-> |
| 90 | build(); |
| 91 | return s_dbControllerService; |
| 92 | } |
| 93 |
nothing calls this directly
no test coverage detected