You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by lo...@apache.org on 2022/12/16 13:13:50 UTC

[nifi-minifi-cpp] 03/03: MINIFICPP-2014 Add ProcessSession::remove to Lua API

This is an automated email from the ASF dual-hosted git repository.

lordgamez pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git

commit 5378719e57cd2d1d5f12b25339e0b5cd4930dcac
Author: Martin Zink <ma...@apache.org>
AuthorDate: Tue Dec 13 12:25:03 2022 +0100

    MINIFICPP-2014 Add ProcessSession::remove to Lua API
    
    Signed-off-by: Gabor Gyimesi <ga...@gmail.com>
    
    This closes #1472
---
 extensions/script/lua/LuaProcessSession.cpp         | 14 ++++++++++++++
 extensions/script/lua/LuaProcessSession.h           |  1 +
 extensions/script/lua/LuaScriptEngine.cpp           |  3 ++-
 .../TestExecuteScriptProcessorWithLuaScript.cpp     | 21 +++++++++++++++++++++
 .../TestExecuteScriptProcessorWithPythonScript.cpp  | 20 ++++++++++++++++++++
 5 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/extensions/script/lua/LuaProcessSession.cpp b/extensions/script/lua/LuaProcessSession.cpp
index 44a683711..610aea69e 100644
--- a/extensions/script/lua/LuaProcessSession.cpp
+++ b/extensions/script/lua/LuaProcessSession.cpp
@@ -131,4 +131,18 @@ void LuaProcessSession::releaseCoreResources() {
   session_.reset();
 }
 
+void LuaProcessSession::remove(const std::shared_ptr<script::ScriptFlowFile>& script_flow_file) {
+  if (!session_) {
+    throw std::runtime_error("Access of ProcessSession after it has been released");
+  }
+
+  auto flow_file = script_flow_file->getFlowFile();
+
+  if (!flow_file) {
+    throw std::runtime_error("Access of FlowFile after it has been released");
+  }
+
+  session_->remove(flow_file);
+}
+
 }  // namespace org::apache::nifi::minifi::lua
diff --git a/extensions/script/lua/LuaProcessSession.h b/extensions/script/lua/LuaProcessSession.h
index c428239fb..a05b9e6cf 100644
--- a/extensions/script/lua/LuaProcessSession.h
+++ b/extensions/script/lua/LuaProcessSession.h
@@ -39,6 +39,7 @@ class LuaProcessSession {
   void transfer(const std::shared_ptr<script::ScriptFlowFile> &flow_file, const core::Relationship& relationship);
   void read(const std::shared_ptr<script::ScriptFlowFile> &script_flow_file, sol::table input_stream_callback);
   void write(const std::shared_ptr<script::ScriptFlowFile> &flow_file, sol::table output_stream_callback);
+  void remove(const std::shared_ptr<script::ScriptFlowFile>& flow_file);
 
   /**
    * Sometimes we want to release shared pointers to core resources when
diff --git a/extensions/script/lua/LuaScriptEngine.cpp b/extensions/script/lua/LuaScriptEngine.cpp
index bce738dfd..11b235b2b 100644
--- a/extensions/script/lua/LuaScriptEngine.cpp
+++ b/extensions/script/lua/LuaScriptEngine.cpp
@@ -46,7 +46,8 @@ LuaScriptEngine::LuaScriptEngine() {
       "get", &lua::LuaProcessSession::get,
       "read", &lua::LuaProcessSession::read,
       "write", &lua::LuaProcessSession::write,
-      "transfer", &lua::LuaProcessSession::transfer);
+      "transfer", &lua::LuaProcessSession::transfer,
+      "remove", &lua::LuaProcessSession::remove);
   lua_.new_usertype<script::ScriptFlowFile>(
       "FlowFile",
       "getAttribute", &script::ScriptFlowFile::getAttribute,
diff --git a/extensions/script/tests/TestExecuteScriptProcessorWithLuaScript.cpp b/extensions/script/tests/TestExecuteScriptProcessorWithLuaScript.cpp
index 5fbc5195a..71d8d3a1d 100644
--- a/extensions/script/tests/TestExecuteScriptProcessorWithLuaScript.cpp
+++ b/extensions/script/tests/TestExecuteScriptProcessorWithLuaScript.cpp
@@ -21,6 +21,7 @@
 #include <string>
 #include <set>
 
+#include "SingleProcessorTestController.h"
 #include "TestBase.h"
 #include "Catch.h"
 
@@ -31,6 +32,8 @@
 #include "utils/file/FileUtils.h"
 #include "utils/TestUtils.h"
 
+namespace org::apache::nifi::minifi::processors::test {
+
 TEST_CASE("Script engine is not set", "[executescriptMisconfiguration]") {
   TestController testController;
   auto plan = testController.createPlan();
@@ -454,3 +457,21 @@ TEST_CASE("Lua: Non existent script file should throw", "[executescriptLuaNonExi
 
   logTestController.reset();
 }
+
+TEST_CASE("Lua can remove flowfiles", "[ExecuteScript]") {
+  const auto execute_script = std::make_shared<ExecuteScript>("ExecuteScript");
+
+  minifi::test::SingleProcessorTestController controller{execute_script};
+  LogTestController::getInstance().setTrace<minifi::processors::ExecuteScript>();
+  execute_script->setProperty(ExecuteScript::ScriptEngine, "lua");
+  execute_script->setProperty(ExecuteScript::ScriptBody.getName(),
+      R"(
+        function onTrigger(context, session)
+          flow_file = session:get()
+          session:remove(flow_file)
+        end
+      )");
+  REQUIRE_NOTHROW(controller.trigger("hello"));
+}
+
+}  // namespace org::apache::nifi::minifi::processors::test
diff --git a/extensions/script/tests/TestExecuteScriptProcessorWithPythonScript.cpp b/extensions/script/tests/TestExecuteScriptProcessorWithPythonScript.cpp
index b03dd0844..a1d803765 100644
--- a/extensions/script/tests/TestExecuteScriptProcessorWithPythonScript.cpp
+++ b/extensions/script/tests/TestExecuteScriptProcessorWithPythonScript.cpp
@@ -20,6 +20,7 @@
 #include <string>
 #include <set>
 
+#include "SingleProcessorTestController.h"
 #include "TestBase.h"
 #include "Catch.h"
 
@@ -31,6 +32,8 @@
 #include "utils/file/PathUtils.h"
 #include "utils/TestUtils.h"
 
+namespace org::apache::nifi::minifi::processors::test {
+
 TEST_CASE("Script engine is not set", "[executescriptMisconfiguration]") {
   TestController test_controller;
   auto plan = test_controller.createPlan();
@@ -407,3 +410,20 @@ TEST_CASE("Python: Non existent script file should throw", "[executescriptPython
 
   log_test_controller.reset();
 }
+
+TEST_CASE("Python can remove flowfiles", "[ExecuteScript]") {
+  const auto execute_script = std::make_shared<ExecuteScript>("ExecuteScript");
+
+  minifi::test::SingleProcessorTestController controller{execute_script};
+  LogTestController::getInstance().setTrace<minifi::processors::ExecuteScript>();
+  execute_script->setProperty(ExecuteScript::ScriptEngine, "python");
+  execute_script->setProperty(ExecuteScript::ScriptBody.getName(),
+      R"(
+        def onTrigger(context, session):
+          flow_file = session.get()
+          session.remove(flow_file);
+      )");
+  REQUIRE_NOTHROW(controller.trigger("hello"));
+}
+
+}  // namespace org::apache::nifi::minifi::processors::test