-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRunFor591Service.cs
More file actions
89 lines (83 loc) · 2.95 KB
/
Copy pathRunFor591Service.cs
File metadata and controls
89 lines (83 loc) · 2.95 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Autofac;
using Newtonsoft.Json;
using RunFor591.Common;
using RunFor591.CrawlerUtility;
using RunFor591.NotifyUtility;
using SimpleServices;
namespace RunFor591
{
public class RunFor591Service : IWindowsService
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public ApplicationContext AppContext { get; set; }
public void Start(string[] args)
{
//註冊IOC
AutoFacUtility.Run();
log.Info("Service Start.");
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
var MyTimer = new Timer();
MyTimer.Elapsed += new ElapsedEventHandler(StartCrawler);
var interval = Setting.GetTimerInterval();
MyTimer.Interval = interval * 1000;
MyTimer.Start();
}
private void StartCrawler(object sender, ElapsedEventArgs e)
{
try
{
log.Info("crawler excute");
AppContext.Log("crawler excute");
new Crawler().StartCrawl591();
}
catch (InvalidSettingException ex)
{
var msg = "Invalid Setting" + ex.Message;
log.Error(msg);
AppContext.Log(msg);
Helper.ShowMessageBox(msg, "Invalid settings.conf");
//如果是windows service,就用serviceController停止,service name寫在program.cs
if (!Environment.UserInteractive)
new ServiceController("RunFor591").Stop();
else
{
Environment.Exit(0);
}
}
catch (JsonReaderException ex)
{
log.Error(ex.Message);
}
catch (Exception ex)
{
var msg = "Service Error. msg:" + ex.Message + ex.StackTrace;
AppContext.Log(msg);
log.Error(msg);
var notifyService = AutoFacUtility.Container.Resolve<INotify>();
notifyService.SendMessage(msg);
}
}
public void Stop()
{
AppContext.Log("Service stop!");
log.Info("Service stop!");
}
void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception)args.ExceptionObject;
var msg = "Windows Service Error on: {0} " + e.Message + e.StackTrace;
log.Error(msg);
var notifyService = AutoFacUtility.Container.Resolve<INotify>();
notifyService.SendMessage(msg);
}
}
}