public List <BackupSetSchedule> GetPlan(DateTime from, DateTime to) { //return GetNextToSchedule((int)to.Subtract(DateTime.Now).TotalMinutes); int interval = (int)to.Subtract(DateTime.Now).TotalMinutes; Console.WriteLine(" -- - - - GetPlan(): wanted interval (minutes)=" + interval); DateTime now = DateTime.Now; DateTime endInterval = DateTime.Now.AddMinutes(interval); using (dbc = DAL.Instance.GetDb()){ SqlExpressionVisitor <ScheduleTime> ev = OrmLiteConfig.DialectProvider.ExpressionVisitor <ScheduleTime>(); ev.Where(st => st.Day == DayOfWeek.Friday); var jn = new JoinSqlBuilder <BackupSetSchedule, BackupSet>(); //string endperiod = DateTime.Now.AddMinutes(interval).Hour+":"+DateTime.Now.AddMinutes(interval).Minute; // 1 - gather what to schedule for the rest of the current hour jn = jn.LeftJoin <BackupSet, ScheduleTime>(x => x.Id, y => y.BackupSetId) .Where <BackupSet>(x => x.Enabled && !x.IsTemplate) .And <ScheduleTime>(y => y.Day == now.DayOfWeek && y.BeginHour == now.Hour && y.BeginMinute >= now.Minute /*y.Day == endInterval.DayOfWeek*/); // 2 -remaining hours of the current day if (endInterval.DayOfWeek == now.DayOfWeek && endInterval.Hour > now.Hour + 1) { jn.Or <BackupSet>(x => x.Enabled && !x.IsTemplate) .And <ScheduleTime>(s => s.Day == now.DayOfWeek && s.BeginHour > now.Hour && s.BeginHour < endInterval.Hour); /*jn = jn.Or<ScheduleTime>(s => s.BeginHour == endInterval.Hour * && s.BeginMinute < endInterval.Minute);*/ } else if (endInterval.DayOfWeek != now.DayOfWeek) { jn.Or <BackupSet>(x => x.Enabled && !x.IsTemplate) .And <ScheduleTime>(s => s.Day == now.DayOfWeek && s.BeginHour > now.Hour && s.BeginHour <= 23); } // 3- loop and add every complete (24h) day until (interval_end_day -1), if any. int nbOfDays = endInterval.Subtract(now).Days; for (int i = 1; i <= nbOfDays; i++) { jn.Or <BackupSet>(x => x.Enabled && !x.IsTemplate) .And <ScheduleTime>(s => s.Day == now.AddDays(i).DayOfWeek); } if (endInterval.DayOfWeek != now.DayOfWeek) { // remaining hours of the end interval day jn.Or <BackupSet>(x => x.Enabled && !x.IsTemplate) .And <ScheduleTime>(y => y.Day == endInterval.DayOfWeek && y.BeginHour < endInterval.Hour //&& y.BeginMinute < endInterval.Minute ); // remaining minutes jn.Or <BackupSet>(x => x.Enabled && !x.IsTemplate) .And <ScheduleTime>(y => y.Day == endInterval.DayOfWeek && y.BeginHour == endInterval.Hour && y.BeginMinute < endInterval.Minute ); } jn.SelectAll <BackupSetSchedule>(); Console.WriteLine("next to schedule SQL = " + jn.ToSql()); return(dbc.Query <BackupSetSchedule>(jn.ToSql())); } }
/// <summary> /// Gets the next Backupsets to be scheduled for running. /// </summary> /// <returns> /// The next to schedule. /// </returns> /// <param name='interval'> /// Interval, in minutes. /// </param> public List <BackupSet> GetNextToSchedule(int interval) { Console.WriteLine(" -- - - - GetNextToSchedule : wanted interval (minutes)=" + interval); DateTime now = DateTime.Now; DateTime endInterval = DateTime.Now.AddMinutes(interval); using (dbc = DAL.Instance.GetDb()){ SqlExpressionVisitor <ScheduleTime> ev = OrmLiteConfig.DialectProvider.ExpressionVisitor <ScheduleTime>(); ev.Where(st => st.Day == DayOfWeek.Friday); //ev.O var jn = new JoinSqlBuilder <BackupSet, ScheduleTime>(); //string endperiod = DateTime.Now.AddMinutes(interval).Hour+":"+DateTime.Now.AddMinutes(interval).Minute; // 1 - gather what to schedule for the rest of the current hour jn = jn.Join <BackupSet, ScheduleTime>(x => x.Id, y => y.BackupSetId /*, null, x=> new{y}*/) .Where <BackupSet>(x => x.Enabled) .And <BackupSet>(x => !x.IsTemplate) /*.And<BackupSet>(x => x.Generation == * (dbc.GetScalar<BackupSet, int>(bs => Sql.Max(bs.Generation), bs => bs.Id == x.Id)) * )*/ .And <ScheduleTime>(y => y.Day == now.DayOfWeek && y.BeginHour == now.Hour && y.BeginMinute >= now.Minute /*y.Day == endInterval.DayOfWeek*/); // 2 -remaining hours of the current day if (endInterval.DayOfWeek == now.DayOfWeek && endInterval.Hour > now.Hour + 1) { jn = jn.And <ScheduleTime>(s => s.Day == now.DayOfWeek && s.BeginHour > now.Hour && s.BeginHour < endInterval.Hour); jn = jn.Or <ScheduleTime>(s => s.BeginHour == endInterval.Hour && s.BeginMinute < endInterval.Minute); } else if (endInterval.DayOfWeek != now.DayOfWeek) { jn = jn.And <ScheduleTime>(s => s.Day == now.DayOfWeek && s.BeginHour > now.Hour && s.BeginHour <= 23); } // 3- loop and add every complete (24h) day until (interval_end_day -1), if any. int nbOfDays = endInterval.Subtract(now).Days; for (int i = 1; i <= nbOfDays; i++) { jn = jn.Or <ScheduleTime>(s => s.Day == now.AddDays(i).DayOfWeek); } if (endInterval.DayOfWeek != now.DayOfWeek) { // remaining hours of the end interval day jn = jn.Or <ScheduleTime>(y => y.Day == endInterval.DayOfWeek && y.BeginHour < endInterval.Hour //&& y.BeginMinute < endInterval.Minute ); // remaining minutes jn = jn.Or <ScheduleTime>(y => y.Day == endInterval.DayOfWeek && y.BeginHour == endInterval.Hour && y.BeginMinute < endInterval.Minute ); } /*else{ // remaining minutes * jn = jn.And<ScheduleTime>(y => y.Day == endInterval.DayOfWeek * && y.BeginHour == endInterval.Hour * && y.BeginMinute < endInterval.Minute * ); * }*/ /*else if(endInterval.Hour > DateTime.Now.Hour){ * jn = jn.And<ScheduleTime>(y => y.BeginHour < endInterval.Hour * && y.BeginMinute < endInterval.Minute); * } * else if(endInterval.Hour== DateTime.Now.Hour){ * jn = jn.And<ScheduleTime>(y => y.BeginMinute < endInterval.Minute); * }*/ // minutes of end hour /*jn = jn.Or<ScheduleTime>(y => y.Day == endInterval.DayOfWeek * && y.BeginHour <= endInterval.Hour * && y.BeginMinute < endInterval.Minute * );*/ jn.SelectAll <BackupSet>(); Console.WriteLine("next to schedule SQL = " + jn.ToSql()); return(dbc.Query <BackupSet>(jn.ToSql())); } }