fix: Don't parse non-existent json files in SparkDataDirectory

Bugs (fixed):
  * Sparkd assert crashes if binance_usds_futures_execution.json file
    doesn't exist
This commit is contained in:
John Colvin committed 2025-01-03 16:59:07 +00:00
1 parent bb6e5dd548
commit 002bdff9a3
3 files changed
+37 -16

No files matched your search

+1 -1
View File
@@ -61,7 +61,7 @@ boost::filesystem::path SparkDataDirectory::database_file(
return root_ / "databases" / dir_name / (db_name + ".sqlite");
}
nlohmann::json SparkDataDirectory::load_config_json(
std::optional<nlohmann::json> SparkDataDirectory::load_config_json(
const std::string &name) const {
const auto json_file = config_json_file(name);
std::cout << "LOAD config json " << name << " from file "
+14 -5
View File
@@ -2,6 +2,7 @@
#include <boost/filesystem/path.hpp>
#include <nlohmann/json.hpp>
#include <optional>
#include <string>
namespace bts::spark {
@@ -24,10 +25,11 @@ class SparkDataDirectory {
boost::filesystem::path database_file(
const std::string &dir_name, const std::string &db_name) const;
nlohmann::json load_config_json(const std::string &name) const;
std::optional<nlohmann::json> load_config_json(
const std::string &name) const;
template <class T>
T load_config_json(const std::string &name) const;
std::optional<T> load_config_json(const std::string &name) const;
template <class T>
bool load_config_json(const std::string &name, T &result) const;
@@ -41,16 +43,23 @@ class SparkDataDirectory {
//////////////////////////////////////////////////
template <class T>
T SparkDataDirectory::load_config_json(const std::string &name) const {
std::optional<T> SparkDataDirectory::load_config_json(
const std::string &name) const {
auto j = load_config_json(name);
return j.get<T>();
if (j) {
return j->get<T>();
}
return std::nullopt;
}
template <class T>
bool SparkDataDirectory::load_config_json(
const std::string &name, T &result) const {
auto j = load_config_json(name);
j.get_to(result);
if (!j) {
return false;
}
j->get_to(result);
return true;
}
@@ -1,5 +1,6 @@
#include "execution_gateway_provider.h"
#include "binance_execution/usds_futures/execution_gateway.h"
#include "binance_execution/usds_futures/execution_gateway_config.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/md/SecurityOracle.h"
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
@@ -8,11 +9,31 @@
namespace binance_execution {
using bts::orderman::OrderGatewayDefinition;
using bts::spark::SparkDataDirectory;
using std::vector;
struct ExecutionGatewayProvider::PImpl {
PImpl(bts::md::SecurityOracle sec_oracle)
: sec_oracle(std::move(sec_oracle)) {}
bts::md::SecurityOracle sec_oracle;
bool load_usds_futures(vector<OrderGatewayDefinition> &dest) {
SparkDataDirectory data_dir;
auto cfg =
data_dir.load_config_json<usds_futures::ExecutionGatewayConfig>(
"binance_usds_futures_execution");
if (!cfg) {
return false;
}
auto &gw = dest.emplace_back();
*gw.connection.mutable_name() = "binance_usds_futures_1";
gw.connection.set_venue(bts::api::VENUE_BINANCE_USDS_FUTURES);
gw.gateway = std::make_shared<usds_futures::ExecutionGateway>(
sec_oracle, std::move(*cfg), gw.connection.name());
return true;
}
};
ExecutionGatewayProvider::ExecutionGatewayProvider(
@@ -24,16 +45,7 @@ ExecutionGatewayProvider::~ExecutionGatewayProvider() = default;
std::vector<bts::orderman::OrderGatewayDefinition>
ExecutionGatewayProvider::create() {
std::vector<bts::orderman::OrderGatewayDefinition> gws;
bts::spark::SparkDataDirectory data_dir;
usds_futures::ExecutionGatewayConfig usds_futures_cfg;
if (data_dir.load_config_json(
"binance_usds_futures_execution", usds_futures_cfg)) {
auto &binance_gw = gws.emplace_back();
*binance_gw.connection.mutable_name() = "binance_usds_futures_1";
binance_gw.connection.set_venue(bts::api::VENUE_BINANCE_USDS_FUTURES);
binance_gw.gateway = std::make_shared<usds_futures::ExecutionGateway>(
me_->sec_oracle, usds_futures_cfg, binance_gw.connection.name());
}
me_->load_usds_futures(gws);
return gws;
}