Files
ayanova7/utils/GeneratorService/Service1.cs
2018-06-29 19:47:36 +00:00

184 lines
6.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using GZTW.AyaNova.BLL;
namespace GeneratorService
{
public partial class AyGenSrv : ServiceBase
{
private System.Timers.Timer timer = null;
private bool Initialized = false;
private int maxTries = 0;
private const int retryInterval = 60000;
private const int processInterval = 300000;
//case 2048
private string eventLogSource = "AyaNovaGenerator";
public AyGenSrv()
{
InitializeComponent();
//Init timer and set initial interval to 1 second
//so that it starts "immediately" when the service starts
//after it's fired it will go to it's normal cycle of 5 minutes
timer = new System.Timers.Timer(1000);
//set the event handler for the timer
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.ServiceTimer_Tick);
}
private bool TryConnecting()
{
//Failed to connect after maxtries?
if (maxTries > 9)
{
EventLog.WriteEntry(eventLogSource,
"Connection to AyaNova database could not be initialized after 10 attempts.\r\n" +
"Other errors in the application error log before or after this one may provide more details.\r\n" +
"The configuration file config.txt probably has an incorrect connection setting\r\n" +
"to access the database (wrong path, login or password) or the database server is not running.", EventLogEntryType.Error);
QuitNow();
return false;
}
if (GZTW.AyaNova.BLL.AyaBizUtils.AyaNovaConnectionSetting.SingleUserConnection)
{
EventLog.WriteEntry(eventLogSource,
"The current database connection in the config.txt file specifies a stand alone embedded FireBird database.\r\n" +
"Generator is not required for a single user database connection. It's required for a shared network connection only.\r\n" +
"In a single user connection AyaNova handles the tasks of Generator automatically.\r\n" +
"Generator will now close.\r\n" +
"See the AyaNova manual for more details.", EventLogEntryType.Error);
QuitNow();
return false;
}
//not timed out so try to connect
try
{
GZTW.AyaNova.BLL.AyaBizUtils.Initialize();
//case 2093 ampersand in name crashing generator service
// eventLogSource = "AyaNovaGenerator - (" + AyaBizUtils.REGTO + ")";
eventLogSource = "AyaNovaGenerator - (" + System.Text.RegularExpressions.Regex.Replace(AyaBizUtils.REGTO, @"[^A-Za-z0-9]+", "") +")";
Initialized = true;
EventLog.WriteEntry(eventLogSource,
"Connection established", EventLogEntryType.Information);
}
catch (Exception ex)
{
Initialized = false;
maxTries++;
if(maxTries < 10)
EventLog.WriteEntry(eventLogSource,
"Connection failed (attempt "+maxTries.ToString()+" of 10 ) - retrying in 1 minute", EventLogEntryType.Warning);
//Only log the last attempt, earlier ones could have been waiting for dbserver to start and not relevant
string strMessage = ex.Message;
if (ex.InnerException != null)
strMessage = ex.InnerException.Message;
if(maxTries > 9)
EventLog.WriteEntry(eventLogSource, strMessage, EventLogEntryType.Error);
}
return Initialized;
}
private void QuitNow()
{
timer.Stop();
timer.AutoReset = false;
timer.Enabled = false;
this.Stop();
}
protected override void OnStart(string[] args)
{
timer.AutoReset = true;
timer.Enabled = true;
timer.Start();
}
protected override void OnStop()
{
timer.Stop();
timer.AutoReset = false;
timer.Enabled = false;
base.OnStop();
}
protected override void OnPause()
{
timer.Stop();
base.OnPause();
}
protected override void OnContinue()
{
timer.Start();
base.OnContinue();
}
//process
private void ServiceTimer_Tick(object sender, System.Timers.ElapsedEventArgs e)
{
this.timer.Stop();
//make an appearance of less memory for those that don't understand how .net memory allocation works
//and bitch to us that task manager shows it's using too much memory
try
{
System.Diagnostics.Process loProcess = System.Diagnostics.Process.GetCurrentProcess();
loProcess.MaxWorkingSet = loProcess.MaxWorkingSet;//doesn't matter what you set it to, just setting it triggers re-evalutaion
}
catch { };
//If not initialized restart with a shorter timer than normal
if (!Initialized)
{
if (!TryConnecting())
{
this.timer.Interval = retryInterval;
this.timer.Start();
return;
}
}
//It's initialized so process as normal
try
{
GenProcess.GO("121605GENERATOR", "121605GENERATOR");
}
catch (Exception ex)
{
string strMessage = ex.Message;
if (ex.InnerException != null)
strMessage = ex.InnerException.Message;
EventLog.WriteEntry(eventLogSource, strMessage, EventLogEntryType.Error);
}
this.timer.Interval = processInterval;
this.timer.Start();
}
}
}