Files
spark/src/lib/plugin/plugin.cpp
T

71 lines
2.1 KiB
C++

#include "lib/config/config.h"
#include "lib/config/protos/system.pb.h"
#include "lib/logging/logging.h"
#include "lib/orderman/OrderBuilder.h"
#include "lib/plugin/SparkServices.h"
#include "lib/utils/PBUtils.h"
#include <algorithm>
#include <boost/dll.hpp>
#include <boost/filesystem.hpp>
extern "C" void init_spark_plugin(bts::SparkServices &serv);
namespace bts {
namespace plugin {
using namespace bts::orderman;
void init_plugins(SparkServices &services) {
static std::vector<decltype(boost::dll::import_symbol<void(SparkServices &)>(
boost::filesystem::path(), ""))>
keep_alive;
auto plugin_path = os::sparkd_wd() + "/plugins/";
if (!boost::filesystem::is_directory(plugin_path)) {
LOG(Plugin, Info,
"No plugin directory found at {}. Skipping plugin loading",
plugin_path);
return;
}
// initialize the plugins in libspark.so
init_spark_plugin(services);
boost::filesystem::directory_iterator end;
for (auto iter = boost::filesystem::directory_iterator(plugin_path);
iter != end; ++iter) {
auto &f = *iter;
auto pl = f.path().filename().string();
if (pl == "order_plugin_example.so" &&
config::get<config::DeploymentType>().type() != "dev") {
LOG(Plugin, Info,
"Skipping order_plugin_example.so because we are not running in "
"dev mode",
plugin_path);
continue;
}
if (pl == "libspark.so")
continue;
LOG(Plugin, Info, "Attempting to initialize plugin {}", pl);
try {
auto init_plugin = boost::dll::import_symbol<void(SparkServices &)>(
f.path(), "init_spark_plugin");
init_plugin(services);
keep_alive.push_back(init_plugin);
LOG(Plugin, Info, "Successfully initialized plugin {}", pl);
} catch (boost::system::system_error const &e) {
LOG(Plugin, Error,
"Failed to initialize plugin {}. Error: {} (OS error code: {})",
pl, std::string(e.what()), e.code());
}
}
}
} // namespace plugin
} // namespace bts