private void RefreshRepository() { Collection <DateTime> jobRuns = null; Job2 job2; this.CreateFileSystemWatcher(); IEnumerable <string> jobDefinitions = ScheduledJobStore.GetJobDefinitions(); foreach (string str in jobDefinitions) { jobRuns = ScheduledJobSourceAdapter.GetJobRuns(str); if (jobRuns == null) { continue; } ScheduledJobDefinition scheduledJobDefinition = null; IEnumerator <DateTime> enumerator = jobRuns.GetEnumerator(); using (enumerator) { while (enumerator.MoveNext()) { DateTime dateTime = enumerator.Current; if (dateTime <= ScheduledJobSourceAdapter.JobRepository.GetLatestJobRun(str)) { continue; } try { if (scheduledJobDefinition == null) { scheduledJobDefinition = ScheduledJobDefinition.LoadFromStore(str, null); } job2 = ScheduledJobSourceAdapter.LoadJobFromStore(scheduledJobDefinition.Name, dateTime); } catch (ScheduledJobException scheduledJobException) { continue; } catch (DirectoryNotFoundException directoryNotFoundException) { continue; } catch (FileNotFoundException fileNotFoundException) { continue; } catch (UnauthorizedAccessException unauthorizedAccessException) { continue; } catch (IOException oException) { continue; } ScheduledJobSourceAdapter.JobRepository.AddOrReplace(job2); ScheduledJobSourceAdapter.JobRepository.SetLatestJobRun(str, dateTime); } } } }
internal static void SaveJobToStore(ScheduledJob job) { DateTime valueOrDefault; DateTime minValue; string outputPath = job.Definition.OutputPath; if (!string.IsNullOrEmpty(outputPath)) { FileStream fileStream = null; FileStream fileStream1 = null; try { ScheduledJobSourceAdapter.CheckJobStoreResults(outputPath, job.Definition.ExecutionHistoryLength); string str = outputPath; DateTime?pSBeginTime = job.PSBeginTime; if (pSBeginTime.HasValue) { valueOrDefault = pSBeginTime.GetValueOrDefault(); } else { valueOrDefault = DateTime.MinValue; } fileStream = ScheduledJobStore.CreateFileForJobRunItem(str, valueOrDefault, ScheduledJobStore.JobRunItem.Status); ScheduledJobSourceAdapter.SaveStatusToFile(job, fileStream); string str1 = outputPath; DateTime?nullable = job.PSBeginTime; if (nullable.HasValue) { minValue = nullable.GetValueOrDefault(); } else { minValue = DateTime.MinValue; } fileStream1 = ScheduledJobStore.CreateFileForJobRunItem(str1, minValue, ScheduledJobStore.JobRunItem.Results); ScheduledJobSourceAdapter.SaveResultsToFile(job, fileStream1); } finally { if (fileStream != null) { fileStream.Close(); } if (fileStream1 != null) { fileStream1.Close(); } } return; } else { string str2 = StringUtil.Format(ScheduledJobErrorStrings.CantSaveJobNoFilePathSpecified, job.Name); throw new ScheduledJobException(str2); } }
/// <summary> /// Starts a job as defined by the contained ScheduledJobDefinition object. /// </summary> public override void StartJob() { lock (SyncRoot) { if (_job != null && !IsFinishedState(_job.JobStateInfo.State)) { string msg = StringUtil.Format(ScheduledJobErrorStrings.JobAlreadyRunning, _jobDefinition.Name); throw new PSInvalidOperationException(msg); } _statusInfo = null; _asyncJobStop = false; PSBeginTime = DateTime.Now; if (_powerShell == null) { InitialSessionState iss = InitialSessionState.CreateDefault2(); iss.Commands.Clear(); iss.Formats.Clear(); iss.Commands.Add( new SessionStateCmdletEntry("Start-Job", typeof(Microsoft.PowerShell.Commands.StartJobCommand), null)); // Get the default host from the default runspace. _host = GetDefaultHost(); _runspace = RunspaceFactory.CreateRunspace(_host, iss); _runspace.Open(); _powerShell = System.Management.Automation.PowerShell.Create(); _powerShell.Runspace = _runspace; // Indicate SetShouldExit to host. AddSetShouldExitToHost(); } else { _powerShell.Commands.Clear(); } _job = StartJobCommand(_powerShell); _job.StateChanged += new EventHandler <JobStateEventArgs>(HandleJobStateChanged); SetJobState(_job.JobStateInfo.State); // Add all child jobs to this object's list so that // the user and Receive-Job can retrieve results. foreach (Job childJob in _job.ChildJobs) { this.ChildJobs.Add(childJob); } // Add this job to the local repository. ScheduledJobSourceAdapter.AddToRepository(this); } // lock }
public override void PersistJob(Job2 job) { if (job != null) { ScheduledJobSourceAdapter.SaveJobToStore(job as ScheduledJob); return; } else { throw new PSArgumentNullException("job"); } }
private static void UpdateRepositoryObjects(FileSystemEventArgs e) { string str = null; DateTime dateTime; if (ScheduledJobSourceAdapter.GetJobRunInfo(e.Name, out str, out dateTime)) { ScheduledJob job = ScheduledJobSourceAdapter.JobRepository.GetJob(str, dateTime); if (job != null) { Job2 job2 = null; try { job2 = ScheduledJobSourceAdapter.LoadJobFromStore(str, dateTime); } catch (ScheduledJobException scheduledJobException) { } catch (DirectoryNotFoundException directoryNotFoundException) { } catch (FileNotFoundException fileNotFoundException) { } catch (UnauthorizedAccessException unauthorizedAccessException) { } catch (IOException oException) { } if (job2 != null) { job.Update(job2 as ScheduledJob); } return; } else { return; } } else { return; } }
public override void StartJob() { lock (base.SyncRoot) { if (this._job == null || this.IsFinishedState(this._job.JobStateInfo.State)) { this._statusInfo = null; this._asyncJobStop = false; base.PSBeginTime = new DateTime?(DateTime.Now); if (this._powerShell != null) { this._powerShell.Commands.Clear(); } else { InitialSessionState initialSessionState = InitialSessionState.CreateDefault2(); initialSessionState.Commands.Clear(); initialSessionState.Formats.Clear(); initialSessionState.Commands.Add(new SessionStateCmdletEntry("Start-Job", typeof(StartJobCommand), null)); this._host = this.GetDefaultHost(); this._runspace = RunspaceFactory.CreateRunspace(this._host, initialSessionState); this._runspace.Open(); this._powerShell = System.Management.Automation.PowerShell.Create(); this._powerShell.Runspace = this._runspace; this.AddSetShouldExitToHost(); } this._job = this.StartJobCommand(this._powerShell); this._job.StateChanged += new EventHandler <JobStateEventArgs>(this.HandleJobStateChanged); base.SetJobState(this._job.JobStateInfo.State); foreach (Job childJob in this._job.ChildJobs) { base.ChildJobs.Add(childJob); } ScheduledJobSourceAdapter.AddToRepository(this); } else { string str = StringUtil.Format(ScheduledJobErrorStrings.JobAlreadyRunning, this._jobDefinition.Name); throw new PSInvalidOperationException(str); } } }
internal static Job2 LoadJobFromStore(string definitionName, DateTime jobRun) { FileStream fileForJobRunItem = null; Exception exception = null; bool flag = false; Job2 job2 = null; try { try { fileForJobRunItem = ScheduledJobStore.GetFileForJobRunItem(definitionName, jobRun, ScheduledJobStore.JobRunItem.Results, FileMode.Open, FileAccess.Read, FileShare.Read); job2 = ScheduledJobSourceAdapter.LoadResultsFromFile(fileForJobRunItem); } catch (ArgumentException argumentException1) { ArgumentException argumentException = argumentException1; exception = argumentException; } catch (DirectoryNotFoundException directoryNotFoundException1) { DirectoryNotFoundException directoryNotFoundException = directoryNotFoundException1; exception = directoryNotFoundException; } catch (FileNotFoundException fileNotFoundException1) { FileNotFoundException fileNotFoundException = fileNotFoundException1; exception = fileNotFoundException; flag = true; } catch (UnauthorizedAccessException unauthorizedAccessException1) { UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1; exception = unauthorizedAccessException; } catch (IOException oException1) { IOException oException = oException1; exception = oException; } catch (SerializationException serializationException) { flag = true; } catch (InvalidDataContractException invalidDataContractException) { flag = true; } catch (XmlException xmlException) { flag = true; } catch (TypeInitializationException typeInitializationException) { flag = true; } } finally { if (fileForJobRunItem != null) { fileForJobRunItem.Close(); } } if (flag) { ScheduledJobStore.RemoveJobRun(definitionName, jobRun); } if (exception == null) { return(job2); } else { object[] objArray = new object[2]; objArray[0] = definitionName; objArray[1] = jobRun; string str = StringUtil.Format(ScheduledJobErrorStrings.CantLoadJobRunFromStore, objArray); throw new ScheduledJobException(str, exception); } }
private void CreateFileSystemWatcher() { if (ScheduledJobSourceAdapter.StoreWatcher == null) { lock (ScheduledJobSourceAdapter.SyncObject) { if (ScheduledJobSourceAdapter.StoreWatcher == null) { ScheduledJobSourceAdapter.StoreWatcher = new FileSystemWatcher(ScheduledJobStore.GetJobDefinitionLocation()); ScheduledJobSourceAdapter.StoreWatcher.IncludeSubdirectories = true; ScheduledJobSourceAdapter.StoreWatcher.NotifyFilter = NotifyFilters.LastWrite; ScheduledJobSourceAdapter.StoreWatcher.Filter = "Results.xml"; ScheduledJobSourceAdapter.StoreWatcher.EnableRaisingEvents = true; FileSystemWatcher storeWatcher = ScheduledJobSourceAdapter.StoreWatcher; storeWatcher.Changed += (object sender, FileSystemEventArgs e) => ScheduledJobSourceAdapter.UpdateRepositoryObjects(e); } } } }