Defines a time window. More...
#include <TimeWindow.h>
Public Member Functions | |
TimeWindow () | |
Default constructor. More... | |
TimeWindow (std::vector< String > crontab_specs) | |
Constructor initialized with crontab specs. More... | |
bool | update_current_time (time_t now=0) |
Logically sets the current time. More... | |
bool | within_window () const |
Tests to see if current time is within the window. More... | |
void | enable_window (bool enable) |
Enables or disables this time window. More... | |
bool | is_window_enabled () const |
Indicates if the time window is enabled or disabled. More... | |
Private Attributes | |
std::vector< Crontab > | m_crontabs |
Vector of Crontab objects that define each subwindow. More... | |
bool | m_within_window |
Set to true if currently within the time window. More... | |
bool | m_enabled |
Set to true if the time window is enabled. More... | |
Defines a time window.
This class can be used to define a time window (or set of discontiguous time windows) that can be used, for example, to define times when certain activities can take place. It can be initialized with a set of crontab entries that define the time window(s) and has member functions for setting the current time and testing to see if the current time falls within the defined time window(s). The motivating use case for this class was to allow the Hypertable RangeServers to define the daily period of low activity and have it change it's maintenance algorithm to be more aggressive during these periods. Example usage:
vector<String> specs; specs.push_back("* 2-5 * * *"); TimeWindow low_activity_time(specs); ... time_t now = time(0); low_activity_time.set_current_time(now); if (low_activity_time.within_window()) { ... perform heavy mainteance activity ... }
Definition at line 61 of file TimeWindow.h.
|
inline |
Default constructor.
Definition at line 65 of file TimeWindow.h.
TimeWindow::TimeWindow | ( | std::vector< String > | crontab_specs | ) |
Constructor initialized with crontab specs.
This function constructs a Crontab object with each of the specs in crontab_specs
and pushes them onto m_crontabs. It wraps each Crontab object construction in a try/catch block and asserts with a message indicating the bad entry that caused the constructor to throw an exception.
crontab_specs | Crontab specs that define the time window(s) |
Definition at line 41 of file TimeWindow.cc.
|
inline |
Enables or disables this time window.
enable | true if to enable, false otherwise |
Definition at line 97 of file TimeWindow.h.
|
inline |
Indicates if the time window is enabled or disabled.
Definition at line 101 of file TimeWindow.h.
bool TimeWindow::update_current_time | ( | time_t | now = 0 | ) |
Logically sets the current time.
This function fetches the current time (or uses now
if non-zero) and determines if it falls within the time window by checking it against all of the Crontab objects in m_crontabs. For each Crontab object in m_crontabs, it calls Crontab::next_event() and if any of them report that the next event is within 60 seconds of the current time, it sets m_within_window to true, otherwise it sets it to false.
now | Time to use for checking if we're within the time window |
Definition at line 58 of file TimeWindow.cc.
|
inline |
Tests to see if current time is within the window.
Checks to see if the current time (as set by set_current_time()) is within the time window.
Definition at line 93 of file TimeWindow.h.
|
private |
Vector of Crontab objects that define each subwindow.
Definition at line 106 of file TimeWindow.h.
|
private |
Set to true if the time window is enabled.
Definition at line 112 of file TimeWindow.h.
|
private |
Set to true if currently within the time window.
Definition at line 109 of file TimeWindow.h.