-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonitorTimer.cs
56 lines (50 loc) · 2.37 KB
/
MonitorTimer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Threading;
namespace ServiceMonitor
{
public class MonitorTimer
{
public static Timer CreateTimer(Service currentService,SMTPConfig smtpConfiguration)
{
TimerCallback timerCallback = new TimerCallback(MonitorTimerCallback);
if(currentService.AutoRestartTime == "0")
{
return null; //disable timer
}
string[] splitTime = currentService.AutoRestartTime.Split(':');
int hours = int.Parse(splitTime[0]);
int minutes = int.Parse(splitTime[1]);
DateTime triggerTime = DateTime.Today.AddDays(0).AddHours(hours).AddMinutes(minutes);
TimeSpan timeDue = triggerTime.Subtract(DateTime.Now);
Messages.LogMessage($"Automatic restart timer scheduled for {triggerTime.ToShortDateString()} at {triggerTime.ToShortTimeString()}");
Messages.LogMessage($"{timeDue:%h} Hours {timeDue:%m} Minutes {timeDue:%s} Seconds from now.");
TimeSpan dailyEvent;
switch (currentService.AutoRestartFrequency)
{
case Service.Frequency.Hourly:
dailyEvent = new TimeSpan(0, 1, 0, 0);
break;
case Service.Frequency.Daily:
dailyEvent = new TimeSpan(1, 0, 0, 0);
break;
case Service.Frequency.Weekly:
dailyEvent = new TimeSpan(7, 0, 0, 0);
break;
case Service.Frequency.Monthly:
dailyEvent = new TimeSpan(30, 0, 0, 0);
break;
case Service.Frequency.Never:
//Intentional fallthrough to default
default:
return null; //No automatic restarts
}
return new Timer(timerCallback, currentService, timeDue, dailyEvent);
}
static void MonitorTimerCallback(object timedRestartService)
{
Service serviceToRestart = (Service)timedRestartService;
Messages.LogMessage($"Timer fired for {serviceToRestart.Name} at {DateTime.Now.ToShortDateString()} {DateTime.Now.ToLongTimeString()}",System.Diagnostics.EventLogEntryType.Information,EventID.TimerRestart);
ServiceControl.RestartService(serviceToRestart.serviceController, false);
}
}
}