You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2021/12/22 08:43:54 UTC

[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #1228: MINIFICPP-1703 ExecuteScript error handling fix, more docs and tests

arpadboda commented on a change in pull request #1228:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1228#discussion_r773700888



##########
File path: extensions/script/ExecuteScript.cpp
##########
@@ -79,80 +79,72 @@ void ExecuteScript::onSchedule(core::ProcessContext *context, core::ProcessSessi
   context->getProperty(ScriptBody.getName(), script_body_);
   context->getProperty(ModuleDirectory.getName(), module_directory_);
 
-  if (script_file_.empty() && script_engine_.empty()) {
+  if (script_file_.empty() && script_body_.empty()) {
     logger_->log_error("Either Script Body or Script File must be defined");
     return;
   }
 }
 
 void ExecuteScript::onTrigger(const std::shared_ptr<core::ProcessContext> &context,
                               const std::shared_ptr<core::ProcessSession> &session) {
-  try {
-    std::shared_ptr<script::ScriptEngine> engine;
+  std::shared_ptr<script::ScriptEngine> engine;
 
-    // Use an existing engine, if one is available
-    if (script_engine_q_.try_dequeue(engine)) {
-      logger_->log_debug("Using available %s script engine instance", script_engine_);
-    } else {
-      logger_->log_info("Creating new %s script instance", script_engine_);
-      logger_->log_info("Approximately %d %s script instances created for this processor",
-                        script_engine_q_.size_approx(),
-                        script_engine_);
+  // Use an existing engine, if one is available
+  if (script_engine_q_.try_dequeue(engine)) {
+    logger_->log_debug("Using available %s script engine instance", script_engine_);
+  } else {
+    logger_->log_info("Creating new %s script instance", script_engine_);
+    logger_->log_info("Approximately %d %s script instances created for this processor",
+                      script_engine_q_.size_approx(),
+                      script_engine_);
 
-      if (script_engine_ == "python") {
+    if (script_engine_ == "python") {
 #ifdef PYTHON_SUPPORT
-        engine = createEngine<python::PythonScriptEngine>();
+      engine = createEngine<python::PythonScriptEngine>();
 #else
-        throw std::runtime_error("Python support is disabled in this build.");
+      throw std::runtime_error("Python support is disabled in this build.");
 #endif  // PYTHON_SUPPORT
-      } else if (script_engine_ == "lua") {
+    } else if (script_engine_ == "lua") {
 #ifdef LUA_SUPPORT
-        engine = createEngine<lua::LuaScriptEngine>();
+      engine = createEngine<lua::LuaScriptEngine>();
 #else
-        throw std::runtime_error("Lua support is disabled in this build.");
+      throw std::runtime_error("Lua support is disabled in this build.");
 #endif  // LUA_SUPPORT
-      }
-
-      if (engine == nullptr) {
-        throw std::runtime_error("No script engine available");
-      }
-
-      if (!script_body_.empty()) {
-        engine->eval(script_body_);
-      } else if (!script_file_.empty()) {
-        engine->evalFile(script_file_);
-      } else {
-        throw std::runtime_error("Neither Script Body nor Script File is available to execute");
-      }
     }
 
-    if (script_engine_ == "python") {
+    if (engine == nullptr) {
+      throw std::runtime_error("No script engine available");
+    }
+
+    if (!script_body_.empty()) {
+      engine->eval(script_body_);
+    } else if (!script_file_.empty()) {
+      engine->evalFile(script_file_);
+    } else {
+      throw std::runtime_error("Neither Script Body nor Script File is available to execute");
+    }
+  }
+
+  if (script_engine_ == "python") {
 #ifdef PYTHON_SUPPORT

Review comment:
       Wouldn't it be easier to check if engine is valid (not nullptr) here?
   In case python support is disabled, I think the error above at engine init is already thrown. 

##########
File path: extensions/script/ExecuteScript.cpp
##########
@@ -79,80 +79,72 @@ void ExecuteScript::onSchedule(core::ProcessContext *context, core::ProcessSessi
   context->getProperty(ScriptBody.getName(), script_body_);
   context->getProperty(ModuleDirectory.getName(), module_directory_);
 
-  if (script_file_.empty() && script_engine_.empty()) {
+  if (script_file_.empty() && script_body_.empty()) {
     logger_->log_error("Either Script Body or Script File must be defined");
     return;
   }
 }
 
 void ExecuteScript::onTrigger(const std::shared_ptr<core::ProcessContext> &context,
                               const std::shared_ptr<core::ProcessSession> &session) {
-  try {
-    std::shared_ptr<script::ScriptEngine> engine;
+  std::shared_ptr<script::ScriptEngine> engine;
 
-    // Use an existing engine, if one is available
-    if (script_engine_q_.try_dequeue(engine)) {
-      logger_->log_debug("Using available %s script engine instance", script_engine_);
-    } else {
-      logger_->log_info("Creating new %s script instance", script_engine_);
-      logger_->log_info("Approximately %d %s script instances created for this processor",
-                        script_engine_q_.size_approx(),
-                        script_engine_);
+  // Use an existing engine, if one is available
+  if (script_engine_q_.try_dequeue(engine)) {
+    logger_->log_debug("Using available %s script engine instance", script_engine_);
+  } else {
+    logger_->log_info("Creating new %s script instance", script_engine_);
+    logger_->log_info("Approximately %d %s script instances created for this processor",
+                      script_engine_q_.size_approx(),
+                      script_engine_);
 
-      if (script_engine_ == "python") {
+    if (script_engine_ == "python") {
 #ifdef PYTHON_SUPPORT

Review comment:
       I know it's old code, but I feel like these ifdefs are unnecessary here. 
   Python being enabled in the build could also be checked in OnSchedule. In case it's not, the processor can simply thrown an exception there, preventing ontrigger to be called ever. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org