1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-08-14 05:49:26 +03:00

F #1548: Introduced relative actions into scheduler

This commit is contained in:
juanmont
2018-02-19 14:23:35 +01:00
parent e77b1adbe8
commit a0305c353a
4 changed files with 83 additions and 62 deletions

View File

@ -170,7 +170,7 @@ private:
/**
* This method removes sched_action DONE/MESSAGE attributes
*/
int parse_sched_action();
int parse_sched_action(string& error_str);
protected:

View File

@ -376,47 +376,6 @@ public:
*/
bool clear_log();
private:
void sum_days(struct tm * next, struct tm * now, int mayor_day, int minor_day, int max_day, int comparative){
if (mayor_day >= 0 && minor_day < max_day)
{
if( mayor_day < comparative ) //next
{
next->tm_mday = next->tm_mday + ((max_day-1) - comparative + minor_day);
}
else // same
{
next->tm_mday = next->tm_mday + (mayor_day - comparative);
}
}
}
void generate_next_day(int rep, int mayor_day, int minor_day, struct tm * next, struct tm * now)
{
if ( rep == 0 ) //Repeat every weeks
{
sum_days(next, now, mayor_day, minor_day, 7, next->tm_wday);
next->tm_min = now->tm_min;
next->tm_hour = now->tm_hour;
}
if ( rep == 1 ) //Repeat every months
{
cout << next->tm_mday << endl;
sum_days(next, now, mayor_day, minor_day, 32, next->tm_mday);
next->tm_min = now->tm_min;
next->tm_hour = now->tm_hour;
}
if ( rep == 2 ) //Repeat every months
{
sum_days(next, now, mayor_day, minor_day, 366, next->tm_yday);
next->tm_min = now->tm_min;
next->tm_hour = now->tm_hour;
}
}
protected:
/**

View File

@ -503,6 +503,50 @@ bool VirtualMachineXML::is_only_public_cloud() const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
static void sum_days(struct tm * next, struct tm * now, int mayor_day, int minor_day, int max_day, int comparative){
if (mayor_day >= 0 && minor_day < max_day)
{
if( mayor_day <= comparative ) //next
{
next->tm_mday = next->tm_mday + ((max_day) - comparative + minor_day);
}
else // same
{
next->tm_mday = next->tm_mday + (mayor_day - comparative);
}
}
}
/* -------------------------------------------------------------------------- */
static void generate_next_day(int rep, int mayor_day, int minor_day, struct tm * next, struct tm * now)
{
if ( rep == 0 ) //Repeat every weeks
{
sum_days(next, now, mayor_day, minor_day, 7, next->tm_wday);
next->tm_min = now->tm_min;
next->tm_hour = now->tm_hour;
}
if ( rep == 1 ) //Repeat every months
{
cout << next->tm_mday << endl;
sum_days(next, now, mayor_day, minor_day, 32, next->tm_mday);
next->tm_min = now->tm_min;
next->tm_hour = now->tm_hour;
}
if ( rep == 2 ) //Repeat every months
{
sum_days(next, now, mayor_day, minor_day, 366, next->tm_yday);
next->tm_min = now->tm_min;
next->tm_hour = now->tm_hour;
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachineXML::next_action(VectorAttribute& vatt)
{
string days;
@ -528,11 +572,10 @@ int VirtualMachineXML::next_action(VectorAttribute& vatt)
s_days.insert(atoi((*it).c_str()));
}
time_t t_next = time(0);
struct tm next;
struct tm start_tm;
localtime_r(&t_next, &next);
localtime_r(&action_time, &start_tm);
next = start_tm;
start_day = start_tm.tm_wday;
if (rep_mode == 1)
@ -552,16 +595,20 @@ int VirtualMachineXML::next_action(VectorAttribute& vatt)
ret = s_days.insert(start_day);
if ( ret.second == false )
{
mayor_day = *(ret.first);
mayor_day = *ret.first;
if ( ret.first++ != s_days.end() )
if ( ++ret.first != s_days.end() )
{
mayor_day = *ret.first;
}
}
else
{
mayor_day = *((ret.first)++);
mayor_day = minor_day;
if ( ++ret.first != s_days.end() )
{
mayor_day = *ret.first;
}
it = s_days.find(start_day);
s_days.erase (it);
}

View File

@ -74,9 +74,13 @@ int VMTemplate::insert(SqlDB *db, string& error_str)
erase_template_attribute("NAME", name);
// ---------------------------------------------------------------------
// Remove DONE/MESSAGE from SCHED_ACTION
// Remove DONE/MESSAGE from SCHED_ACTION and check rest attributes
// ---------------------------------------------------------------------
parse_sched_action();
int rc = parse_sched_action(error_str);
if (rc == -1)
{
return rc;
}
// ------------------------------------------------------------------------
// Insert the Template
@ -167,7 +171,7 @@ error_common:
return -1;
}
int VMTemplate::parse_sched_action()
int VMTemplate::parse_sched_action(string& error_str)
{
vector<VectorAttribute *> _sched_actions;
vector<VectorAttribute *>::iterator i;
@ -199,33 +203,38 @@ int VMTemplate::parse_sched_action()
first_day = *s_days.cbegin();
last_day = *s_days.cend();
if (!(rep_mode == 0 && first_day > 0 && last_day < 7)) //WEEK
if (!(rep_mode == 0 && first_day >= 0 && last_day < 7)) //WEEK [0,6]
{
error_str = "Error parsing days of the week. [0,6]";
return -1;
}
else if (!(rep_mode == 1 && first_day > 0 && last_day < 32)) //MONTH
else if (!(rep_mode == 1 && first_day >= 1 && last_day < 32)) //MONTH [1,31]
{
error_str = "Error parsing days of the month. [1,31]";
return -1;
}
else if (!(rep_mode == 2 && first_day > 0 && last_day < 366)) //YEAR
else if (!(rep_mode == 2 && first_day >= 0 && last_day < 366)) //YEAR [0,365]
{
error_str = "Error parsing days of the year. [0,365]";
return -1;
}
}
// else
// {
// return -1;
// }
else
{
error_str = "Error parsing DAYS and REP.";
return -1;
}
has_end_mode = vatt->vector_value("END_TYPE", end_mode);
has_end_value = vatt->vector_value("END_VALUE", end_value);
if (has_end_mode == 0 && has_end_value == 0)
{
// if (end_mode == 1 && end_value < 0) //N_REP
// {
// return -1;
// }
if (end_mode == 1 && end_value < 0) //N_REP
{
error_str = "Error parsing END_VALUE of type N_REP.";
return -1;
}
else if ( end_mode == 2 ) //DATE
{
time_t value = end_value;
@ -234,12 +243,14 @@ int VMTemplate::parse_sched_action()
time_t out = mktime(&val_tm);
if (out == -1)
{
error_str = "Error parsing END_VALUE of type DATE.";
return -1;
}
}
}
else
{
error_str = "Error parsing END_TYPE and END_VALUE.";
return -1;
}
@ -254,7 +265,11 @@ int VMTemplate::parse_sched_action()
int VMTemplate::post_update_template(string& error)
{
parse_sched_action();
int rc = parse_sched_action(error);
if (rc == -1)
{
return rc;
}
return 0;
}