프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / MarkusLogview / LogviewWatcher / LogviewWatcher.cs @ 84578b97

이력 | 보기 | 이력해설 | 다운로드 (3.4 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Diagnostics;
6
using System.Linq;
7
using System.ServiceProcess;
8
using System.Text;
9
using System.Threading.Tasks;
10
using System.Timers;
11

    
12
namespace LogviewWatcher
13
{
14
    partial class LogviewWatcher : ServiceBase
15
    {
16
        private Timer _timer;
17
        //private Int32 _hours = 0;
18
        //private Int32 _runAt = 3;
19
        private TimeSpan _waitTime = TimeSpan.FromMinutes(15);
20
        ServiceController _service = new ServiceController("logviewDep");
21

    
22
        public LogviewWatcher()
23
        {
24
            InitializeComponent();
25

    
26
            this.EventLog.Source = this.ServiceName;
27
            this.EventLog.Log = "Application";
28

    
29
            if (!EventLog.SourceExists(this.ServiceName))
30
                EventLog.CreateEventSource(this.ServiceName, "Application");
31

    
32
        }
33

    
34
        protected override void OnStart(string[] args)
35
        {
36
            // TODO: 여기에 서비스를 시작하는 코드를 추가합니다.
37
            try
38
            {
39
                _service.Start();
40
            }
41
            catch (Exception e)
42
            {
43
                this.EventLog.WriteEntry("Dependency wather start error  :: " + e.Message);
44
                throw;
45
            }
46

    
47
            // 30분으로 변경 
48
            // TODO: 여기에 서비스를 시작하는 코드를 추가합니다.
49
            //_hours = (24 - (DateTime.Now.Hour + 1)) + _runAt; ;
50
            _timer = new Timer();
51
            _timer.Interval = TimeSpan.FromMinutes(20).TotalMilliseconds;
52
            _timer.Elapsed += new ElapsedEventHandler(Tick);
53
            _timer.Start();
54

    
55
            this.EventLog.WriteEntry("watcher start");
56
            DependencyWatcher();
57

    
58
        }
59

    
60
        private void Tick(object sender, ElapsedEventArgs e)
61
        {
62
            DependencyWatcher();
63
        }
64

    
65

    
66
        void DependencyWatcher()
67
        {
68
            try
69
            {
70

    
71
                Process[] dep = Process.GetProcessesByName("LogviewSqlDependency");
72

    
73
                if (dep.Length > 0)
74
                {
75

    
76
                    dep[0].Kill();
77
                    this.EventLog.WriteEntry("Dependency wather :: Dependency Service Kill");
78

    
79
                    _service.Refresh();
80

    
81
                    if (_service.Status == ServiceControllerStatus.Running || _service.Status == ServiceControllerStatus.StartPending)
82
                    {
83

    
84
                        _service.WaitForStatus(ServiceControllerStatus.Stopped);
85

    
86
                    }
87

    
88
                    _service.Start(new string[] { "Restart" });
89
                    _service.WaitForStatus(ServiceControllerStatus.Running);
90

    
91
                    this.EventLog.WriteEntry("Dependency wather :: Dependency Service start");
92

    
93
                }
94
                else
95
                {
96

    
97
                    _service.Start();
98
                    _service.WaitForStatus(ServiceControllerStatus.Running);
99

    
100
                    this.EventLog.WriteEntry("Dependency wather :: process not found");
101
                }
102

    
103

    
104

    
105
            }
106
            catch (Exception ex)
107
            {
108

    
109
                this.EventLog.WriteEntry("Dependency wather :: Dependency service restart Error " + ex.ToString());
110

    
111
            }
112
        }
113

    
114

    
115
        protected override void OnStop()
116
        {
117
            // TODO: 서비스를 중지하는 데 필요한 작업을 수행하는 코드를 여기에 추가합니다.
118
            _timer.Stop();
119
        }
120
    }
121
}
클립보드 이미지 추가 (최대 크기: 500 MB)