public Task InsertAsync(BackgroundJobInfo jobInfo) { jobInfo.Id = jobInfo.Id ?? Guid.NewGuid().ToString();//联锁增量(参考lastid); _jobs[jobInfo.Id] = jobInfo; return(Task.FromResult(0)); }
private void TryProcessJob(BackgroundJobInfo jobInfo) { try { jobInfo.TryCount++; jobInfo.LastTryTime = Clock.Now; var jobType = Type.GetType(jobInfo.JobType); using (var job = _iocResolver.ResolveAsDisposable(jobType)) { try { var jobExecuteMethod = job.Object.GetType().GetTypeInfo().GetMethod("Execute"); var argsType = jobExecuteMethod.GetParameters()[0].ParameterType; var argsObj = JsonConvert.DeserializeObject(jobInfo.JobArgs, argsType); jobExecuteMethod.Invoke(job.Object, new[] { argsObj }); AsyncHelper.RunSync(() => _store.DeleteAsync(jobInfo)); } catch (Exception ex) { Logger.Warn(ex.Message, ex); var nextTryTime = jobInfo.CalculateNextTryTime(); if (nextTryTime.HasValue) { jobInfo.NextTryTime = nextTryTime.Value; } else { jobInfo.IsAbandoned = true; } TryUpdate(jobInfo); EventBus.Trigger( this, new SharePlatformHandledExceptionData( new BackgroundJobException( "后台作业执行失败。有关详细信息,请参阅内部异常。请参阅BackgroundJob属性以获取有关后台作业的信息。", ex) { BackgroundJob = jobInfo, JobObject = job.Object } ) ); } } } catch (Exception ex) { Logger.Warn(ex.ToString(), ex); jobInfo.IsAbandoned = true; TryUpdate(jobInfo); } }
public Task UpdateAsync(BackgroundJobInfo jobInfo) { if (jobInfo.IsAbandoned) { return(DeleteAsync(jobInfo)); } return(Task.FromResult(0)); }
private void TryUpdate(BackgroundJobInfo jobInfo) { try { _store.UpdateAsync(jobInfo); } catch (Exception updateEx) { Logger.Warn(updateEx.ToString(), updateEx); } }
public async Task <bool> DeleteAsync(string jobId) { BackgroundJobInfo jobInfo = await _store.GetAsync(jobId); if (jobInfo == null) { return(false); } await _store.DeleteAsync(jobInfo); return(true); }
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()); }
public Task DeleteAsync(BackgroundJobInfo jobInfo) { _jobs.TryRemove(jobInfo.Id, out _); return(Task.FromResult(0)); }
public Task UpdateAsync(BackgroundJobInfo jobInfo) { return(Task.FromResult(0)); }
public Task InsertAsync(BackgroundJobInfo jobInfo) { return(Task.FromResult(0)); }