0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TimeWindow.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
27 
28 #include <Common/Compat.h>
29 #include "TimeWindow.h"
30 
31 #include <Common/Crontab.h>
32 #include <Common/Error.h>
33 #include <Common/Logger.h>
34 
35 #include <boost/algorithm/string.hpp>
36 
37 #include <ctime>
38 
39 using namespace Hypertable;
40 
41 TimeWindow::TimeWindow(std::vector<String> crontab_specs)
42  : m_within_window(false), m_enabled(true) {
43 
44  for (auto &spec : crontab_specs) {
45  try {
46  boost::trim_if(spec, boost::is_any_of("\"'"));
47  if (!spec.empty())
48  m_crontabs.push_back( Crontab(spec) );
49  }
50  catch (Exception &e) {
51  HT_FATALF("Problem parsing time window crontab entry (%s) - %s",
52  spec.c_str(), e.what());
53  }
54 
55  }
56 }
57 
59  bool window_status_on_entry = m_within_window;
60  time_t next;
61 
62  if (now == 0)
63  now = time(0);
64 
65  for (auto &crontab : m_crontabs) {
66  next = crontab.next_event(now);
67  HT_ASSERT(next >= now);
68  // if next event occurs within the next minute, we're within window
69  if (next - now <= (time_t)60) {
70  m_within_window = true;
71  return window_status_on_entry != m_within_window;
72  }
73  }
74  m_within_window = false;
75  return window_status_on_entry != m_within_window;
76 }
bool m_within_window
Set to true if currently within the time window.
Definition: TimeWindow.h:109
Declarations for TimeWindow.
bool update_current_time(time_t now=0)
Logically sets the current time.
Definition: TimeWindow.cc:58
#define HT_ASSERT(_e_)
Definition: Logger.h:396
Tracks timing of periodic events.
Definition: Crontab.h:55
Logging routines and macros.
Compatibility Macros for C/C++.
Hypertable definitions
#define HT_FATALF(msg,...)
Definition: Logger.h:343
This is a generic exception class for Hypertable.
Definition: Error.h:314
TimeWindow()
Default constructor.
Definition: TimeWindow.h:65
Crontab class for periodic events.
Error codes, Exception handling, error logging.
std::vector< Crontab > m_crontabs
Vector of Crontab objects that define each subwindow.
Definition: TimeWindow.h:106