Example #1
0
        private void ProcessQueuedItems(object ignored)
        {
            while (true)
            {
                JobMessage job;
                lock (_jobs)
                {
                    if (_jobs.Count == 0)
                    {
                        _delegateQueuedOrRunning = false;
                        break;
                    }

                    job = _jobs.Dequeue();
                }

                XxlJobUtilExtensions.RunSync(async() =>
                {
                    try
                    {
                        var properies = new Dictionary <string, object>
                        {
                            [XxlJobConstant.LOGGER_SCOPE_JOBID_KEY]    = job.Id,
                            [XxlJobConstant.LOGGER_SCOPE_JOBLOGID_KEY] = job.LogId,
                            [XxlJobConstant.LOGGER_SCOPE_JOBAREA_KEY]  = XxlJobConstant.LOGGER_SCOPE_JOBAREA_VALUE
                        };
                        using (_logger.BeginScope(properies))
                        {
                            var result = await _sender.Execute(job);

                            if (result.Code == XxlJobConstant.HTTP_FAIL_CODE)
                            {
                                _logger.LogError($"执行失败. Id:{job.Id}, Msg: {result.Msg}");
                            }

                            job.CallBackCode = result.Code;
                            job.Reason       = result.Content.SerializeObject();
                        }
                        await _sender.CallBack(new List <JobMessage>()
                        {
                            job
                        });
                    }
                    catch (Exception ex)
                    {
                        var failedInfo = new FailedInfo()
                        {
                            ServiceProvider = _serviceProvider,
                            Exception       = ex,
                            Job             = job,
                            Message         = $"执行异常. Id:{job.Id}"
                        };

                        _options?.FailedCallback?.Invoke(failedInfo);
                        _logger.LogError(ex, failedInfo.Message);
                    }
                });
            }
        }
Example #2
0
        public Task <bool> Kill(int jobId, string killedReason)
        {
            var killedJobs = new List <JobMessage>();

            if (_executingJobs.TryGetValue(jobId, out XxlJobQueue queue))
            {
                killedJobs = queue.Kill(killedReason);
            }

            if (killedJobs.Count > 0)
            {
                Task.Run(async() => await _sender.CallBack(killedJobs));
            }

            return(Task.FromResult(killedJobs.Count > 0));
        }