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

            return(Task.FromResult(0));
        }
Ejemplo n.º 2
0
        public Task UpdateAsync(BackgroundJobInfo jobInfo)
        {
            if (jobInfo.IsAbandoned)
            {
                return(DeleteAsync(jobInfo));
            }

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

                var jobType = Type.GetType(jobInfo.JobType);
                var job     = _serviceProvider.GetService(jobType);
                try
                {
                    var jobExecuteMethod = job.GetType().GetTypeInfo().GetMethod("Execute");
                    var argsType         = jobExecuteMethod.GetParameters()[0].ParameterType;
//                    var argsObj = _serializer.Deserialize(jobInfo.JobArgs, argsType);
                    var argsObj = JsonConvert.DeserializeObject(jobInfo.JobArgs, argsType);

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

                    AsyncHelper.RunSync(() => _store.DeleteAsync(jobInfo));
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.Message, ex);

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

                    TryUpdate(jobInfo);

                    _eventBus.Publish(
                        new HandleEventException(
                            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,
                        JobObject     = job
                    }));
                }
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex.ToString(), ex);

                jobInfo.IsAbandoned = true;

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

            _jobs.TryRemove(jobInfo.Id, out _);

            return(Task.FromResult(0));
        }
Ejemplo n.º 5
0
 private void TryUpdate(BackgroundJobInfo jobInfo)
 {
     try
     {
         _store.UpdateAsync(jobInfo);
     }
     catch (Exception updateEx)
     {
         _logger.LogWarning(updateEx.ToString(), updateEx);
     }
 }
Ejemplo n.º 6
0
        public async Task <string> 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 _store.InsertAsync(jobInfo);

            return(jobInfo.Id.ToString());
        }
Ejemplo n.º 7
0
 public Task UpdateAsync(BackgroundJobInfo jobInfo)
 {
     return(Task.FromResult(0));
 }
Ejemplo n.º 8
0
 public Task InsertAsync(BackgroundJobInfo jobInfo)
 {
     return(Task.FromResult(0));
 }