public void CloseIdle() { if (!IsRunning()) { return; } TimeSpan idleTime = new TimeSpan(_taskData.ActioningData.StopIfIdleHours, _taskData.ActioningData.StopIfIdleMinutes, _taskData.ActioningData.StopIfIdleSeconds); if (idleTime.TotalSeconds == 0) { return; // Do not kill tasks with 0 idle hours, minutes, seconds. } if (_taskData.DebugData.DateLastSignal.Add(idleTime) < DateTime.Now) { DoKill(); _taskData.DebugData.TaskStatus = ETaskStatus.Ready; StatusChanged?.Invoke(this, new TaskStatusChangedEventArgs(_taskStatus, _taskData.Id)); const string notificationMessageTemplate = "Task \"{0}\" stopped after being idle"; string notificationMessage = string.Format(notificationMessageTemplate, _taskData.Name); TaskNotification?.Invoke(this, new TaskNotificationEventArgs(notificationMessage, _taskData.Id, _taskData.Name, ENotificationType.TaskIdleKilled)); string managementMessage = ToManagementMessage(notificationMessage); _taskData.DebugData.Output += managementMessage; ManagementDataReceived?.Invoke(this, new TaskDataReceivedEventArgs(managementMessage, _taskData.Id)); } }
private void FireManagementDataReceived(object sender, TaskDataReceivedEventArgs e) { if (InvokeRequired) { FireManagementDataReceivedCallback d = new FireManagementDataReceivedCallback(FireManagementDataReceived); this?.Invoke(d, new object[] { sender, e }); } else { ManagementDataReceived?.Invoke(sender, e); } }
public void Kill() { DoKill(); const string notificationMessageTemplate = "Task \"{0}\" killed."; string notificationMessage = string.Format(notificationMessageTemplate, _taskData.Name); TaskNotification?.Invoke(this, new TaskNotificationEventArgs(notificationMessage, _taskData.Id, _taskData.Name, ENotificationType.TaskKilled)); string managementMessage = ToManagementMessage(notificationMessage); _taskData.DebugData.Output += managementMessage; ManagementDataReceived?.Invoke(this, new TaskDataReceivedEventArgs(managementMessage, _taskData.Id)); }
private void Process_Exited(object sender, EventArgs e) { lock (_locker) { _taskData.DebugData.DateLastSignal = DateTime.Now; Process process = sender as Process; int result = process.ExitCode; //this tell if there was an error or not string message = ToExitMessage(result.ToString()); _taskData.DebugData.Output += message; Exited?.Invoke(this, new TaskExitedEventArgs(result, message, _taskData.Id)); _taskData.DebugData.TaskStatus = _taskData.DebugData.HasErrors()?ETaskStatus.CompletedWithErrors: ETaskStatus.Ready; _taskStatus = _taskData.DebugData.TaskStatus; StatusChanged?.Invoke(this, new TaskStatusChangedEventArgs(_taskStatus, _taskData.Id)); _hasErrorMessage = false; _hasOutputMessage = false; _process = null; const string notificationMessageTemplate = "Task \"{0}\" exited with exit code {1}."; string notificationMessage = string.Format(notificationMessageTemplate, _taskData.Name, process.ExitCode); TaskNotification?.Invoke(this, new TaskNotificationEventArgs(notificationMessage, _taskData.Id, _taskData.Name, ENotificationType.TaskExit)); string managementMessage = ToManagementMessage(notificationMessage); _taskData.DebugData.Output += managementMessage; ManagementDataReceived?.Invoke(this, new TaskDataReceivedEventArgs(managementMessage, _taskData.Id)); } }
private void RunTaskAsProcess(TaskData taskData) { _hasErrorMessage = false; _hasOutputMessage = false; var process = new Process { StartInfo = new ProcessStartInfo { FileName = taskData.ActioningData.Command, Arguments = taskData.ActioningData.Parameters, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, WorkingDirectory = taskData.ActioningData.StartIn, CreateNoWindow = true } }; if (taskData.ActioningData.RunAsOther) { System.Security.SecureString ssPwd = new System.Security.SecureString(); process.StartInfo.Domain = taskData.ActioningData.Domain; process.StartInfo.UserName = taskData.ActioningData.UserName; string password = taskData.ActioningData.Password; for (int x = 0; x < password.Length; x++) { ssPwd.AppendChar(password[x]); } process.StartInfo.Password = ssPwd; } process.OutputDataReceived += Process_OutputDataReceived; process.ErrorDataReceived += Process_ErrorDataReceived; process.EnableRaisingEvents = true; process.Exited += Process_Exited; _taskData.DebugData.Output = string.Empty; _taskData.DebugData.TaskStatus = ETaskStatus.Running; _taskStatus = _taskData.DebugData.TaskStatus; string notificationMessageTemplate = "Starting task \"{0}\"... "; string notificationMessage = string.Format(notificationMessageTemplate, _taskData.Name); TaskNotification?.Invoke(this, new TaskNotificationEventArgs(notificationMessage, _taskData.Id, _taskData.Name, ENotificationType.TaskStart)); string managementMessage = ToManagementMessage(notificationMessage); _taskData.DebugData.Output += managementMessage; ManagementDataReceived?.Invoke(this, new TaskDataReceivedEventArgs(managementMessage, _taskData.Id)); StatusChanged?.Invoke(this, new TaskStatusChangedEventArgs(_taskStatus, _taskData.Id)); _taskData.DebugData.DateStarted = DateTime.Now; try { process.Start(); } catch (Exception ex) { notificationMessageTemplate = "Error while running \"{0}\": \r\b\t:{1}"; notificationMessage = string.Format(notificationMessageTemplate, _taskData.Name, ex.Message); TaskNotification?.Invoke(this, new TaskNotificationEventArgs(notificationMessage, _taskData.Id, _taskData.Name, ENotificationType.TaskCrash)); managementMessage = ToManagementMessage(notificationMessage); _taskData.DebugData.Output += managementMessage; ManagementDataReceived?.Invoke(this, new TaskDataReceivedEventArgs(managementMessage, _taskData.Id)); } process.BeginOutputReadLine(); process.BeginErrorReadLine(); _process = process; }