aboutsummaryrefslogtreecommitdiffstats
path: root/include/http_server.h
diff options
context:
space:
mode:
authoripknHama <ipknhama@gmail.com>2016-08-29 21:13:24 +0900
committeripknHama <ipknhama@gmail.com>2016-09-10 01:22:06 +0900
commit079df6efc5879091067d68dcbf7b8794f223d024 (patch)
treea8a4868ec580a11c0fce9551bc90437db46233df /include/http_server.h
parent1a4a51bf7662c860544c5049c6cc18511f991253 (diff)
downloadcrow-079df6efc5879091067d68dcbf7b8794f223d024.tar.gz
crow-079df6efc5879091067d68dcbf7b8794f223d024.zip
Add a periodic handler feature
Diffstat (limited to 'include/http_server.h')
-rw-r--r--include/http_server.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/include/http_server.h b/include/http_server.h
index 86943ee..49c4ef0 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -1,5 +1,6 @@
#pragma once
+#include <chrono>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/asio.hpp>
#ifdef CROW_ENABLE_SSL
@@ -28,6 +29,7 @@ namespace crow
Server(Handler* handler, std::string bindaddr, uint16_t port, std::tuple<Middlewares...>* middlewares = nullptr, uint16_t concurrency = 1, typename Adaptor::context* adaptor_ctx = nullptr)
: acceptor_(io_service_, tcp::endpoint(boost::asio::ip::address::from_string(bindaddr), port)),
signals_(io_service_, SIGINT, SIGTERM),
+ tick_timer_(io_service_),
handler_(handler),
concurrency_(concurrency),
port_(port),
@@ -37,6 +39,24 @@ namespace crow
{
}
+ void set_tick_function(std::chrono::milliseconds d, std::function<void()> f)
+ {
+ tick_interval_ = d;
+ tick_function_ = f;
+ }
+
+ void on_tick()
+ {
+ tick_function_();
+ tick_timer_.expires_from_now(boost::posix_time::milliseconds(tick_interval_.count()));
+ tick_timer_.async_wait([this](const boost::system::error_code& ec)
+ {
+ if (ec)
+ return;
+ on_tick();
+ });
+ }
+
void run()
{
if (concurrency_ < 0)
@@ -109,6 +129,18 @@ namespace crow
CROW_LOG_ERROR << "Worker Crash: An uncaught exception occurred: " << e.what();
}
}));
+
+ if (tick_function_ && tick_interval_.count() > 0)
+ {
+ tick_timer_.expires_from_now(boost::posix_time::milliseconds(tick_interval_.count()));
+ tick_timer_.async_wait([this](const boost::system::error_code& ec)
+ {
+ if (ec)
+ return;
+ on_tick();
+ });
+ }
+
CROW_LOG_INFO << server_name_ << " server is running, local port " << port_;
signals_.async_wait(
@@ -172,6 +204,7 @@ namespace crow
std::vector<std::function<std::string()>> get_cached_date_str_pool_;
tcp::acceptor acceptor_;
boost::asio::signal_set signals_;
+ boost::asio::deadline_timer tick_timer_;
Handler* handler_;
uint16_t concurrency_{1};
@@ -180,6 +213,9 @@ namespace crow
std::string bindaddr_;
unsigned int roundrobin_index_{};
+ std::chrono::milliseconds tick_interval_;
+ std::function<void()> tick_function_;
+
std::tuple<Middlewares...>* middlewares_;
#ifdef CROW_ENABLE_SSL