提交 51c9ffba 编写于 作者: G groot

fix license check bug


Former-commit-id: 69114c03a3482ab0835e209b7c680d9b2a0f684c
上级 a2d7a407
...@@ -9,25 +9,18 @@ ...@@ -9,25 +9,18 @@
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>
#include <boost/serialization/map.hpp> #include <boost/serialization/map.hpp>
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/operations.hpp>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
namespace zilliz { namespace zilliz {
namespace vecwise { namespace vecwise {
namespace server { namespace server {
using IO_SERVICE = boost::asio::io_service; LicenseCheck::LicenseCheck() {
namespace {
IO_SERVICE& GetIOService() {
static IO_SERVICE io;
return io;
} }
}
// Part 1: Legality check
ServerError ServerError
LicenseCheck::LegalityCheck(const std::string &license_file_path) { LicenseCheck::LegalityCheck(const std::string &license_file_path) {
...@@ -81,14 +74,16 @@ LicenseCheck::AlterFile(const std::string &license_file_path, ...@@ -81,14 +74,16 @@ LicenseCheck::AlterFile(const std::string &license_file_path,
const boost::system::error_code &ec, const boost::system::error_code &ec,
boost::asio::deadline_timer *pt) { boost::asio::deadline_timer *pt) {
ServerError err = LegalityCheck(license_file_path); ServerError err = LicenseCheck::LegalityCheck(license_file_path);
if(err!=SERVER_SUCCESS) if(err!=SERVER_SUCCESS) {
{ printf("license file check error\n");
exit(1); exit(1);
} }
printf("---runing---\n"); printf("---runing---\n");
pt->expires_at(pt->expires_at() + boost::posix_time::hours(1)); pt->expires_at(pt->expires_at() + boost::posix_time::hours(1));
pt->async_wait(boost::bind(AlterFile, license_file_path, boost::asio::placeholders::error, pt)); pt->async_wait(boost::bind(LicenseCheck::AlterFile, license_file_path, boost::asio::placeholders::error, pt));
return SERVER_SUCCESS; return SERVER_SUCCESS;
} }
...@@ -102,22 +97,26 @@ LicenseCheck::StartCountingDown(const std::string &license_file_path) { ...@@ -102,22 +97,26 @@ LicenseCheck::StartCountingDown(const std::string &license_file_path) {
} }
//create a thread to run AlterFile //create a thread to run AlterFile
std::thread io_thread([&]() { if(counting_thread_ == nullptr) {
boost::asio::io_service& io = GetIOService(); counting_thread_ = std::make_shared<std::thread>([&]() {
boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); boost::asio::deadline_timer t(io_service_, boost::posix_time::hours(1));
t.async_wait(boost::bind(AlterFile, license_file_path, boost::asio::placeholders::error, &t)); t.async_wait(boost::bind(LicenseCheck::AlterFile, license_file_path, boost::asio::placeholders::error, &t));
io.run();//this thread will block here io_service_.run();//this thread will block here
}); });
io_thread.detach(); }
return SERVER_SUCCESS; return SERVER_SUCCESS;
} }
ServerError ServerError
LicenseCheck::StopCountingDown() { LicenseCheck::StopCountingDown() {
boost::asio::io_service& io = GetIOService(); if(!io_service_.stopped()) {
if(!io.stopped()) { io_service_.stop();
io.stop(); }
if(counting_thread_ != nullptr) {
counting_thread_->join();
counting_thread_ = nullptr;
} }
return SERVER_SUCCESS; return SERVER_SUCCESS;
......
...@@ -4,36 +4,43 @@ ...@@ -4,36 +4,43 @@
#include "LicenseLibrary.h" #include "LicenseLibrary.h"
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <thread>
#include <memory>
namespace zilliz { namespace zilliz {
namespace vecwise { namespace vecwise {
namespace server { namespace server {
class LicenseCheck { class LicenseCheck {
public: private:
// Part 1: Legality check LicenseCheck();
static ServerError
LegalityCheck(const std::string &license_file_path);
public:
static LicenseCheck &
GetInstance() {
static LicenseCheck instance;
return instance;
};
// Part 2: Timing check license
static ServerError static ServerError
AlterFile(const std::string &license_file_path, LegalityCheck(const std::string &license_file_path);
const boost::system::error_code &ec,
boost::asio::deadline_timer *pt);
static ServerError ServerError
StartCountingDown(const std::string &license_file_path); StartCountingDown(const std::string &license_file_path);
static ServerError ServerError
StopCountingDown(); StopCountingDown();
private: private:
static ServerError
AlterFile(const std::string &license_file_path,
const boost::system::error_code &ec,
boost::asio::deadline_timer *pt);
private:
boost::asio::io_service io_service_;
std::shared_ptr<std::thread> counting_thread_;
}; };
......
...@@ -159,15 +159,13 @@ Server::Start() { ...@@ -159,15 +159,13 @@ Server::Start() {
ConfigNode license_config = config.GetConfig(CONFIG_LICENSE); ConfigNode license_config = config.GetConfig(CONFIG_LICENSE);
std::string license_file_path = license_config.GetValue(CONFIG_LICENSE_PATH); std::string license_file_path = license_config.GetValue(CONFIG_LICENSE_PATH);
SERVER_LOG_INFO << "License path: " << license_file_path; SERVER_LOG_INFO << "License path: " << license_file_path;
if(server::LicenseCheck::LegalityCheck(license_file_path) != SERVER_SUCCESS) { if(server::LicenseCheck::LegalityCheck(license_file_path) != SERVER_SUCCESS) {
SERVER_LOG_ERROR << "License check failed"; SERVER_LOG_ERROR << "License check failed";
exit(1); exit(1);
} }
if(server::LicenseCheck::StartCountingDown(license_file_path) != SERVER_SUCCESS) { server::LicenseCheck::GetInstance().StartCountingDown(license_file_path);
SERVER_LOG_ERROR << "License counter start error";
exit(1);
}
#endif #endif
// Handle Signal // Handle Signal
...@@ -218,7 +216,7 @@ Server::Stop() { ...@@ -218,7 +216,7 @@ Server::Stop() {
StopService(); StopService();
#ifdef ENABLE_LICENSE #ifdef ENABLE_LICENSE
server::LicenseCheck::StopCountingDown(); server::LicenseCheck::GetInstance().StopCountingDown();
#endif #endif
SERVER_LOG_INFO << "Vecwise server closed"; SERVER_LOG_INFO << "Vecwise server closed";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册