From 9b1a04e08075aef24b03b4c9920206c88738113c Mon Sep 17 00:00:00 2001 From: Roman Kochkin Date: Fri, 7 Oct 2022 15:52:18 +0400 Subject: [PATCH] The service is not restarted if there is an error in the configuration (#16) * The service is not restarted if there is an error in the configuration * Added TConfigException to config parser --- .gitignore | 3 +++ Makefile | 2 +- debian/changelog | 6 ++++++ debian/wb-mqtt-mbgate.service | 3 +++ src/config_parser.cpp | 15 ++++++++++----- src/main.cpp | 15 ++++++++++----- src/mbgate_exception.cpp | 4 ++++ src/mbgate_exception.h | 9 +++++++++ 8 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 src/mbgate_exception.cpp create mode 100644 src/mbgate_exception.h diff --git a/.gitignore b/.gitignore index b9aa3a9..318886a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ wb-mqtt-mbgate # Visual Studio Code .vscode + +# CLion +.idea diff --git a/Makefile b/Makefile index fb9381e..6fe1c18 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ ifeq ($(origin CXX),default) endif TARGET = wb-mqtt-mbgate -OBJS = main.o config_parser.o +OBJS = main.o config_parser.o mbgate_exception.o SRC_DIR = src COMMON_OBJS = i_modbus_server_observer.o i_modbus_backend.o modbus_wrapper.o mqtt_converters.o observer.o modbus_lmb_backend.o log.o diff --git a/debian/changelog b/debian/changelog index 99825f5..fbb395b 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +wb-mqtt-mbgate (1.4.0) stable; urgency=medium + + * The service is not restarted if there is an error in the configuration + + -- Roman Kochkin Thu, 06 Oct 2022 16:08:23 +0400 + wb-mqtt-mbgate (1.3.1) stable; urgency=medium * Fix default port setting in bullseye for UNIX socket connection diff --git a/debian/wb-mqtt-mbgate.service b/debian/wb-mqtt-mbgate.service index bcc25a3..8b6ffb7 100644 --- a/debian/wb-mqtt-mbgate.service +++ b/debian/wb-mqtt-mbgate.service @@ -11,5 +11,8 @@ User=root ExecStart=/usr/bin/wb-mqtt-mbgate -c /etc/wb-mqtt-mbgate.conf ExecStartPre=/usr/bin/wb-mqtt-mbgate-confgen -c /etc/wb-mqtt-mbgate.conf +# Service not restarting after return EXIT_NOTCONFIGURED +RestartPreventExitStatus=6 + [Install] WantedBy=multi-user.target \ No newline at end of file diff --git a/src/config_parser.cpp b/src/config_parser.cpp index 3f8b1dd..97928a9 100644 --- a/src/config_parser.cpp +++ b/src/config_parser.cpp @@ -15,6 +15,7 @@ #include "mqtt_converters.h" #include "modbus_lmb_backend.h" #include "observer.h" +#include "mbgate_exception.h" using namespace std; using namespace WBMQTT; @@ -37,8 +38,12 @@ IConfigParser::~IConfigParser() TJSONConfigParser::TJSONConfigParser(const string& config_file, const string& schema_file) : Root(Parse(config_file)) { - auto schema(Parse(schema_file)); - Validate(Root, schema); + try { + auto schema(Parse(schema_file)); + Validate(Root, schema); + } catch (const std::runtime_error& e){ + throw TConfigException(e.what()); + } } bool TJSONConfigParser::Debug() @@ -80,7 +85,7 @@ tuple TJSONConfigParser::Build() modbusBackend = make_shared(args); } else { - throw runtime_error("invalid modbus type: '" + type + "'"); + throw TConfigException("invalid modbus type: '" + type + "'"); } } @@ -154,7 +159,7 @@ void TJSONConfigParser::_BuildStore(TStoreType type, const Json::Value& list, PM } else if (format == "bcd") { int_type = TMQTTIntConverter::BCD; } else { - throw runtime_error("Unknown integer format: " + format); + throw TConfigException("Unknown integer format: " + format); } conv = make_shared(int_type, scale, size, byteswap, wordswap); @@ -169,7 +174,7 @@ void TJSONConfigParser::_BuildStore(TStoreType type, const Json::Value& list, PM try { modbus->Observe(obs, type, TModbusAddressRange(address, size), slave_id); } catch (const WrongSegmentException &e) { - throw runtime_error(string("Address overlapping: ") + StoreTypeToString(type) + ": topic " + topic); + throw TConfigException(string("Address overlapping: ") + StoreTypeToString(type) + ": topic " + topic); } } } diff --git a/src/main.cpp b/src/main.cpp index b66b67f..8bf7bbc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include "modbus_lmb_backend.h" #include "observer.h" #include "config_parser.h" +#include "mbgate_exception.h" #include @@ -30,8 +31,9 @@ using namespace WBMQTT; const auto DRIVER_STOP_TIMEOUT_S = chrono::seconds(10); -namespace -{ +namespace { + constexpr auto EXIT_NOTCONFIGURED = 6; // The program is not configured + void PrintUsage() { cout << WBMQTT_NAME << XSTR(WBMQTT_VERSION) @@ -175,10 +177,13 @@ int main(int argc, char *argv[]) t->Stop(); WBMQTT::SignalHandling::Wait(); - } catch (const exception& e) { + } catch (const TConfigException &e) { + LOG(Error) << "FATAL: " << e.what(); + return EXIT_NOTCONFIGURED; + } catch (const exception &e) { LOG(Error) << "FATAL: " << e.what(); - return 1; + return EXIT_FAILURE; } - return 0; + return EXIT_SUCCESS; } diff --git a/src/mbgate_exception.cpp b/src/mbgate_exception.cpp new file mode 100644 index 0000000..66681f5 --- /dev/null +++ b/src/mbgate_exception.cpp @@ -0,0 +1,4 @@ +#include "mbgate_exception.h" + +TConfigException::TConfigException(const std::string &message) + : std::runtime_error("Configuration error: " + message) {} \ No newline at end of file diff --git a/src/mbgate_exception.h b/src/mbgate_exception.h new file mode 100644 index 0000000..f40aa9d --- /dev/null +++ b/src/mbgate_exception.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +class TConfigException : public std::runtime_error { +public: + explicit TConfigException(const std::string &message); +}; \ No newline at end of file