示例#1
0
        public virtual async Task <JobRunResult> Run(string jobName, CancellationToken cancelToken)
        {
            JobRunResult result;
            JobInfo?     actual = null;

            try
            {
                var job = await this.repository.Get <PersistJobInfo>(jobName);

                if (job == null)
                {
                    throw new ArgumentException("No job found named " + jobName);
                }

                actual = PersistJobInfo.FromPersist(job);
                result = await this.RunJob(actual, cancelToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Log.Write(ex);
                result = new JobRunResult(false, actual, ex);
            }

            return(result);
        }
示例#2
0
 public async Task Schedule(JobInfo jobInfo)
 {
     jobInfo.AssertValid();
     this.ResolveJob(jobInfo);
     this.ScheduleNative(jobInfo);
     await this.repository.Set(jobInfo.Identifier, PersistJobInfo.ToPersist(jobInfo));
 }
示例#3
0
        public async Task Cancel(string jobIdentifier)
        {
            var job = await this.repository.Get <PersistJobInfo>(jobIdentifier);

            if (job != null)
            {
                this.CancelNative(PersistJobInfo.FromPersist(job));
                await this.repository.Remove <PersistJobInfo>(jobIdentifier);
            }
        }
示例#4
0
        public async Task <JobInfo?> GetJob(string jobName)
        {
            var job = await this.repository.Get <PersistJobInfo>(jobName);

            if (job == null)
            {
                return(null);
            }

            return(PersistJobInfo.FromPersist(job));
        }
示例#5
0
        public virtual async Task CancelAll()
        {
            var jobs = await this.repository.GetAllWithKeys <PersistJobInfo>();

            foreach (var job in jobs)
            {
                if (!job.Value.IsSystemJob)
                {
                    this.CancelNative(PersistJobInfo.FromPersist(job.Value));
                    await this.repository.Remove <PersistJobInfo>(job.Key);
                }
            }
        }
示例#6
0
        public async Task <IEnumerable <JobRunResult> > RunAll(CancellationToken cancelToken, bool runSequentially)
        {
            var list = new List <JobRunResult>();

            if (!this.IsRunning)
            {
                try
                {
                    this.IsRunning = true;
                    var jobs = await this.repository.GetAll <PersistJobInfo>();

                    var tasks = new List <Task <JobRunResult> >();

                    if (runSequentially)
                    {
                        foreach (var job in jobs)
                        {
                            var actual = PersistJobInfo.FromPersist(job);
                            var result = await this
                                         .RunJob(actual, cancelToken)
                                         .ConfigureAwait(false);

                            list.Add(result);
                        }
                    }
                    else
                    {
                        foreach (var job in jobs)
                        {
                            var actual = PersistJobInfo.FromPersist(job);
                            tasks.Add(this.RunJob(actual, cancelToken));
                        }

                        await Task
                        .WhenAll(tasks)
                        .ConfigureAwait(false);

                        list.AddRange(tasks.Select(x => x.Result));
                    }
                }
                catch (Exception ex)
                {
                    Log.Write(ex);
                }
                finally
                {
                    this.IsRunning = false;
                }
            }
            return(list);
        }
示例#7
0
        protected async Task <JobRunResult> RunJob(JobInfo job, CancellationToken cancelToken)
        {
            this.jobStarted.OnNext(job);
            var result = default(JobRunResult);
            var cancel = false;

            try
            {
                this.LogJob(JobState.Start, job);
                var jobDelegate = this.ResolveJob(job);

                var newData = await jobDelegate
                              .Run(job, cancelToken)
                              .ConfigureAwait(false);

                if (!job.Repeat)
                {
                    await this.Cancel(job.Identifier);

                    cancel = true;
                }
                this.LogJob(JobState.Finish, job);
                result = new JobRunResult(newData, job, null);
            }
            catch (Exception ex)
            {
                this.LogJob(JobState.Error, job, ex);
                result = new JobRunResult(false, job, ex);
            }
            finally
            {
                if (!cancel)
                {
                    job.LastRunUtc = DateTime.UtcNow;
                    await this.repository.Set(job.Identifier, PersistJobInfo.ToPersist(job));
                }
            }
            this.jobFinished.OnNext(result);
            return(result);
        }
示例#8
0
 public async Task Register(JobInfo jobInfo)
 {
     this.ResolveJob(jobInfo);
     this.RegisterNative(jobInfo);
     await this.repository.Set(jobInfo.Identifier, PersistJobInfo.ToPersist(jobInfo));
 }