/// <summary> /// Validates if a windows service is running. /// </summary> /// <returns></returns> protected bool ValidateWindowsService() { try { if (string.IsNullOrEmpty(ServiceName)) { return(true); } ServiceController serviceController; if (!string.IsNullOrEmpty(ServiceMachineName)) { serviceController = new ServiceController( ServiceName, ServiceMachineName); } else { serviceController = new ServiceController(ServiceName); } if (serviceController.Status != ServiceControllerStatus.Running) { LogProvider.WarnFormat("Service '{0}' is not running.", Name); return(false); } LogProvider.DebugFormat("Service '{0}' is running.", Name); return(true); } catch (Exception ex) { LogProvider.FatalFormat("Failed to control service '{0}'.", ex, Name); return(false); } }
/// <summary> /// The code to be executed everytime the waiting period elapses. /// </summary> /// <remarks> /// This will complete before the waiting period until the next iteration begins. /// </remarks> public override void OnWaitingPeriodElapsed() { AssertConfigurationValid(); if (!IsMonitorEnabled()) { LogProvider.DebugFormat("This service monitor is disabled at this time: {0}", Name); return; } bool ok = ValidateServiceFile() && ValidateWindowsService(); if (!ok) { ok = RestartService(); } if (!ok) { ok = RestartServer(); } if (!ok) { LogProvider.WarnFormat("Non-functional service could not be restarted, '{0}'.", Name); } else { LogProvider.DebugFormat("Functional service '{0}'.", Name); } }
private void Dispose(bool disposing) { if (!_disposed) { LogProvider.DebugFormat("Disposing worker task {0}.", Name); ClearTimer(); LogProvider.DebugFormat("Disposed worker task {0}.", Name); } _disposed = true; }
/// <summary> /// Validates that a service file exists, and if not it is logged. /// A custom logger should be provided to notify the appropriate party. /// </summary> /// <returns></returns> protected bool ValidateServiceFile() { if (string.IsNullOrEmpty(OutputFileNameToWatch)) { return(true); } FileInfo fileInfo; try { fileInfo = new FileInfo(OutputFileNameToWatch); } catch (Exception acquireEx) { LogProvider.WarnFormat("Could not find file '{0}'.", acquireEx, OutputFileNameToWatch); return(false); } DateTime lastWrite; try { lastWrite = fileInfo.LastWriteTime; } catch (IOException accessEx) { LogProvider.WarnFormat("Could not determine last access time for '{0}'.", accessEx, OutputFileNameToWatch); return(false); } if (lastWrite.AddMinutes(OutputFileMaxAgeMinutes) < DateTime.Now) { if (lastWrite.AddMinutes(OutputFileMaxAgeMinutes * 10) < DateTime.Now) { //this file access time is likely old because it is inaccessible, // or this service has been ignored WAY to long. LogProvider.WarnFormat("'{0}' service cannot accurately check for updated file '{1}'. The file is inaccessible or severely out of date!", Name, OutputFileNameToWatch); } else { LogProvider.WarnFormat("'{0}' service has not updated file '{1}' in {2} minutes. The normal maximum age is {3} minutes old.", Name, OutputFileNameToWatch, DateTime.Now.Subtract(lastWrite).TotalMinutes, GetExpectedAgeMinutes()); } return(false); } LogProvider.DebugFormat("'{0}' service has updated file '{1}' {2} minutes ago.", Name, OutputFileNameToWatch, DateTime.Now.Subtract(lastWrite).TotalMinutes); return(true); }
/// <summary> /// Pings the task to ensure that it is running. /// </summary> public override void Ping() { if (_disposed) { throw new ObjectDisposedException("WorkerRole"); } if (_iterationRunning) { LogProvider.DebugFormat("Running iteration for '{1}' now, next due is {0:yyyy-MM-dd HH:mm:ss}", _nextIterationTimestamp, Name); } if (_lastCheckIn.AddSeconds(Math.Min(30, WaitingPeriodSeconds * 2)) < DateTime.UtcNow && _nextIterationTimestamp.AddSeconds(Math.Min(30, WaitingPeriodSeconds * 2)) < DateTime.UtcNow) { //if behind by a normal iteration duration, then restart the timer LogProvider.WarnFormat("Task timer is being restarted for '{1}' due to next iteration not firing on time, Last CheckIn: {0:yyyy-MM-dd HH:mm:ss}. Now: {2:yyyy-MM-dd HH:mm:ss}", _lastCheckIn, Name, DateTime.UtcNow); ResetTimer(); } else { LogProvider.DebugFormat("Next iteration for '{1}' due by {0:yyyy-MM-dd HH:mm:ss}", _nextIterationTimestamp, Name); } }