/// <summary>
 /// 移除任务
 /// </summary>
 /// <param name="jobInfo"></param>
 public void RemoveJobInfo(JobRunRequest jobInfo)
 {
     if (ExecutingJobs.TryGetValue(jobInfo.jobId, out ConcurrentHashSet <JobRunRequest> sets))
     {
         sets.TryRemove(jobInfo);
     }
 }
Beispiel #2
0
        /// <summary>
        /// 异步执行任务 把任务插入线程任务队列中排队执行
        /// </summary>
        /// <param name="jobInfo"></param>
        /// <param name="jobHandler"></param>
        /// <returns></returns>
        private async Task AsyncExecuteJob(JobRunRequest jobInfo, IJobHandler jobHandler)
        {
            Func <object, Task> action = async(state) =>
            {
                if (jobInfo.JobStatus == JobStatus.Killed)  //已终止的任务 就不要再运行了
                {
                    _logger.LogInformation($"**************该任务已被终止 {jobInfo.jobId},{jobInfo.logId}********************");
                    return;
                }

                jobInfo.SetRunning();
                ReturnT executeResult = null;
                try
                {
                    executeResult = await jobHandler.Execute(new JobExecuteContext(jobInfo.logId, jobInfo.executorParams));
                }
                catch (Exception ex)
                {
                    executeResult = ReturnT.Failed(ex.StackTrace + "————" + ex.Message);
                    _logger.LogError(ex, "xxljob执行任务错误");
                }

                _xxlJobExecutor.RemoveJobInfo(jobInfo);
                await CallBack(jobInfo.logId, executeResult);  //这里保证一定要回调结果 不然就要重试了(配置了重试次数),这里回调为失败结果也会重试(配置了重试次数)
            };

            _xxlJobExecutor.RegistJobInfo(jobInfo);
            //插入任务执行队列中 根据jobid路由到固定线程中 保证同一个jobid串行执行
            _taskExecutor.GetSingleThreadTaskExecutor(jobInfo.jobId).Execute(action, null);
            await Task.CompletedTask;
        }
        /// <summary>
        /// 注册任务
        /// </summary>
        /// <param name="jobInfo"></param>
        public void RegistJobInfo(JobRunRequest jobInfo)
        {
            if (ExecutingJobs.ContainsKey(jobInfo.jobId))
            {
                ExecutingJobs[jobInfo.jobId].TryAdd(jobInfo);
                return;
            }

            lock (ExecutingJobs)
            {
                if (ExecutingJobs.ContainsKey(jobInfo.jobId))
                {
                    ExecutingJobs[jobInfo.jobId].TryAdd(jobInfo);
                }
                else
                {
                    var set = new ConcurrentHashSet <JobRunRequest>();
                    set.TryAdd(jobInfo);
                    ExecutingJobs.TryAdd(jobInfo.jobId, set);
                }
            }
        }
 public JobMessage(JobRunRequest request)
 {
     _request = request;
 }