Skip to content

Commit

Permalink
The service is not restarted if there is an error in the configuration (
Browse files Browse the repository at this point in the history
#16)

* The service is not restarted if there is an error in the configuration
* Added TConfigException to config parser
  • Loading branch information
Roman Kochkin authored Oct 7, 2022
1 parent 5fbc705 commit 9b1a04e
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ wb-mqtt-mbgate

# Visual Studio Code
.vscode

# CLion
.idea
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> 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
Expand Down
3 changes: 3 additions & 0 deletions debian/wb-mqtt-mbgate.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 10 additions & 5 deletions src/config_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -80,7 +85,7 @@ tuple<PModbusServer, PMqttClient> TJSONConfigParser::Build()

modbusBackend = make_shared<TModbusRTUBackend>(args);
} else {
throw runtime_error("invalid modbus type: '" + type + "'");
throw TConfigException("invalid modbus type: '" + type + "'");
}
}

Expand Down Expand Up @@ -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<TMQTTIntConverter>(int_type, scale, size, byteswap, wordswap);
Expand All @@ -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);
}
}
}
15 changes: 10 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "modbus_lmb_backend.h"
#include "observer.h"
#include "config_parser.h"
#include "mbgate_exception.h"

#include <wblib/signal_handling.h>

Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions src/mbgate_exception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "mbgate_exception.h"

TConfigException::TConfigException(const std::string &message)
: std::runtime_error("Configuration error: " + message) {}
9 changes: 9 additions & 0 deletions src/mbgate_exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <stdexcept>
#include <string>

class TConfigException : public std::runtime_error {
public:
explicit TConfigException(const std::string &message);
};

0 comments on commit 9b1a04e

Please sign in to comment.