This commit is contained in:
Zeeshaun
2026-03-24 15:26:03 -05:00
parent 1e5beb292a
commit 9cc87337b8
333 changed files with 17931 additions and 0 deletions
@@ -0,0 +1,38 @@
using System;
using NCrontab;
using Serilog;
using LCU.Library.Internal.DispatcherModels.Entities;
using LCU.Library.Internal.DispatcherModels.Enums;
namespace LCU.Api.Internal.DispatcherSharedLib.Utilities;
public static class CronHelpers
{
public static void SetNextScheduledRunTime(ref Job job)
{
var isCronSchedule = !string.IsNullOrEmpty(job.CronSchedule);
if (isCronSchedule)
{
CrontabSchedule schedule = CrontabSchedule.Parse(job.CronSchedule);
job.NextRunTime = schedule.GetNextOccurrence(DateTime.Now);
if (job.SkipHolidays)
{
job.NextRunTime = HolidayHelpers.HandleHolidays(job.NextRunTime);
}
job.JobStatus = JobStatus.ReadyToSchedule;
}
}
public static CrontabSchedule SetCronSchedule(string cron, string cronDefault)
{
try
{
return CrontabSchedule.Parse(cron);
}
catch (Exception exception)
{
Log.Error(exception, "Failure parsing cron-tab in appsettings.");
return CrontabSchedule.Parse(cronDefault);
}
}
}
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace LCU.Api.Internal.DispatcherSharedLib.Utilities;
public static class HolidayHelpers
{
public static DateTime? HandleHolidays(DateTime? nextRunTime)
{
if (nextRunTime != null)
{
List<DateTime> holidays = GetHolidays(nextRunTime.Value.Year);
var today = Convert.ToDateTime(nextRunTime).Date;
var isHoliday = holidays.Where(x => today == x).Any();
if (isHoliday)
{
nextRunTime = nextRunTime.Value.AddDays(1);
}
}
return nextRunTime;
}
private static List<DateTime> GetHolidays(int year)
{
List<DateTime> holidays = new List<DateTime>
{
GetNewYearsDay(year),
GetMemorialDay(year),
GetIndependenceDay(year),
GetLaborDay(year),
GetThanksgivingDay(year),
GetChristmasDay(year)
};
return holidays;
}
///<summary> New Years Day - Jan 1st </summary>
public static DateTime GetNewYearsDay(int year)
{
return new DateTime(year, 1, 1).Date;
}
///<summary> Memorial Day - last Monday in May </summary>
public static DateTime GetMemorialDay(int year)
{
DateTime memorialDay = new DateTime(year, 5, 31);
var dayOfWeek = memorialDay.DayOfWeek;
while (dayOfWeek != DayOfWeek.Monday)
{
memorialDay = memorialDay.AddDays(-1);
dayOfWeek = memorialDay.DayOfWeek;
}
return memorialDay.Date;
}
///<summary> Independence Day - July 4th </summary>
public static DateTime GetIndependenceDay(int year)
{
return new DateTime(year, 7, 4).Date;
}
///<summary> Labor Day -- 1st Monday in September </summary>
public static DateTime GetLaborDay(int year)
{
DateTime laborDay = new DateTime(year, 9, 1);
var dayOfWeek = laborDay.DayOfWeek;
while (dayOfWeek != DayOfWeek.Monday)
{
laborDay = laborDay.AddDays(1);
dayOfWeek = laborDay.DayOfWeek;
}
return laborDay.Date;
}
///<summary> Thanksgiving Day - 4th Thursday in November </summary>
public static DateTime GetThanksgivingDay(int year)
{
var thanksgiving = (from day in Enumerable.Range(1, 30)
where new DateTime(year, 11, day).DayOfWeek == DayOfWeek.Thursday
select day).ElementAt(3);
return new DateTime(year, 11, thanksgiving).Date;
}
///<summary> Christmas Day - Dec 25th </summary>
public static DateTime GetChristmasDay(int year)
{
return new DateTime(year, 12, 25).Date;
}
}