public WatcherResult Watch() { try { if (string.IsNullOrWhiteSpace(ProcessName)) { throw new MissingArgumentException(ProcessWatcherArgs.ProcessName); } var processes = System.Diagnostics.Process.GetProcessesByName(ProcessName); var process = processes.FirstOrDefault(e => !e.HasExited); if (!NotifyOnlyOnFirstOccurrence) { _lastStatus = null; } if (WatchForStopped) { if (process == null && ( // First Time after service started !_lastStatus.HasValue // Or process was previously running || _lastStatus.Value)) { _lastStatus = false; return(WatcherResult.Succeed( ArgumentCollection.New() .WithArgument(ProcessWatcherResultArgs.ProcessName, ProcessName) )); } _lastStatus = process != null; return(WatcherResult.NotFound); } if (process != null && ( // First Time after service started !_lastStatus.HasValue // Or process was previously stopped || !_lastStatus.Value) ) { _lastStatus = true; return(WatcherResult.Succeed( ArgumentCollection.New() .WithArgument(ProcessWatcherResultArgs.ProcessName, process.ProcessName) .WithArgument(ProcessWatcherResultArgs.ProcessId, process.Id) )); } _lastStatus = process != null; return(WatcherResult.NotFound); } catch (Exception exception) { LoggingService.Error(exception); return(WatcherResult.NotFound.WithException(exception)); } }
public void CreateResultSucceedInstanceTest() { var watcherResult = WatcherResult.Succeed(ArgumentCollection.New()); Assert.IsNotNull(watcherResult); Assert.IsTrue(watcherResult.Result); }
public WatcherResult Watch() { try { var usage = Counter.NextValue(); // LoggingService.Debug($"Last Usage: {_lastUsage}, Current Usage: {usage}, Target {TargetMaximumUsage}"); if (usage > TargetMaximumUsage && CanNotify()) { // Notify only for the first occurrence when exceeding the target maximum if (_lastUsage < TargetMaximumUsage // Current usage is greater than previous notification usage || _lastNotificationUsage < usage) { _lastUsage = usage; _lastNotificationUsage = usage; _lastNotificationTime = DateTime.Now; return(WatcherResult.Succeed(ArgumentCollection.New() .WithArgument(UsageWatcherResultArgs.Usage, usage.ToString("F")) )); } } _lastUsage = usage; return(WatcherResult.NotFound); } catch (Exception exception) { LoggingService.Error(exception); return(WatcherResult.NotFound); } }
public virtual WatcherResult Watch() { try { LoggingService.Trace("Watching."); if (DataManager == null) { LoggingService.Debug("Creating new instance of Plugin Data Manager."); DataManager = new PluginDataManager(this.Id); LoggingService.Debug("Setting Data File."); DataManager.SetDataFile(); } var plannedExecutionTime = DataManager.GetPlannedExecutionTime(); LoggingService.Trace("Planned Execution Time: {0}", plannedExecutionTime.ToString("G")); if (DateTime.Compare(DateTime.Now, plannedExecutionTime) >= 0) { LoggingService.Debug("Time Reached"); var nextExecutionTime = GetNextExecutionTime(); LoggingService.Debug("Saving Next Execution Time: {0}", nextExecutionTime.ToString("G")); DataManager.SaveNextExecutionTime(nextExecutionTime); return(WatcherResult.Succeed(ArgumentCollection.New())); } LoggingService.Trace("Time Not Reached Yet!"); return(WatcherResult.NotFound); } catch (Exception exception) { LoggingService.Error(exception); return(WatcherResult.NotFound); } }
public WatcherResult Watch() { try { if (string.IsNullOrWhiteSpace(Host)) { throw new Exception($"{nameof(Host)} not set"); } bool?oldStatus = _lastStatus; bool status; try { using (var ping = new Ping()) { PingReply pingReply = ping.Send(Host); status = (pingReply != null && pingReply.Status == IPStatus.Success); } } catch (PingException pingException) { LoggingService.Error(pingException); LoggingService.Error($"Exception HResult = {pingException.HResult}"); status = false; } bool notify = false; if (!_lastStatus.HasValue || status != _lastStatus.Value) { _lastStatus = status; if (AnyStatus) { notify = true; } else if (status == Reachable) { notify = true; } } if (!notify) { return(WatcherResult.NotFound); } _lastStatus = status; var statusString = status ? "Reachable" : "Unreachable"; return(WatcherResult.Succeed(ArgumentCollection.New() .WithArgument(PingStatusWatcherResultArgs.Host, this.Host) .WithArgument(PingStatusWatcherResultArgs.IsReachable, status)) .WithArgument(PingStatusWatcherResultArgs.ResultMessage, $"Host {Host} is {statusString}")); } catch (Exception exception) { LoggingService.Error(exception); return(WatcherResult.NotFound); } }
public void CreateResultSucceedWithArgumentFailsOnGettingWrongTypeTest() { var argumentKey = "key"; var argumentValue = "vale"; var watcherResult = WatcherResult.Succeed(ArgumentCollection.New().WithArgument(argumentKey, argumentValue)); Assert.IsNotNull(watcherResult); Assert.IsTrue(watcherResult.Result); Assert.IsNotNull(watcherResult.WatchingArguments); Assert.IsTrue(watcherResult.WatchingArguments.Any()); Assert.AreEqual(1, watcherResult.WatchingArguments.Count); Assert.IsTrue(watcherResult.WatchingArguments.HasArgument(argumentKey)); watcherResult.WatchingArguments.GetValue <int>(argumentKey); }
public void CreateResultSucceedWithArgumentInstanceTest() { var argumentKey = "key"; var argumentValue = "vale"; var watcherResult = WatcherResult.Succeed(ArgumentCollection.New().WithArgument(argumentKey, argumentValue)); Assert.IsNotNull(watcherResult); Assert.IsTrue(watcherResult.Result); Assert.IsNotNull(watcherResult.WatchingArguments); Assert.IsTrue(watcherResult.WatchingArguments.Any()); Assert.AreEqual(1, watcherResult.WatchingArguments.Count); Assert.IsTrue(watcherResult.WatchingArguments.HasArgument(argumentKey)); Assert.IsNotNull(watcherResult.WatchingArguments[argumentKey]); Assert.IsNotNull(watcherResult.WatchingArguments.GetValue <string>(argumentKey)); Assert.AreEqual(argumentValue, watcherResult.WatchingArguments[argumentKey]); Assert.AreEqual(argumentValue, watcherResult.WatchingArguments.GetValue <string>(argumentKey)); }
public WatcherResult Watch() { try { if (string.IsNullOrWhiteSpace(ServiceName)) { throw new Exception("ServiceName is incorrect ! NULL or EMPTY"); } var status = WinServiceUtilities.GetServiceStatus(ServiceName); bool notify = false; if (!_serviceStatus.HasValue || status != _serviceStatus.Value) { _serviceStatus = status; if (AnyStatus) { notify = true; } else if ((int)status == Status) { notify = true; } } if (!notify) { return(WatcherResult.NotFound); } _serviceStatus = status; return(WatcherResult.Succeed(ArgumentCollection.New() .WithArgument(WinServiceWatcherResultArgs.ServiceName, ServiceName) .WithArgument(WinServiceWatcherResultArgs.Status, status.ToString("G")))); } catch (Exception exception) { LoggingService.Error(exception); return(WatcherResult.NotFound .WithException(exception)); } }
public WatcherResult Watch() { try { var files = Select(); if (files.Any()) { return(WatcherResult.Succeed(ArgumentCollection.New() .WithArgument(FtpWatcherResultArgs.RemoteFilesCollection, files) )); } return(WatcherResult.NotFound); } catch (Exception exception) { LoggingService.Error(exception); return(WatcherResult.NotFound.WithException(exception)); } }
public WatcherResult Watch() { try { if (string.IsNullOrWhiteSpace(Url)) { throw new Exception("Url not set"); } HttpStatusCode?oldStatusCode = _lastStatusCode; HttpStatusCode status; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { status = response.StatusCode; } } catch (WebException webException) { if (webException.Response != null && webException.Response is HttpWebResponse response) { status = response.StatusCode; } else { throw; } } bool notify = false; if (!_lastStatusCode.HasValue || status != _lastStatusCode.Value) { _lastStatusCode = status; if (AnyStatus) { notify = true; } else if ((int)status == StatusCode) { notify = true; } } if (!notify) { return(WatcherResult.NotFound); } _lastStatusCode = status; string resultMessage = oldStatusCode.HasValue ? $"Response from Url {Url} has changed from {oldStatusCode} ({(int) oldStatusCode}) to {status} ({(int) status})" : $"Response from Url {Url} is {status} ({(int) status})"; return(WatcherResult.Succeed(ArgumentCollection.New() .WithArgument(UrlStatusWatcherResultArgs.Url, this.Url) .WithArgument(UrlStatusWatcherResultArgs.StatusCodeName, status.ToString("G"))) .WithArgument(UrlStatusWatcherResultArgs.StatusCodeValue, (int)status) .WithArgument(UrlStatusWatcherResultArgs.ResultMessage, resultMessage)); } catch (Exception exception) { LoggingService.Error(exception); return(WatcherResult.NotFound); } }