示例#1
0
        public void OnPerforming(PerformingContext filterContext)
        {
            //设置新的分布式锁,分布式锁会阻止两个相同的任务并发执行,用方法名称和JobName
            var jobresource = $"{filterContext.BackgroundJob.Job.Method.Name}.{filterContext.BackgroundJob.Job.Args[1]}";
            var locktimeout = TimeSpan.FromSeconds(_timeoutInSeconds);

            try
            {
                var jobItem = filterContext.BackgroundJob.Job.Args.FirstOrDefault();
                var job     = jobItem as HttpJobItem;
                if (job != null)
                {
                    var isCronJob = !string.IsNullOrEmpty(job.Cron);
                    var key       = isCronJob ? job.JobName : filterContext.BackgroundJob.Id;
                    var conts     = filterContext.Connection.GetAllItemsFromSet($"JobPauseOf:{key}");
                    if (conts.Contains("true"))
                    {
                        filterContext.Canceled = true;//任务被暂停不执行直接跳过
                        return;
                    }

                    if (!string.IsNullOrEmpty(job.JobName))
                    {
                        filterContext.BackgroundJob.Id.AddTags(job.JobName);
                    }
                }

                //设置运行时被设置的参数
                try
                {
                    var hashKey        = CodingUtil.MD5(filterContext.BackgroundJob.Id + ".runtime");
                    var excuteDataList = filterContext.Connection.GetAllEntriesFromHash(hashKey);
                    if (excuteDataList.Any())
                    {
                        filterContext.SetJobParameter("runtimeKey", hashKey);
                        foreach (var keyvalue in excuteDataList)
                        {
                            filterContext.SetJobParameter(keyvalue.Key, keyvalue.Value);
                        }
                    }
                }
                catch (Exception)
                {
                    //ignore
                }

                //申请分布式锁
                var distributedLock = filterContext.Connection.AcquireDistributedLock(jobresource, locktimeout);
                filterContext.Items["DistributedLock"] = distributedLock;
            }
            catch (Exception ec)
            {
                filterContext.Canceled = true;
                logger.Info($"[OnPerforming] BackgroundJob.Job.JObName:{filterContext.BackgroundJob.Job.Args[1]} AcquireDistributedLock Timeout,BackgroundJob.Id:{filterContext.BackgroundJob.Id},Exception:{ec}");
            }
        }
        void IServerFilter.OnPerforming(PerformingContext filterContext)
        {
            var jobId       = this.GetJobIdentifier(filterContext.BackgroundJob.Job);
            var connection  = JobStorage.Current.GetConnection();
            var hashEntries = connection.GetAllEntriesFromHash(jobId);

            if (hashEntries is null)
            {
                return;
            }

            if (hashEntries.TryGetValue(ParameterName, out string lastSuccessUtcString) &&
                DateTime.TryParseExact(lastSuccessUtcString, "O", CultureInfo.InvariantCulture, DateTimeStyles.None, out var lastSuccess))
            {
                filterContext.SetJobParameter(ParameterName, lastSuccess);
            }
        }
示例#3
0
        public void OnPerforming(PerformingContext filterContext)
        {
            Assert.NotNull(filterContext);

            if (_cancelsTheCreation)
            {
                filterContext.Canceled = true;
            }

            _results.Add(String.Format("{0}::{1}", _name, "OnPerforming"));

            if (_setOnPreMethodParameters != null)
            {
                foreach (var parameter in _setOnPreMethodParameters)
                {
                    filterContext.SetJobParameter(parameter.Key, parameter.Value);
                }
            }

            if (_readParameters != null)
            {
                foreach (var parameter in _readParameters)
                {
                    Assert.Equal(
                        parameter.Value,
                        filterContext.GetJobParameter<string>(parameter.Key));
                }
            }
            
            if (_throwException)
            {
                throw new Exception();
            } 
        }