Exemplo n.º 1
0
        public Task InsertAsync(BackgroundJobInfo jobInfo)
        {
            jobInfo.Id             = Interlocked.Increment(ref this._lastId);
            this._jobs[jobInfo.Id] = jobInfo;

            return(Task.FromResult(0));
        }
Exemplo n.º 2
0
        private void TryProcessJob(BackgroundJobInfo jobInfo)
        {
            try
            {
                jobInfo.TryCount++;
                jobInfo.LastTryTime = Clock.Now;

                var jobType = Type.GetType(jobInfo.JobType);
                using (var job = this._iocResolver.ResolveAsDisposable(jobType))
                {
                    try
                    {
                        var jobExecuteMethod = job.Object.GetType().GetMethod("Execute");
                        var argsType         = jobExecuteMethod.GetParameters()[0].ParameterType;
                        var argsObj          = JsonConvert.DeserializeObject(jobInfo.JobArgs, argsType);

                        jobExecuteMethod.Invoke(job.Object, new[] { argsObj });

                        AsyncHelper.RunSync(() => this._store.DeleteAsync(jobInfo));
                    }
                    catch (Exception ex)
                    {
                        this.Logger.Warn(ex.Message, ex);

                        var nextTryTime = jobInfo.CalculateNextTryTime();
                        if (nextTryTime.HasValue)
                        {
                            jobInfo.NextTryTime = nextTryTime.Value;
                        }
                        else
                        {
                            jobInfo.IsAbandoned = true;
                        }

                        this.TryUpdate(jobInfo);

                        this.EventBus.Trigger(
                            this,
                            new AbpHandledExceptionData(
                                new BackgroundJobException(
                                    "A background job execution is failed. See inner exception for details. See BackgroundJob property to get information on the background job.",
                                    ex
                                    )
                        {
                            BackgroundJob = jobInfo
                        }
                                )
                            );
                    }
                }
            }
            catch (Exception ex)
            {
                this.Logger.Warn(ex.ToString(), ex);

                jobInfo.IsAbandoned = true;

                this.TryUpdate(jobInfo);
            }
        }
Exemplo n.º 3
0
        public Task UpdateAsync(BackgroundJobInfo jobInfo)
        {
            if (jobInfo.IsAbandoned)
            {
                return(this.DeleteAsync(jobInfo));
            }

            return(Task.FromResult(0));
        }
Exemplo n.º 4
0
        public Task DeleteAsync(BackgroundJobInfo jobInfo)
        {
            if (!this._jobs.ContainsKey(jobInfo.Id))
            {
                return(Task.FromResult(0));
            }

            this._jobs.Remove(jobInfo.Id);

            return(Task.FromResult(0));
        }
Exemplo n.º 5
0
 private void TryUpdate(BackgroundJobInfo jobInfo)
 {
     try
     {
         this._store.UpdateAsync(jobInfo);
     }
     catch (Exception updateEx)
     {
         this.Logger.Warn(updateEx.ToString(), updateEx);
     }
 }
Exemplo n.º 6
0
        public async Task EnqueueAsync <TJob, TArgs>(TArgs args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan?delay = null)
            where TJob : IBackgroundJob <TArgs>
        {
            var jobInfo = new BackgroundJobInfo
            {
                JobType  = typeof(TJob).AssemblyQualifiedName,
                JobArgs  = args.ToJsonString(),
                Priority = priority
            };

            if (delay.HasValue)
            {
                jobInfo.NextTryTime = Clock.Now.Add(delay.Value);
            }

            await this._store.InsertAsync(jobInfo);
        }
Exemplo n.º 7
0
 public Task UpdateAsync(BackgroundJobInfo jobInfo)
 {
     return(Task.FromResult(0));
 }
Exemplo n.º 8
0
 public Task InsertAsync(BackgroundJobInfo jobInfo)
 {
     return(Task.FromResult(0));
 }