2017-01-25 09:41:24 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2016-02-05 08:14:39 +01:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
|
* that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-09-05 00:07:22 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2016-02-05 08:14:39 +01:00
|
|
|
|
|
|
|
|
|
namespace XenAPI
|
|
|
|
|
{
|
2017-05-25 15:56:13 +02:00
|
|
|
|
public partial class VMSS
|
2016-02-05 08:14:39 +01:00
|
|
|
|
{
|
2019-03-15 15:10:42 +01:00
|
|
|
|
public DateTime? GetNextRunTime(DateTime serverLocalTime)
|
2016-02-05 08:14:39 +01:00
|
|
|
|
{
|
2019-03-15 15:10:42 +01:00
|
|
|
|
if (!TryGetScheduleMin(schedule, out int min))
|
|
|
|
|
return null;
|
|
|
|
|
|
2016-02-15 05:35:26 +01:00
|
|
|
|
if (frequency == vmss_frequency.hourly)
|
2019-03-15 15:10:42 +01:00
|
|
|
|
return GetHourlyDate(serverLocalTime, min);
|
|
|
|
|
|
|
|
|
|
if (!TryGetScheduleHour(schedule, out int hour))
|
|
|
|
|
return null;
|
|
|
|
|
|
2016-02-15 05:35:26 +01:00
|
|
|
|
if (frequency == vmss_frequency.daily)
|
2019-03-15 15:10:42 +01:00
|
|
|
|
return GetDailyDate(serverLocalTime, min, hour);
|
|
|
|
|
|
2016-02-15 05:35:26 +01:00
|
|
|
|
if (frequency == vmss_frequency.weekly)
|
2017-09-05 00:07:22 +02:00
|
|
|
|
{
|
2019-03-15 15:10:42 +01:00
|
|
|
|
var dates = GetWeeklyDates(serverLocalTime, min, hour, BackUpScheduleDays(schedule));
|
|
|
|
|
if (dates.Count > 0)
|
|
|
|
|
return dates[0];
|
2016-02-05 08:14:39 +01:00
|
|
|
|
}
|
2019-03-15 15:10:42 +01:00
|
|
|
|
|
|
|
|
|
return null;
|
2017-09-05 00:07:22 +02:00
|
|
|
|
}
|
2016-02-05 08:14:39 +01:00
|
|
|
|
|
2019-03-15 15:10:42 +01:00
|
|
|
|
public static DateTime GetHourlyDate(DateTime time, int min)
|
2016-02-05 08:14:39 +01:00
|
|
|
|
{
|
2019-03-15 15:10:42 +01:00
|
|
|
|
var nextDateTime = new DateTime(time.Year, time.Month, time.Day, time.Hour, min, 0);
|
|
|
|
|
if (time > nextDateTime)
|
|
|
|
|
nextDateTime = nextDateTime.AddHours(1);
|
|
|
|
|
return nextDateTime;
|
2017-09-03 04:33:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-15 15:10:42 +01:00
|
|
|
|
public static DateTime GetDailyDate(DateTime time, int min, int hour)
|
2017-09-03 04:33:29 +02:00
|
|
|
|
{
|
2019-03-15 15:10:42 +01:00
|
|
|
|
var nextDateTime = new DateTime(time.Year, time.Month, time.Day, hour, min, 0);
|
|
|
|
|
if (time > nextDateTime)
|
|
|
|
|
nextDateTime = nextDateTime.AddDays(1);
|
|
|
|
|
return nextDateTime;
|
2016-02-05 08:14:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-15 15:10:42 +01:00
|
|
|
|
public static List<DateTime> GetWeeklyDates(DateTime time, int min, int hour, DayOfWeek[] listDaysOfWeek)
|
2016-02-05 08:14:39 +01:00
|
|
|
|
{
|
2019-03-15 15:10:42 +01:00
|
|
|
|
var nextRun = new DateTime(time.Year, time.Month, time.Day, hour, min, 0);
|
|
|
|
|
|
|
|
|
|
var runs = new List<DateTime>();
|
|
|
|
|
foreach (var d in listDaysOfWeek)
|
2016-02-05 08:14:39 +01:00
|
|
|
|
{
|
2019-03-15 15:10:42 +01:00
|
|
|
|
if (nextRun.DayOfWeek < d)
|
|
|
|
|
runs.Add(nextRun.AddDays(d - nextRun.DayOfWeek));
|
|
|
|
|
else if (d < nextRun.DayOfWeek)
|
|
|
|
|
runs.Add(nextRun.AddDays(7 - (nextRun.DayOfWeek - d)));
|
|
|
|
|
else if (time < nextRun)
|
|
|
|
|
runs.Add(nextRun);
|
|
|
|
|
else
|
|
|
|
|
runs.Add(nextRun.AddDays(7));
|
2016-02-05 08:14:39 +01:00
|
|
|
|
}
|
2019-03-15 15:10:42 +01:00
|
|
|
|
|
|
|
|
|
runs.Sort();
|
|
|
|
|
return runs;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 04:33:29 +02:00
|
|
|
|
public override string Name()
|
2019-03-15 15:10:42 +01:00
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
return name_label;
|
2016-02-05 08:14:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 04:33:29 +02:00
|
|
|
|
public override string Description()
|
2016-02-05 08:14:39 +01:00
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
return name_description;
|
2016-02-05 08:14:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-15 15:10:42 +01:00
|
|
|
|
private static bool TryGetScheduleMin(Dictionary<string, string> schedule, out int result)
|
2016-02-05 08:14:39 +01:00
|
|
|
|
{
|
2019-03-15 15:10:42 +01:00
|
|
|
|
result = 0;
|
|
|
|
|
return schedule.TryGetValue("min", out var outStr) && int.TryParse(outStr, out result);
|
2016-02-05 08:14:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-15 15:10:42 +01:00
|
|
|
|
private static bool TryGetScheduleHour(Dictionary<string, string> schedule, out int result)
|
|
|
|
|
{
|
|
|
|
|
result = 0;
|
|
|
|
|
return schedule.TryGetValue("hour", out var outStr) && int.TryParse(outStr, out result);
|
2016-02-05 08:14:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-15 15:10:42 +01:00
|
|
|
|
public static DayOfWeek[] BackUpScheduleDays(Dictionary<string, string> schedule)
|
|
|
|
|
{
|
|
|
|
|
if (!schedule.ContainsKey("days"))
|
|
|
|
|
return new DayOfWeek[] { };
|
|
|
|
|
|
|
|
|
|
var days = schedule["days"].Split(new[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
var list = new List<DayOfWeek>();
|
|
|
|
|
foreach (var d in days)
|
|
|
|
|
{
|
|
|
|
|
if (Enum.TryParse(d, true, out DayOfWeek result))
|
|
|
|
|
list.Add(result);
|
|
|
|
|
}
|
|
|
|
|
return list.ToArray();
|
2016-02-05 08:14:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|