diff options
author | ipknHama <ipknhama@gmail.com> | 2016-08-29 21:13:24 +0900 |
---|---|---|
committer | ipknHama <ipknhama@gmail.com> | 2016-09-10 01:22:06 +0900 |
commit | 079df6efc5879091067d68dcbf7b8794f223d024 (patch) | |
tree | a8a4868ec580a11c0fce9551bc90437db46233df | |
parent | 1a4a51bf7662c860544c5049c6cc18511f991253 (diff) | |
download | crow-079df6efc5879091067d68dcbf7b8794f223d024.tar.gz crow-079df6efc5879091067d68dcbf7b8794f223d024.zip |
Add a periodic handler feature
-rw-r--r-- | include/crow.h | 13 | ||||
-rw-r--r-- | include/http_server.h | 36 |
2 files changed, 49 insertions, 0 deletions
diff --git a/include/crow.h b/include/crow.h index 00209c7..0ec8a57 100644 --- a/include/crow.h +++ b/include/crow.h @@ -1,5 +1,6 @@ #pragma once +#include <chrono> #include <string> #include <functional> #include <memory> @@ -101,12 +102,14 @@ namespace crow if (use_ssl_) { ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, bindaddr_, port_, &middlewares_, concurrency_, &ssl_context_))); + ssl_server_->set_tick_function(tick_interval_, tick_function_); ssl_server_->run(); } else #endif { server_ = std::move(std::unique_ptr<server_t>(new server_t(this, bindaddr_, port_, &middlewares_, concurrency_, nullptr))); + server_->set_tick_function(tick_interval_, tick_function_); server_->run(); } } @@ -210,12 +213,22 @@ namespace crow return utility::get_element_by_type<T, Middlewares...>(middlewares_); } + template <typename Duration, typename Func> + self_t& tick(Duration d, Func f) { + tick_interval_ = std::chrono::duration_cast<std::chrono::milliseconds>(d); + tick_function_ = f; + return *this; + } + private: uint16_t port_ = 80; uint16_t concurrency_ = 1; std::string bindaddr_ = "0.0.0.0"; Router router_; + std::chrono::milliseconds tick_interval_; + std::function<void()> tick_function_; + std::tuple<Middlewares...> middlewares_; #ifdef CROW_ENABLE_SSL 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 |