refactor: Wrap md run logic in class MarketDataSession
Start in the constructor, gracefully exit in the destructor.
This commit is contained in:
1 parent
fe85c3e648
commit
fd21269df7
5 files changed
+108
-2
No files matched your search
@@ -17,7 +17,6 @@
|
||||
#include "spark/orderman/OrderServiceServer.h"
|
||||
#include "spark/orderman/events/TradeSummarySignalDispatcher.h"
|
||||
#include "spark/orderman/order_factory/DynamicOrderRegistry.h"
|
||||
#include "spark/os/os.h"
|
||||
#include "spark/os/spark_data_directory.h"
|
||||
#include "spark/persistence/PersistenceServiceServer.h"
|
||||
#include "spark/plugin/SparkServices.h"
|
||||
@@ -32,6 +31,7 @@
|
||||
#include "sparkd/execution/execution_gateway_provider.h"
|
||||
#include "sparkd/http/WebServer.h"
|
||||
#include "sparkd/md/MarketDataAuxiliaryServiceServer.h"
|
||||
#include "sparkd/md/market_data_session.h"
|
||||
#include "sparkd/md/recording/MDRecorder.h"
|
||||
#include "sparkd/position/MarginServiceServer.h"
|
||||
#include "sparkd/position/PositionServiceServer.h"
|
||||
@@ -46,6 +46,7 @@
|
||||
namespace bts::cmds {
|
||||
|
||||
using bts::api::ServiceContext;
|
||||
using bts::md::MarketDataSession;
|
||||
using bts::md::MDClient;
|
||||
using bts::md::TradeSummarySignalDispatcher;
|
||||
using bts::metrics::MetricsContainer;
|
||||
@@ -107,7 +108,7 @@ static void execute() {
|
||||
auto py_plugin_man = std::make_shared<python_interop::PluginManager>();
|
||||
// start work loop in background
|
||||
py_plugin_man->start_work_thread();
|
||||
md::cmds::Run::run(py_plugin_man);
|
||||
MarketDataSession md_session{py_plugin_man};
|
||||
|
||||
api::synthetic::SyntheticSecurityService synthetic_service(sc);
|
||||
bts::md::MDClient mdclient(
|
||||
|
||||
@@ -141,5 +141,6 @@ MarketDataGateway::MarketDataGateway(
|
||||
MarketDataGateway::~MarketDataGateway() = default;
|
||||
|
||||
void MarketDataGateway::start() { me_->start(); }
|
||||
void MarketDataGateway::stop() { me_->stop(); }
|
||||
|
||||
} // namespace bts::md
|
||||
@@ -29,6 +29,7 @@ class MarketDataGateway {
|
||||
~MarketDataGateway();
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
#include "market_data_session.h"
|
||||
#include "python_interop/plugin_manager.h"
|
||||
#include "spark/api/md/MarketDataConfig.pb.h"
|
||||
#include "spark/config/config.h"
|
||||
#include "spark/config/protos/market_data.pb.h"
|
||||
#include "spark/core/time_stamp.h"
|
||||
#include "spark/persistence/IDGenerator.h"
|
||||
#include "spark/utils/datetime.h"
|
||||
#include "spark/utils/thread_manager.h"
|
||||
#include "sparkd/cmds/md/cmds/Cleanup.h"
|
||||
#include "sparkd/md/MarketDataGateway.h"
|
||||
|
||||
namespace bts::md {
|
||||
|
||||
using bts::api::md::MarketDataConfig;
|
||||
using bts::cmds::md::cmds::Cleanup;
|
||||
using bts::core::TimeStamp;
|
||||
using bts::persistence::IDGenerator;
|
||||
using bts::threading::configure_current_thread;
|
||||
using std::shared_ptr;
|
||||
using std::thread;
|
||||
using std::this_thread::sleep_for;
|
||||
|
||||
static void boot_sleep() {
|
||||
auto conf = config::get<bts::md::MDBootDelayWindow>();
|
||||
const auto *start = &conf.standard_start();
|
||||
const auto *end = &conf.standard_end();
|
||||
if (datetime::is_local_time_sunday()) {
|
||||
start = &conf.sunday_start();
|
||||
end = &conf.sunday_end();
|
||||
}
|
||||
int hh_start = 0, mm_start = 0, hh_end = 0, mm_end = 0;
|
||||
sscanf(start->c_str(), "%i:%i", &hh_start, &mm_start);
|
||||
sscanf(end->c_str(), "%i:%i", &hh_end, &mm_end);
|
||||
int64_t sleep_secs =
|
||||
datetime::is_local_time_between(hh_start, mm_start, hh_end, mm_end)
|
||||
? conf.delay_seconds()
|
||||
: 2;
|
||||
LOG(md, Info, R"([md_session_boot_sleep.json]{{ "dur": "{}s", "ts": {} }})",
|
||||
sleep_secs, TimeStamp::now_ns());
|
||||
sleep_for(std::chrono::seconds(sleep_secs));
|
||||
}
|
||||
|
||||
struct MarketDataSession::PImpl {
|
||||
volatile bool is_running = true;
|
||||
thread run_thread;
|
||||
|
||||
PImpl(shared_ptr<python_interop::PluginManager> py_worker) {
|
||||
LOG(md, Info, R"([md_session_starting.json]{{ "ts": {} }})",
|
||||
TimeStamp::now_ns());
|
||||
Cleanup::execute();
|
||||
run_thread = thread([&]() {
|
||||
configure_current_thread("MarketDataGateway");
|
||||
LOG(md, Info, R"([md_session_started.json]{{ "ts": {} }})",
|
||||
TimeStamp::now_ns());
|
||||
MarketDataGateway gw{
|
||||
config::get<MarketDataConfig>(), IDGenerator::instance_ptr(),
|
||||
py_worker};
|
||||
gw.start();
|
||||
while (is_running) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
gw.stop();
|
||||
LOG(md, Info, R"([md_session_stopped.json]{{ "ts": {} }})",
|
||||
TimeStamp::now_ns());
|
||||
});
|
||||
boot_sleep();
|
||||
}
|
||||
|
||||
~PImpl() {
|
||||
LOG(md, Info, R"([md_session_stopping.json]{{ "ts": {} }})",
|
||||
TimeStamp::now_ns());
|
||||
is_running = false;
|
||||
run_thread.join();
|
||||
}
|
||||
};
|
||||
|
||||
MarketDataSession::MarketDataSession(
|
||||
shared_ptr<python_interop::PluginManager> py_worker)
|
||||
: me(std::make_unique<PImpl>(std::move(py_worker))) {}
|
||||
|
||||
MarketDataSession::~MarketDataSession() = default;
|
||||
|
||||
} // namespace bts::md
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "python_interop/plugin_manager.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace bts::md {
|
||||
|
||||
class MarketDataSession {
|
||||
public:
|
||||
MarketDataSession(std::shared_ptr<python_interop::PluginManager> py_worker);
|
||||
~MarketDataSession();
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
};
|
||||
|
||||
} // namespace bts::md
|
||||
Reference in new issue
Block a user