proxygen
|
#include <FunctionScheduler.h>
Classes | |
struct | LatencyDistribution |
struct | RepeatFunc |
struct | RunTimeOrder |
Public Types | |
using | IntervalDistributionFunc = Function< std::chrono::milliseconds()> |
using | NextRunTimeFunc = Function< std::chrono::steady_clock::time_point(std::chrono::steady_clock::time_point, std::chrono::steady_clock::time_point)> |
Public Member Functions | |
FunctionScheduler () | |
~FunctionScheduler () | |
void | setSteady (bool steady) |
void | addFunction (Function< void()> &&cb, std::chrono::milliseconds interval, StringPiece nameID=StringPiece(), std::chrono::milliseconds startDelay=std::chrono::milliseconds(0)) |
void | addFunction (Function< void()> &&cb, std::chrono::milliseconds interval, const LatencyDistribution &latencyDistr, StringPiece nameID=StringPiece(), std::chrono::milliseconds startDelay=std::chrono::milliseconds(0)) |
void | addFunctionOnce (Function< void()> &&cb, StringPiece nameID=StringPiece(), std::chrono::milliseconds startDelay=std::chrono::milliseconds(0)) |
void | addFunctionUniformDistribution (Function< void()> &&cb, std::chrono::milliseconds minInterval, std::chrono::milliseconds maxInterval, StringPiece nameID, std::chrono::milliseconds startDelay) |
void | addFunctionConsistentDelay (Function< void()> &&cb, std::chrono::milliseconds interval, StringPiece nameID=StringPiece(), std::chrono::milliseconds startDelay=std::chrono::milliseconds(0)) |
void | addFunctionGenericDistribution (Function< void()> &&cb, IntervalDistributionFunc &&intervalFunc, const std::string &nameID, const std::string &intervalDescr, std::chrono::milliseconds startDelay) |
void | addFunctionGenericNextRunTimeFunctor (Function< void()> &&cb, NextRunTimeFunc &&fn, const std::string &nameID, const std::string &intervalDescr, std::chrono::milliseconds startDelay) |
bool | cancelFunction (StringPiece nameID) |
bool | cancelFunctionAndWait (StringPiece nameID) |
void | cancelAllFunctions () |
void | cancelAllFunctionsAndWait () |
bool | resetFunctionTimer (StringPiece nameID) |
bool | start () |
bool | shutdown () |
void | setThreadName (StringPiece threadName) |
template<typename RepeatFuncNextRunTimeFunc > | |
void | addFunctionToHeapChecked (Function< void()> &&cb, RepeatFuncNextRunTimeFunc &&fn, const std::string &nameID, const std::string &intervalDescr, milliseconds startDelay, bool runOnce) |
Private Types | |
typedef std::vector< std::unique_ptr< RepeatFunc > > | FunctionHeap |
typedef std::unordered_map< StringPiece, RepeatFunc *, Hash > | FunctionMap |
Private Member Functions | |
void | run () |
void | runOneFunction (std::unique_lock< std::mutex > &lock, std::chrono::steady_clock::time_point now) |
void | cancelFunction (const std::unique_lock< std::mutex > &lock, RepeatFunc *it) |
void | addFunctionToHeap (const std::unique_lock< std::mutex > &lock, std::unique_ptr< RepeatFunc > func) |
template<typename RepeatFuncNextRunTimeFunc > | |
void | addFunctionToHeapChecked (Function< void()> &&cb, RepeatFuncNextRunTimeFunc &&fn, const std::string &nameID, const std::string &intervalDescr, std::chrono::milliseconds startDelay, bool runOnce) |
void | addFunctionInternal (Function< void()> &&cb, NextRunTimeFunc &&fn, const std::string &nameID, const std::string &intervalDescr, std::chrono::milliseconds startDelay, bool runOnce) |
void | addFunctionInternal (Function< void()> &&cb, IntervalDistributionFunc &&fn, const std::string &nameID, const std::string &intervalDescr, std::chrono::milliseconds startDelay, bool runOnce) |
bool | cancelAllFunctionsWithLock (std::unique_lock< std::mutex > &lock) |
bool | cancelFunctionWithLock (std::unique_lock< std::mutex > &lock, StringPiece nameID) |
Private Attributes | |
std::thread | thread_ |
std::mutex | mutex_ |
bool | running_ {false} |
FunctionHeap | functions_ |
FunctionMap | functionsMap_ |
RunTimeOrder | fnCmp_ |
RepeatFunc * | currentFunction_ {nullptr} |
std::condition_variable | runningCondvar_ |
std::string | threadName_ |
bool | steady_ {false} |
bool | cancellingCurrentFunction_ {false} |
Schedules any number of functions to run at various intervals. E.g.,
fs.addFunction([&] { LOG(INFO) << "tick..."; }, seconds(1), "ticker"); fs.addFunction(std::bind(&TestClass::doStuff, this), minutes(5), "stuff"); fs.start(); ........ fs.cancelFunction("ticker"); fs.addFunction([&] { LOG(INFO) << "tock..."; }, minutes(3), "tocker"); ........ fs.shutdown();
Note: the class uses only one thread - if you want to use more than one thread, either use multiple FunctionScheduler objects, or check out ThreadedRepeatingFunctionRunner.h for a much simpler contract of "run each function periodically in its own thread".
start() schedules the functions, while shutdown() terminates further scheduling.
Definition at line 54 of file FunctionScheduler.h.
|
private |
Definition at line 314 of file FunctionScheduler.h.
|
private |
Definition at line 315 of file FunctionScheduler.h.
using folly::FunctionScheduler::IntervalDistributionFunc = Function<std::chrono::milliseconds()> |
A type alias for function that is called to determine the time interval for the next scheduled run.
Definition at line 154 of file FunctionScheduler.h.
using folly::FunctionScheduler::NextRunTimeFunc = Function<std::chrono::steady_clock::time_point( std::chrono::steady_clock::time_point, std::chrono::steady_clock::time_point)> |
A type alias for function that returns the next run time, given the current run time and the current start time.
Definition at line 161 of file FunctionScheduler.h.
folly::FunctionScheduler::FunctionScheduler | ( | ) |
Definition at line 114 of file FunctionScheduler.cpp.
folly::FunctionScheduler::~FunctionScheduler | ( | ) |
Definition at line 116 of file FunctionScheduler.cpp.
References addFunction(), folly::FunctionScheduler::LatencyDistribution::isPoisson, folly::gen::move, folly::FunctionScheduler::LatencyDistribution::poissonMean, folly::shutdown(), and folly::Range< Iter >::str().
void folly::FunctionScheduler::addFunction | ( | Function< void()> && | cb, |
std::chrono::milliseconds | interval, | ||
StringPiece | nameID = StringPiece() , |
||
std::chrono::milliseconds | startDelay = std::chrono::milliseconds(0) |
||
) |
Adds a new function to the FunctionScheduler.
Functions will not be run until start() is called. When start() is called, each function will be run after its specified startDelay. Functions may also be added after start() has been called, in which case startDelay is still honored.
Throws an exception on error. In particular, each function must have a unique name–two functions cannot be added with the same name.
Referenced by folly::FunctionScheduler::LatencyDistribution::LatencyDistribution(), TEST(), and ~FunctionScheduler().
void folly::FunctionScheduler::addFunction | ( | Function< void()> && | cb, |
std::chrono::milliseconds | interval, | ||
const LatencyDistribution & | latencyDistr, | ||
StringPiece | nameID = StringPiece() , |
||
std::chrono::milliseconds | startDelay = std::chrono::milliseconds(0) |
||
) |
void folly::FunctionScheduler::addFunctionConsistentDelay | ( | Function< void()> && | cb, |
std::chrono::milliseconds | interval, | ||
StringPiece | nameID = StringPiece() , |
||
std::chrono::milliseconds | startDelay = std::chrono::milliseconds(0) |
||
) |
Add a new function to the FunctionScheduler whose start times are attempted to be scheduled so that they are congruent modulo the interval. Note: The scheduling of the next run time happens right before the function invocation, so the first time a function takes more time than the interval, it will be reinvoked immediately.
Definition at line 183 of file FunctionScheduler.cpp.
References folly::gen::move, and folly::Range< Iter >::str().
Referenced by folly::FunctionScheduler::LatencyDistribution::LatencyDistribution(), and TEST().
void folly::FunctionScheduler::addFunctionGenericDistribution | ( | Function< void()> && | cb, |
IntervalDistributionFunc && | intervalFunc, | ||
const std::string & | nameID, | ||
const std::string & | intervalDescr, | ||
std::chrono::milliseconds | startDelay | ||
) |
Add a new function to the FunctionScheduler. The scheduling interval is determined by the interval distribution functor, which is called every time the next function execution is scheduled. This allows for supporting custom interval distribution algorithms in addition to built in constant interval; and Poisson and jitter distributions (
Definition at line 197 of file FunctionScheduler.cpp.
References folly::gen::move.
Referenced by TEST().
void folly::FunctionScheduler::addFunctionGenericNextRunTimeFunctor | ( | Function< void()> && | cb, |
NextRunTimeFunc && | fn, | ||
const std::string & | nameID, | ||
const std::string & | intervalDescr, | ||
std::chrono::milliseconds | startDelay | ||
) |
Like addFunctionGenericDistribution, adds a new function to the FunctionScheduler, but the next run time is determined directly by the given functor, rather than by adding an interval.
Definition at line 212 of file FunctionScheduler.cpp.
References folly::gen::move.
|
private |
Referenced by addFunctionToHeapChecked().
|
private |
void folly::FunctionScheduler::addFunctionOnce | ( | Function< void()> && | cb, |
StringPiece | nameID = StringPiece() , |
||
std::chrono::milliseconds | startDelay = std::chrono::milliseconds(0) |
||
) |
Adds a new function to the FunctionScheduler to run only once.
Definition at line 154 of file FunctionScheduler.cpp.
References folly::gen::move, and folly::Range< Iter >::str().
Referenced by folly::FunctionScheduler::LatencyDistribution::LatencyDistribution(), and TEST().
|
private |
Definition at line 551 of file FunctionScheduler.cpp.
References folly::gen::move, mutex_, and now().
void folly::FunctionScheduler::addFunctionToHeapChecked | ( | Function< void()> && | cb, |
RepeatFuncNextRunTimeFunc && | fn, | ||
const std::string & | nameID, | ||
const std::string & | intervalDescr, | ||
milliseconds | startDelay, | ||
bool | runOnce | ||
) |
Definition at line 228 of file FunctionScheduler.cpp.
References addFunctionInternal(), folly::gen::move, mutex_, and string.
|
private |
void folly::FunctionScheduler::addFunctionUniformDistribution | ( | Function< void()> && | cb, |
std::chrono::milliseconds | minInterval, | ||
std::chrono::milliseconds | maxInterval, | ||
StringPiece | nameID, | ||
std::chrono::milliseconds | startDelay | ||
) |
Add a new function to the FunctionScheduler with the time interval being distributed uniformly within the given interval [minInterval, maxInterval].
Definition at line 167 of file FunctionScheduler.cpp.
References folly::gen::move, and folly::Range< Iter >::str().
Referenced by folly::FunctionScheduler::LatencyDistribution::LatencyDistribution(), and TEST().
void folly::FunctionScheduler::cancelAllFunctions | ( | ) |
All functions registered will be canceled.
Definition at line 362 of file FunctionScheduler.cpp.
References mutex_.
void folly::FunctionScheduler::cancelAllFunctionsAndWait | ( | ) |
Definition at line 367 of file FunctionScheduler.cpp.
References mutex_.
Referenced by TEST().
|
private |
Definition at line 350 of file FunctionScheduler.cpp.
bool folly::FunctionScheduler::cancelFunction | ( | StringPiece | nameID | ) |
Cancels the function with the specified name, so it will no longer be run.
Returns false if no function exists with the specified name.
Definition at line 310 of file FunctionScheduler.cpp.
References mutex_.
Referenced by TEST().
|
private |
Definition at line 340 of file FunctionScheduler.cpp.
References folly::FunctionScheduler::RepeatFunc::cancel(), mutex_, and folly::FunctionScheduler::RepeatFunc::name.
bool folly::FunctionScheduler::cancelFunctionAndWait | ( | StringPiece | nameID | ) |
Definition at line 324 of file FunctionScheduler.cpp.
References mutex_.
Referenced by TEST().
|
private |
Definition at line 295 of file FunctionScheduler.cpp.
bool folly::FunctionScheduler::resetFunctionTimer | ( | StringPiece | nameID | ) |
Resets the specified function's timer. When resetFunctionTimer is called, the specified function's timer will be reset with the same parameters it was passed initially, including its startDelay. If the startDelay was 0, the function will be invoked immediately.
Returns false if no function exists with the specified name.
Definition at line 374 of file FunctionScheduler.cpp.
Referenced by TEST().
|
private |
Definition at line 438 of file FunctionScheduler.cpp.
References folly::lock(), mutex_, now(), and folly::setThreadName().
|
private |
Definition at line 479 of file FunctionScheduler.cpp.
References folly::exceptionStr(), folly::gen::move, and mutex_.
|
inline |
By default steady is false, meaning schedules may lag behind overtime. This could be due to long running tasks or time drift because of randomness in thread wakeup time. By setting steady to true, FunctionScheduler will attempt to catch up. i.e. more like a cronjob
NOTE: it's only safe to set this before calling start()
Definition at line 68 of file FunctionScheduler.h.
References steady_.
Referenced by TEST().
void folly::FunctionScheduler::setThreadName | ( | StringPiece | threadName | ) |
Set the name of the worker thread.
Definition at line 569 of file FunctionScheduler.cpp.
References mutex_, and folly::Range< Iter >::str().
bool folly::FunctionScheduler::shutdown | ( | ) |
Stops the FunctionScheduler.
It may be restarted later by calling start() again. Returns false if the scheduler was not running.
Definition at line 424 of file FunctionScheduler.cpp.
Referenced by TEST().
bool folly::FunctionScheduler::start | ( | ) |
Starts the scheduler.
Returns false if the scheduler was already running.
Definition at line 399 of file FunctionScheduler.cpp.
References f, mutex_, now(), and folly::run().
Referenced by TEST().
|
private |
Definition at line 378 of file FunctionScheduler.h.
|
private |
Definition at line 370 of file FunctionScheduler.h.
|
private |
Definition at line 366 of file FunctionScheduler.h.
|
private |
Definition at line 364 of file FunctionScheduler.h.
|
private |
Definition at line 365 of file FunctionScheduler.h.
|
private |
Definition at line 359 of file FunctionScheduler.h.
|
private |
Definition at line 360 of file FunctionScheduler.h.
|
private |
Definition at line 374 of file FunctionScheduler.h.
|
private |
Definition at line 377 of file FunctionScheduler.h.
Referenced by setSteady().
|
private |
Definition at line 356 of file FunctionScheduler.h.
|
private |
Definition at line 376 of file FunctionScheduler.h.