public static void SetSchedule() { Com.VerySimple.Phreeze.Phreezer phreeze = new Com.VerySimple.Phreeze.Phreezer(ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString); // run sql that checks for any search package where the first notification is null or second notification is null and no agent examination has been completed log4net.LogManager.GetLogger("SetSchedule").Info("Start"); using (MySqlDataReader reader = phreeze.ExecuteReader("select s.s_id, s.a_id, s.o_id, s.r_id, s.att_id, s.ua_id, s.s_first_notification, s.s_second_notification, s.s_search_package_date, s.s_created, s.s_modified, orig.a_id, orig.a_username, orig.a_first_name, orig.a_last_name, orig.a_email, o.o_customer_id, o.o_client_name, o.o_id, o.o_internal_id, r.r_request_type_code from `schedule` s inner join `account` a on a.a_id = s.a_id and a.a_underwriter_codes not like '%dir-%' inner join `order` o on o.o_id = s.o_id inner join `request` r on r.r_id = s.r_id join account orig on orig.a_id = o.o_originator_id left join attachment att on att.att_request_id = r.r_id and (att.att_purpose_code = 'Committment' or att.att_purpose_code = 'ExamSheet') left join upload_log ul on ul.att_id = att.att_id and ul.ua_id <> o.o_originator_id where s.s_search_package_date is not null and att.att_request_id is null and ul.ul_id is null and (s.s_first_notification is null or s.s_second_notification is null) group by s.s_id, s.a_id, s.o_id, s.r_id, s.att_id, s.ua_id, s.s_first_notification, s.s_second_notification, s.s_search_package_date, s.s_created, s.s_modified, orig.a_id, orig.a_username, orig.a_first_name, orig.a_last_name, orig.a_email, o.o_customer_id, o.o_client_name, o.o_id, o.o_internal_id, r.r_request_type_code")) { while (reader.Read()) { string id = reader["s_id"].ToString(); ScheduledEvents.TimerDelegate p = Scheduler; DateTime nw = DateTime.Now; bool isFirstNotification = reader.IsDBNull(6); // actual date set in database DateTime dt = (DateTime)reader["s_search_package_date"]; //dt = dt.AddHours((isFirstNotification) ? 72 : 168); dt = (isFirstNotification) ? AddWorkDays(dt, 3) : AddWorkDays(dt, 7); TimeSpan ts = dt.Subtract(nw); if (ts.TotalMilliseconds < 0) { log4net.LogManager.GetLogger("SetSchedule").Info("Schedule Now Id: " + id); Scheduler(id); } else if (!timers.Contains(id)) { log4net.LogManager.GetLogger("SetSchedule").Info("Schedule Later Id: " + id + " in " + ts.TotalMilliseconds.ToString() + " milliseconds"); SetTimer(p, ((long)ts.TotalMilliseconds), true, id); } } } log4net.LogManager.GetLogger("SetSchedule").Info("End"); }
// Sets a timer to call a method when the timer runs out public static void SetTimer(ScheduledEvents.TimerDelegate func, long repeatTime, bool skipRepeat, string id) { if (timers.Contains(id)) { return; } TimerCallback timerDelegate = new TimerCallback(func); long repeat = (skipRepeat) ? Timeout.Infinite : repeatTime; Timer t = new Timer(timerDelegate, id, repeatTime, repeat); if (repeatTime != 0) { return; } // this lock was put in place to prevent two timers with the same key timers.Add(id, t); }