public void is_time_hour_test() { var cron = new Cron("* * 3 * * *"); Assert.True(cron.IsTime(DateTime.Parse("3:00:00"))); Assert.Single(cron.Hours); Assert.Equal(3, cron.Hours[0]); cron = new Cron("* * 0,12 * * *"); Assert.True(cron.IsTime(DateTime.Parse("12:00:00"))); Assert.True(cron.IsTime(DateTime.Parse("12:00:00 pm"))); Assert.Equal(2, cron.Hours.Length); Assert.Equal(0, cron.Hours[0]); Assert.Equal(12, cron.Hours[1]); cron = new Cron("0 0 0,12 * * *"); // 下一次,零点 var dt = DateTime.Today; var dt2 = cron.GetNext(dt); Assert.Equal(dt.AddHours(12), dt2); dt2 = cron.GetNext(dt2); Assert.Equal(dt.AddHours(24), dt2); dt2 = cron.GetNext(dt2); Assert.Equal(dt.AddHours(36), dt2); }
public void is_time_second_test() { var cron = new Cron("0 * * * *"); Assert.True(cron.IsTime(DateTime.Parse("8:00:00"))); Assert.False(cron.IsTime(DateTime.Parse("8:00:01"))); Assert.Single(cron.Seconds); Assert.Equal(0, cron.Seconds[0]); cron = new Cron("0-10 * * * *"); Assert.True(cron.IsTime(DateTime.Parse("8:00:00"))); Assert.True(cron.IsTime(DateTime.Parse("8:00:03"))); Assert.Equal(11, cron.Seconds.Length); Assert.Equal(0, cron.Seconds[0]); Assert.Equal(10, cron.Seconds[10]); cron = new Cron("*/2 * * * *"); Assert.True(cron.IsTime(DateTime.Parse("8:00:00"))); Assert.True(cron.IsTime(DateTime.Parse("8:00:02"))); Assert.False(cron.IsTime(DateTime.Parse("8:00:03"))); Assert.Equal(30, cron.Seconds.Length); cron = new Cron("5/20 * * * *"); Assert.True(cron.IsTime(DateTime.Parse("8:00:05"))); Assert.True(cron.IsTime(DateTime.Parse("8:00:25"))); Assert.False(cron.IsTime(DateTime.Parse("8:00:20"))); Assert.Equal(3, cron.Seconds.Length); // 下一次,5秒后 var dt = DateTime.Today; var dt2 = cron.GetNext(dt); Assert.Equal(dt.AddSeconds(5), dt2); // 后续每次间隔20秒 dt2 = cron.GetNext(dt2); Assert.Equal(dt.AddSeconds(25), dt2); dt2 = cron.GetNext(dt2); Assert.Equal(dt.AddSeconds(45), dt2); }
public void is_time_day_of_month_test() { var cron = new Cron("* * * 1 * *"); Assert.True(cron.IsTime(DateTime.Parse("2010/08/01"))); Assert.Single(cron.DaysOfMonth); Assert.Equal(1, cron.DaysOfMonth[0]); cron = new Cron("0 0 0 1 * *"); // 下一次 var dt = DateTime.Parse("2010/08/01"); var dt2 = cron.GetNext(dt); Assert.Equal(dt.AddMonths(1), dt2); dt2 = cron.GetNext(dt2); Assert.Equal(dt.AddMonths(2), dt2); dt2 = cron.GetNext(dt2); Assert.Equal(dt.AddMonths(3), dt2); }
public void is_time_month_test() { var cron = new Cron("* * * * 1 *"); Assert.True(cron.IsTime(DateTime.Parse("1/1/2008"))); Assert.Single(cron.Months); Assert.Equal(1, cron.Months[0]); cron = new Cron("* * * * 12 *"); Assert.False(cron.IsTime(DateTime.Parse("1/1/2008"))); Assert.Single(cron.Months); Assert.Equal(12, cron.Months[0]); cron = new Cron("* * * * */3 *"); Assert.True(cron.IsTime(DateTime.Parse("3/1/2008"))); Assert.True(cron.IsTime(DateTime.Parse("6/1/2008"))); Assert.Equal(4, cron.Months.Length); Assert.Equal(3, cron.Months[0]); Assert.Equal(6, cron.Months[1]); Assert.Equal(9, cron.Months[2]); Assert.Equal(12, cron.Months[3]); cron = new Cron("0 0 0 1 */3 *"); // 下一次 var dt = DateTime.Parse("2010/08/01"); var dt2 = cron.GetNext(dt); Assert.Equal(DateTime.Parse("2010-09-01"), dt2); dt2 = cron.GetNext(dt2); Assert.Equal(DateTime.Parse("2010-12-01"), dt2); dt2 = cron.GetNext(dt2); Assert.Equal(DateTime.Parse("2011-03-01"), dt2); }
/// <summary>修改数据时,唤醒作业服务跟进</summary> /// <param name="entity"></param> /// <param name="type"></param> /// <param name="post"></param> /// <returns></returns> protected override Boolean Valid(CronJob entity, DataObjectMethodType type, Boolean post) { if (post) { var cron = new Cron(); if (!cron.Parse(entity.Cron)) { throw new ArgumentException("Cron表达式有误!", nameof(entity.Cron)); } // 重算下一次的时间 if (entity is IEntity e && !e.Dirtys[nameof(entity.Name)]) { entity.NextTime = cron.GetNext(DateTime.Now); } JobService.Wake(); } return(base.Valid(entity, type, post)); }