示例#1
0
        /// <summary>申请历史错误或中断的任务</summary>
        /// <param name="server">申请任务的服务端</param>
        /// <param name="ip">申请任务的IP</param>
        /// <param name="pid">申请任务的服务端进程ID</param>
        /// <param name="count">要申请的任务个数</param>
        /// <returns></returns>
        public IList <JobTask> AcquireOld(String server, String ip, Int32 pid, Int32 count)
        {
            lock (this)
            {
                using (var ts = Meta.CreateTrans())
                {
                    var list = new List <JobTask>();

                    // 查找历史错误任务
                    if (ErrorDelay > 0)
                    {
                        var dt    = DateTime.Now.AddSeconds(-ErrorDelay);
                        var list2 = JobTask.Search(ID, dt, MaxRetry, new[] { JobStatus.错误, JobStatus.取消 }, count);
                        if (list2.Count > 0)
                        {
                            list.AddRange(list2);
                        }
                    }

                    // 查找历史中断任务,持续10分钟仍然未完成
                    if (MaxTime > 0 && list.Count < count)
                    {
                        var dt    = DateTime.Now.AddSeconds(-MaxTime);
                        var list2 = JobTask.Search(ID, dt, MaxRetry, new[] { JobStatus.就绪, JobStatus.抽取中, JobStatus.处理中 }, count - list.Count);
                        if (list2.Count > 0)
                        {
                            list.AddRange(list2);
                        }
                    }
                    if (list.Count > 0)
                    {
                        foreach (var ti in list)
                        {
                            ti.Server    = server;
                            ti.ProcessID = pid;
                            ti.Client    = $"{ip}@{pid}";
                            //ti.Status = JobStatus.就绪;
                            ti.CreateTime = DateTime.Now;
                            ti.UpdateTime = DateTime.Now;
                        }
                        list.Save();
                    }

                    ts.Commit();

                    return(list);
                }
            }
        }
示例#2
0
        /// <summary>申请历史错误或中断的任务</summary>
        /// <param name="server">申请任务的服务端</param>
        /// <param name="ip">申请任务的IP</param>
        /// <param name="pid">申请任务的服务端进程ID</param>
        /// <param name="count">要申请的任务个数</param>
        /// <param name="cache">用于设置全局锁的缓存对象</param>
        /// <returns></returns>
        public IList <JobTask> AcquireOld(String server, String ip, Int32 pid, Int32 count, ICache cache)
        {
            //// 全局锁,确保单个作业只有一个线程在分配作业
            //using var ck = cache.AcquireLock($"Job:{ID}", 5_000);

            using var ts = Meta.CreateTrans();
            var list = new List <JobTask>();

            // 查找历史错误任务
            if (ErrorDelay > 0)
            {
                var dt    = DateTime.Now.AddSeconds(-ErrorDelay);
                var list2 = JobTask.Search(ID, dt, MaxRetry, new[] { JobStatus.错误, JobStatus.取消 }, count);
                if (list2.Count > 0)
                {
                    list.AddRange(list2);
                }
            }

            // 查找历史中断任务,持续10分钟仍然未完成
            if (MaxTime > 0 && list.Count < count)
            {
                var dt    = DateTime.Now.AddSeconds(-MaxTime);
                var list2 = JobTask.Search(ID, dt, MaxRetry, new[] { JobStatus.就绪, JobStatus.抽取中, JobStatus.处理中 }, count - list.Count);
                if (list2.Count > 0)
                {
                    list.AddRange(list2);
                }
            }
            if (list.Count > 0)
            {
                foreach (var ti in list)
                {
                    ti.Server    = server;
                    ti.ProcessID = Interlocked.Increment(ref _idxBatch);
                    ti.Client    = $"{ip}@{pid}";
                    //ti.Status = JobStatus.就绪;
                    ti.CreateTime = DateTime.Now;
                    ti.UpdateTime = DateTime.Now;
                }
                list.Save();
            }

            ts.Commit();

            return(list);
        }