/// <summary>
        /// Writes the job (which implements ISerializable) to the provided
        /// file stream.
        /// </summary>
        /// <param name="job">ScheduledJob job to save</param>
        /// <param name="fs">FileStream</param>
        private static void SaveResultsToFile(ScheduledJob job, FileStream fs)
        {
            XmlObjectSerializer serializer = new System.Runtime.Serialization.NetDataContractSerializer();

            serializer.WriteObject(fs, job);
            fs.Flush();
        }
        /// <summary>
        /// Creates a new Job2 object based on a definition name
        /// that can be run manually.  If the path parameter is
        /// null then a default location will be used to find the
        /// job definition by name.
        /// </summary>
        /// <param name="definitionName">ScheduledJob definition name</param>
        /// <param name="definitionPath">ScheduledJob definition file path</param>
        /// <returns>Job2 object</returns>
        public override Job2 NewJob(string definitionName, string definitionPath)
        {
            if (string.IsNullOrEmpty(definitionName))
            {
                throw new PSArgumentException("definitionName");
            }

            Job2 rtnJob = null;

            try
            {
                ScheduledJobDefinition scheduledJobDef =
                    ScheduledJobDefinition.LoadFromStore(definitionName, definitionPath);

                rtnJob = new ScheduledJob(
                    scheduledJobDef.Command,
                    scheduledJobDef.Name,
                    scheduledJobDef);
            }
            catch (FileNotFoundException)
            {
                // Return null if no job definition exists.
            }

            return(rtnJob);
        }
        private static void SaveResultsToFile(ScheduledJob job, FileStream fs)
        {
            XmlObjectSerializer netDataContractSerializer = new NetDataContractSerializer();

            netDataContractSerializer.WriteObject(fs, job);
            fs.Flush();
        }
        private static void SaveStatusToFile(ScheduledJob job, FileStream fs)
        {
            StatusInfo          statusInfo = new StatusInfo(job.InstanceId, job.Name, job.Location, job.Command, job.StatusMessage, job.JobStateInfo.State, job.HasMoreData, job.PSBeginTime, job.PSEndTime, job.Definition);
            XmlObjectSerializer netDataContractSerializer = new NetDataContractSerializer();

            netDataContractSerializer.WriteObject(fs, statusInfo);
            fs.Flush();
        }
        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);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Serializes a ScheduledJob and saves it to store.
        /// </summary>
        /// <param name="job">ScheduledJob</param>
        internal static void SaveJobToStore(ScheduledJob job)
        {
            string outputPath = job.Definition.OutputPath;

            if (string.IsNullOrEmpty(outputPath))
            {
                string msg = StringUtil.Format(ScheduledJobErrorStrings.CantSaveJobNoFilePathSpecified,
                                               job.Name);
                throw new ScheduledJobException(msg);
            }

            FileStream fsStatus  = null;
            FileStream fsResults = null;

            try
            {
                // Check the job store results and if maximum number of results exist
                // remove the oldest results folder to make room for these new results.
                CheckJobStoreResults(outputPath, job.Definition.ExecutionHistoryLength);

                fsStatus = ScheduledJobStore.CreateFileForJobRunItem(
                    outputPath,
                    job.PSBeginTime ?? DateTime.MinValue,
                    ScheduledJobStore.JobRunItem.Status);

                // Save status only in status file stream.
                SaveStatusToFile(job, fsStatus);

                fsResults = ScheduledJobStore.CreateFileForJobRunItem(
                    outputPath,
                    job.PSBeginTime ?? DateTime.MinValue,
                    ScheduledJobStore.JobRunItem.Results);

                // Save entire job in results file stream.
                SaveResultsToFile(job, fsResults);
            }
            finally
            {
                if (fsStatus != null)
                {
                    fsStatus.Close();
                }

                if (fsResults != null)
                {
                    fsResults.Close();
                }
            }
        }
        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;
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Method to update a ScheduledJob based on new state and
        /// result data from a provided Job.
        /// </summary>
        /// <param name="fromJob">ScheduledJob to update from.</param>
        internal void Update(ScheduledJob fromJob)
        {
            // We do not update "live" jobs.
            if (_job != null || fromJob == null)
            {
                return;
            }

            //
            // Update status.
            //
            PSEndTime = fromJob.PSEndTime;
            JobState state = fromJob.JobStateInfo.State;

            if (Status.State != state)
            {
                SetJobState(state, null);
            }

            lock (SyncRoot)
            {
                _statusInfo = new StatusInfo(
                    fromJob.InstanceId,
                    fromJob.Name,
                    fromJob.Location,
                    fromJob.Command,
                    fromJob.StatusMessage,
                    state,
                    fromJob.HasMoreData,
                    fromJob.PSBeginTime,
                    fromJob.PSEndTime,
                    fromJob._jobDefinition);
            }

            //
            // Update results.
            //
            CopyOutput(fromJob.Output);
            CopyError(fromJob.Error);
            CopyWarning(fromJob.Warning);
            CopyVerbose(fromJob.Verbose);
            CopyProgress(fromJob.Progress);
            CopyDebug(fromJob.Debug);
            CopyInformation(fromJob.Information);
        }
Esempio n. 9
0
        private static void UpdateRepositoryObjects(FileSystemEventArgs e)
        {
            // Extract job run information from change file path.
            string   updateDefinitionName;
            DateTime updateJobRun;

            if (!GetJobRunInfo(e.Name, out updateDefinitionName, out updateJobRun))
            {
                System.Diagnostics.Debug.Assert(false, "All job run updates should have valid directory names.");
                return;
            }

            // Find corresponding job in repository.
            ScheduledJob updateJob = JobRepository.GetJob(updateDefinitionName, updateJobRun);

            if (updateJob == null)
            {
                return;
            }

            // Load updated job information from store.
            Job2 job = null;

            try
            {
                job = LoadJobFromStore(updateDefinitionName, updateJobRun);
            }
            catch (ScheduledJobException)
            { }
            catch (DirectoryNotFoundException)
            { }
            catch (FileNotFoundException)
            { }
            catch (UnauthorizedAccessException)
            { }
            catch (IOException)
            { }

            // Update job in repository based on new job store data.
            if (job != null)
            {
                updateJob.Update(job as ScheduledJob);
            }
        }
 public override Job2 NewJob(string definitionName, string definitionPath)
 {
     if (!string.IsNullOrEmpty(definitionName))
     {
         Job2 scheduledJob = null;
         try
         {
             ScheduledJobDefinition scheduledJobDefinition = ScheduledJobDefinition.LoadFromStore(definitionName, definitionPath);
             scheduledJob = new ScheduledJob(scheduledJobDefinition.Command, scheduledJobDefinition.Name, scheduledJobDefinition);
         }
         catch (FileNotFoundException fileNotFoundException)
         {
         }
         return(scheduledJob);
     }
     else
     {
         throw new PSArgumentException("definitionName");
     }
 }