From bea95b03148c813a3e33fc356caff6f7cc71c691 Mon Sep 17 00:00:00 2001 From: fermin831 Date: Thu, 22 Feb 2018 12:08:47 +0100 Subject: [PATCH] [Windows Agent] Added support for cron day of the week --- pandora_agents/win32/misc/cron.cc | 102 ++++++++++++++++++------------ pandora_agents/win32/misc/cron.h | 4 +- 2 files changed, 64 insertions(+), 42 deletions(-) diff --git a/pandora_agents/win32/misc/cron.cc b/pandora_agents/win32/misc/cron.cc index 523b0397fe..b1e70c9d88 100644 --- a/pandora_agents/win32/misc/cron.cc +++ b/pandora_agents/win32/misc/cron.cc @@ -104,23 +104,6 @@ string Cron::getCronIntervalStr() { return ss.str(); } - -/** - * @brief Set utimestamp (private set) - * - * @param date when module will be executed next time - * @param now current timestamp. Required to update interval - */ -void Cron::setUtimestamp(time_t date, time_t now) { - this->utimestamp = date; - this->cronInterval = date - now; - Pandora::pandoraDebug( - "Module with cron %s will be executed at timestamp: %d.", - this->cronString.c_str(), - this->utimestamp - ); -} - /** * @brief Given a date, return if is inside a cron string or not * @@ -141,30 +124,42 @@ bool Cron::isInCron(time_t date) { // Check all positions faliures for (int i = 0; i < 4; i++) { - if (!isWildCard(i)) { - if (isSingleValue(i)) { - if (this->params[i][CRDOWN] != date_array[i]) return false; + if (!isBetweenParams(date_array[i], i)) return false; + } + + // If no failures, date is inside cron. + return true; +} + +/** + * @brief Check if a value is in a position of cron + * + * @param value Value to check + * @param position Position in cron to make the check + * @return If position is in cron + */ +bool Cron::isBetweenParams(int value, int position) { + if (!isWildCard(position)) { + if (isSingleValue(position)) { + if (this->params[position][CRDOWN] != value) return false; + } else { + if (isNormalInterval(position)) { + if ( + value < this->params[position][CRDOWN] || + value > this->params[position][CRUP] + ) { + return false; + } } else { - if (isNormalInterval(i)) { - if ( - date_array[i] < this->params[i][CRDOWN] || - date_array[i] > this->params[i][CRUP] - ) { - return false; - } - } else { - if ( - date_array[i] < this->params[i][CRDOWN] && - date_array[i] > this->params[i][CRUP] - ) { - return false; - } + if ( + value < this->params[position][CRDOWN] && + value > this->params[position][CRUP] + ) { + return false; } } } } - - // If no failures, date is inside cron. return true; } @@ -256,13 +251,38 @@ bool Cron::shouldExecuteAtFirst (time_t date) { */ void Cron::update (time_t date, int interval) { time_t next_time = getNextExecutionFrom(date, interval); - if (isWildCard(4)) { - setUtimestamp (next_time, date); - return; + + // Check the day of the week + struct tm * timeinfo = localtime(&next_time); + int count = 0; // Avoid infinite loops + while ((!isBetweenParams(timeinfo->tm_wday, 4)) && (count++ < CR_MAX_ITERS)){ + next_time = getNextExecutionFrom(next_time + CR_SECONDS_ONE_DAY, 0); + timeinfo = localtime(&next_time); + } + if (count >= CR_MAX_ITERS) { + Pandora::pandoraLog( + "Module with cron %s will be executed at timestamp: %d, but it can be incorrect", + this->cronString.c_str(), + this->utimestamp + ); } - // TODO if set day of the week - setUtimestamp (next_time, date); + // Security about values + if (next_time < date) { + this->utimestamp = date + interval; + this->cronInterval = interval; + Pandora::pandoraLog("Cron update fails in Module with cron %s", this->cronString.c_str()); + } + + // Save the data + this->utimestamp = next_time; + this->cronInterval = next_time - date; + Pandora::pandoraDebug( + "Module with cron %s will be executed at timestamp: %d.", + this->cronString.c_str(), + this->utimestamp + ); + return; } /** diff --git a/pandora_agents/win32/misc/cron.h b/pandora_agents/win32/misc/cron.h index 992598fb7e..022bf1bb41 100644 --- a/pandora_agents/win32/misc/cron.h +++ b/pandora_agents/win32/misc/cron.h @@ -33,6 +33,8 @@ const int CR_WILDCARD_VALUE = -1; const int CRDOWN = 0; const int CRUP = 1; const int CRINVALID_DATE = -1; +const int CR_SECONDS_ONE_DAY = 86400; +const int CR_MAX_ITERS = 60; class Cron { private: @@ -54,9 +56,9 @@ class Cron { time_t cronInterval; // Methods - void setUtimestamp(time_t date, time_t now); time_t getNextExecutionFrom(time_t date, int interval); bool isInCron(time_t date); + bool isBetweenParams(int value, int position); bool isWildCard(int position); bool isSingleValue(int position); bool isNormalInterval(int position);