This is an automated email from the ASF dual-hosted git repository. martinzink pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git
commit 71f5f8bfb366ed32d371e4b643859887f56406bf Author: Ferenc Gerlits <fgerl...@gmail.com> AuthorDate: Tue Feb 7 11:16:07 2023 +0100 MINIFICPP-2020 Improve error messages when MiNiFi is not able to start Closes #1498 Signed-off-by: Martin Zink <martinz...@apache.org> --- minifi_main/MiNiFiMain.cpp | 16 +++++++++++----- minifi_main/MiNiFiWindowsService.cpp | 17 ++++++----------- minifi_main/MiNiFiWindowsService.h | 8 +++++++- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/minifi_main/MiNiFiMain.cpp b/minifi_main/MiNiFiMain.cpp index 0d5e888a9..f0d4fe769 100644 --- a/minifi_main/MiNiFiMain.cpp +++ b/minifi_main/MiNiFiMain.cpp @@ -133,9 +133,9 @@ int main(int argc, char **argv) { #ifdef WIN32 RunAsServiceIfNeeded(); - bool isStartedByService = false; - HANDLE terminationEventHandler = GetTerminationEventHandle(&isStartedByService); - if (terminationEventHandler == nullptr) { + auto [isStartedByService, terminationEventHandler] = GetTerminationEventHandle(); + if (isStartedByService && !terminationEventHandler) { + std::cerr << "Fatal error: started by service, but could not create the termination event handler\n"; return -1; } @@ -150,9 +150,10 @@ int main(int argc, char **argv) { #ifdef WIN32 if (isStartedByService) { if (!CreateServiceTerminationThread(logger, terminationEventHandler)) { + logger->log_error("Fatal error: started by service, but could not create the service termination thread"); return -1; } - } else { + } else if (terminationEventHandler) { CloseHandle(terminationEventHandler); } #endif @@ -258,7 +259,12 @@ int main(int argc, char **argv) { minifi::core::extension::ExtensionManager::get().initialize(configure); - if (argc >= 3 && std::string("docs") == argv[1]) { + if (argc >= 2 && std::string("docs") == argv[1]) { + if (argc < 3 || argc > 4) { + std::cerr << "Usage: <minifiexe> docs <directory where to write individual doc files> [file where to write PROCESSORS.md]\n"; + std::cerr << " If no file name is given for PROCESSORS.md, it will be printed to stdout.\n"; + exit(1); + } if (utils::file::create_dir(argv[2]) != 0) { std::cerr << "Working directory doesn't exist and cannot be created: " << argv[2] << std::endl; exit(1); diff --git a/minifi_main/MiNiFiWindowsService.cpp b/minifi_main/MiNiFiWindowsService.cpp index 42b4b6d39..a37f78a1c 100644 --- a/minifi_main/MiNiFiWindowsService.cpp +++ b/minifi_main/MiNiFiWindowsService.cpp @@ -246,18 +246,13 @@ void RunAsServiceIfNeeded() { ExitProcess(0); } -HANDLE GetTerminationEventHandle(bool* isStartedByService) { - *isStartedByService = true; - HANDLE hEvent = CreateEvent(0, TRUE, FALSE, SERVICE_TERMINATION_EVENT_NAME); - if (!hEvent) { - return nullptr; - } - - if (GetLastError() != ERROR_ALREADY_EXISTS) { - *isStartedByService = false; - } +GetTerminationEventHandleReturnType GetTerminationEventHandle() { + HANDLE hEvent = CreateEvent(nullptr, TRUE, FALSE, SERVICE_TERMINATION_EVENT_NAME); - return hEvent; + return { + .is_started_by_service = (GetLastError() == ERROR_ALREADY_EXISTS), + .termination_event_handler = hEvent + }; } bool CreateServiceTerminationThread(std::shared_ptr<minifi::core::logging::Logger> logger, HANDLE terminationEventHandle) { diff --git a/minifi_main/MiNiFiWindowsService.h b/minifi_main/MiNiFiWindowsService.h index 605d36cb3..8e99d7319 100644 --- a/minifi_main/MiNiFiWindowsService.h +++ b/minifi_main/MiNiFiWindowsService.h @@ -20,10 +20,16 @@ #ifdef WIN32 #include <memory> + #include "core/Core.h" +struct GetTerminationEventHandleReturnType { + bool is_started_by_service; + HANDLE termination_event_handler; +}; + void RunAsServiceIfNeeded(); -HANDLE GetTerminationEventHandle(bool* isStartedByService); +GetTerminationEventHandleReturnType GetTerminationEventHandle(); bool CreateServiceTerminationThread(std::shared_ptr<org::apache::nifi::minifi::core::logging::Logger> logger, HANDLE terminationEventHandle); #endif