Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
LuceneMonitor.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Collections;
20 using System.ComponentModel;
21 using System.Configuration;
22 using System.Data;
23 using System.Diagnostics;
24 using System.Net;
25 using System.Net.Sockets;
26 using System.Runtime.Remoting;
27 using System.Runtime.Remoting.Channels;
28 using System.ServiceProcess;
29 using System.Threading;
30 
31 using log4net.Core;
32 [assembly: log4net.Config.XmlConfigurator(Watch=true)]
33 
34 namespace Lucene.Net.Distributed.Operations
35 {
36  /// <summary>
37  /// A Windows service that provides system ping checking against LuceneServer.
38  /// </summary>
39  public class LuceneMonitor : System.ServiceProcess.ServiceBase
40  {
41  /// <summary>
42  /// Required designer variable.
43  /// </summary>
44  private System.ComponentModel.Container components = null;
45  private ServiceController scMonitor = new ServiceController();
46  private Thread serviceThread;
47  private int sleepTime = 5000;
48  private bool bRun = true;
49  private string ipAddress = "";
50  private int port = 0;
51  private static readonly log4net.ILog oLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
52 
53 
54  public LuceneMonitor()
55  {
56  // This call is required by the Windows.Forms Component Designer.
57  InitializeComponent();
58 
59  // TODO: Add any initialization after the InitComponent call
60  }
61 
62  // The main entry point for the process
63  static void Main()
64  {
65  System.ServiceProcess.ServiceBase[] ServicesToRun;
66  ServicesToRun = new System.ServiceProcess.ServiceBase[] { new LuceneMonitor() };
67  System.ServiceProcess.ServiceBase.Run(ServicesToRun);
68  }
69 
70  /// <summary>
71  /// Required method for Designer support - do not modify
72  /// the contents of this method with the code editor.
73  /// </summary>
74  private void InitializeComponent()
75  {
76  components = new System.ComponentModel.Container();
77  this.ServiceName = "LuceneMonitor";
78  }
79 
80  /// <summary>
81  /// Clean up any resources being used.
82  /// </summary>
83  protected override void Dispose( bool disposing )
84  {
85  if( disposing )
86  {
87  if (components != null)
88  {
89  components.Dispose();
90  }
91  }
92  base.Dispose( disposing );
93  }
94 
95  /// <summary>
96  /// Set things in motion so your service can do its work.
97  /// </summary>
98  protected override void OnStart(string[] args)
99  {
100  ThreadStart threadStart = new ThreadStart(MonitorService);
101  serviceThread = new Thread(threadStart);
102  serviceThread.Start();
103  }
104 
105  private void LogMessage(string message)
106  {
107  this.LogMessage(message, Level.Info);
108  }
109  private void LogMessage(string message, Level msgLevel)
110  {
111  if (msgLevel==Level.Info)
112  {
113  if (oLog.IsInfoEnabled)
114  oLog.Info(message);
115  }
116  else if (msgLevel==Level.Warn)
117  {
118  if (oLog.IsWarnEnabled)
119  oLog.Warn(message);
120  }
121  }
122  private void LogMessage(string message, Level msgLevel, int ErrorLevel)
123  {
124  if (msgLevel==Level.Error)
125  {
126  if (oLog.IsErrorEnabled)
127  {
128  oLog.Error(message);
129  EventLog.WriteEntry(this.ServiceName, message, EventLogEntryType.Error, ErrorLevel);
130  }
131  }
132  }
133 
134  private void MonitorService()
135  {
136  this.LogMessage(this.ServiceName+" started");
137  scMonitor.ServiceName="LuceneServer";
138  this.sleepTime = (ConfigurationManager.AppSettings["ServiceSleepTime"] != null ? Convert.ToInt32(ConfigurationManager.AppSettings["ServiceSleepTime"]) : this.sleepTime);
139  this.ipAddress = (ConfigurationManager.AppSettings["IPAddress"] != null ? ConfigurationManager.AppSettings["IPAddress"] : "");
140  this.port = (ConfigurationManager.AppSettings["Port"] != null ? Convert.ToInt32(ConfigurationManager.AppSettings["Port"]) : 0);
141  this.LogMessage("ServiceSleepTime = "+this.sleepTime.ToString()+"; ipAddress="+this.ipAddress+"; port="+this.port.ToString());
142 
143  while (bRun)
144  {
145  this.CheckService();
146  Thread.Sleep(sleepTime);
147  }
148  }
149 
150  private void CheckService()
151  {
152  try
153  {
154  scMonitor.Refresh();
155 
156  if (scMonitor.Status.Equals(ServiceControllerStatus.StopPending))
157  scMonitor.WaitForStatus(ServiceControllerStatus.Stopped);
158 
159  if (scMonitor.Status.Equals(ServiceControllerStatus.Stopped))
160  {
161  // Start the service if the current status is stopped.
162  foreach (IChannel ic in ChannelServices.RegisteredChannels)
163  ChannelServices.UnregisterChannel(ic);
164  scMonitor.Start();
165  this.LogMessage(scMonitor.ServiceName + " started (Service stopped or StopPending)", Level.Error, 99);
166  }
167  }
168  catch (Exception e)
169  {
170  this.LogMessage(scMonitor.ServiceName + " error: "+e.Message+e.StackTrace, Level.Error, 199);
171  }
172 
173  }
174 
175 
176  /// <summary>
177  /// Stop this service.
178  /// </summary>
179  protected override void OnStop()
180  {
181  this.bRun=false;
182  }
183  }
184 }