Example #1
0
        public override JobResult Run(JobContext context)
        {
            JobResult r = new JobResult();
            int sleep = 5;
            if (context.Arguments.ContainsKey("sleep"))
                sleep = (int) context.Arguments["sleep"];

            TimeSpan timeSpan = TimeSpan.FromSeconds(sleep);
            string message = string.Format("Sleep for {0} sec start.", timeSpan.TotalSeconds);
            
            context.UpdateStatus(message);
            Debug.WriteLine(message);
            
            Thread.Sleep(sleep);
            r.Result = string.Format("Sleep for {0} sec Complete.", timeSpan.TotalSeconds); ;

            return r;
        }
Example #2
0
        private void RunInternal()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            // get lock
            using (var jobLock = _jobLockProvider.Acquire(Name))
            {
                // give up if no lock
                if (!jobLock.LockAcquired)
                {
                    LastResult = "Could not acquire a job lock.";
                    LastStatus = JobStatus.Canceled;
                    Status     = JobStatus.Waiting;
                    IsBusy     = false;

                    return;
                }

                DateTime started = DateTime.Now;
                LastRunStartTime = started;
                JobManager.Current.OnJobRunning(new JobEventArgs(Name, JobAction.Running, _id));
                Trace.TraceInformation("Run job '{0}' at {1}.", Name, started);

                Status = JobStatus.Running;
                Interlocked.Increment(ref JobManager.JobsRunning);

                try
                {
                    CreateInstance();

                    var       context = new JobContext(Name, Description, LastRunStartTime, LastStatus, Arguments, UpdateStatus);
                    JobResult r       = _instance.Run(context);

                    if (r == null)
                    {
                        if (String.IsNullOrEmpty(LastResult))
                        {
                            LastResult = "Completed";
                        }
                        LastStatus = JobStatus.Completed;
                    }
                    else if (r.Error != null)
                    {
                        LastResult = r.Error.Message;
                        LastStatus = JobStatus.Error;

                        Trace.TraceError(r.Error.ToString());
                    }
                    else
                    {
                        if (r.Result != null)
                        {
                            LastResult = r.Result.ToString();
                        }
                        LastStatus = JobStatus.Completed;
                    }
                }
                catch (Exception ex)
                {
                    LastResult = ex.Message;
                    LastStatus = JobStatus.Error;

                    Trace.TraceError(ex.ToString());
                }
                finally
                {
                    Interlocked.Decrement(ref JobManager.JobsRunning);
                    LastRunFinishTime = DateTime.Now;

                    if (!_keepAlive)
                    {
                        _instance = null;
                    }

                    try
                    {
                        if (_jobHistoryProvider != null)
                        {
                            _jobHistoryProvider.SaveHistory(this);
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError("Error saving job history: " + ex.Message);
                    }

                    JobManager.Current.OnJobCompleted(
                        new JobCompletedEventArgs(
                            Name,
                            JobAction.Completed,
                            _id,
                            started,
                            LastRunFinishTime,
                            LastResult,
                            LastStatus));

                    Trace.TraceInformation(
                        "Job '{0}' completed with status '{1}'.",
                        Name, LastStatus);

                    Status = JobStatus.Waiting;
                    IsBusy = false;
                }
            } // release job lock
        }