Example #1
0
        public Boolean Report(JobLog item)
        {
            if (item == null || item.ID == 0)
            {
                throw new InvalidOperationException("无效操作 JobItemID=" + item?.ID);
            }

            // 判断是否有权
            var app = Session["App"] as App;

            var ji = JobLog.FindByID(item.ID);

            if (ji == null)
            {
                throw new InvalidOperationException($"找不到任务[{item.ID}]");
            }

            var job = Job.FindByID(ji.JobID);

            if (job == null || job.AppID != app.ID)
            {
                XTrace.WriteLine(item.ToJson());
                throw new InvalidOperationException($"应用[{app}]无权操作作业[{job}#{ji}]");
            }

            // 只有部分字段允许客户端修改
            if (item.Status > 0)
            {
                ji.Status = item.Status;
            }

            ji.Row        = item.Row;
            ji.FetchSpeed = item.FetchSpeed;
            ji.Speed      = item.Speed;
            ji.Total      = item.Total;
            ji.Success    = item.Success;
            ji.Cost       = item.Cost;
            ji.Key        = item.Key;
            ji.Message    = item.Message;

            ji.ThreadID = item.ThreadID;

            // 动态调整步进
            if (item.Status == JobStatus.完成)
            {
                AdjustStep(job, ji, Session as IExtend);

                //// 更新积压数
                //UpdateLatency(job, ji);
            }
            // 已终结的作业,汇总统计
            if (item.Status == JobStatus.完成 || item.Status == JobStatus.错误)
            {
                ji.Times++;

                SetJobFinish(job, ji);

                // 记录状态
                UpdateOnline(app, ji, Session as INetSession);
            }
            if (item.Status == JobStatus.错误)
            {
                var ps = ControllerContext.Current.Parameters;

                var err = SetJobError(job, ji, ps);

                ji.Error++;
                //ji.Message = err.Message;

                // 出错时判断如果超过最大错误数,则停止作业
                CheckMaxError(app, job);
            }

            // 从创建到完成的全部耗时
            var ts = DateTime.Now - ji.CreateTime;

            ji.FullCost = (Int32)ts.TotalSeconds;

            ji.SaveAsync();
            //ji.Save();

            return(true);
        }
Example #2
0
        public Int32 Produce(Int32 jobitemid, String topic, String[] messages, Int32 delayTime = 0, Boolean unique = false)
        {
            if (messages == null)
            {
                return(0);
            }
            messages = messages.Distinct().ToArray();
            if (messages.Length == 0)
            {
                return(0);
            }

            var app = Session["App"] as App;

            var ji  = JobLog.FindByID(jobitemid);
            var job = ji?.Job;

            //if (job == null) throw new Exception($"无效作业编号[jobIten={jobitemid}]无效");

            // 去重过滤
            if (unique)
            {
                messages = AppMessage.Filter(app.ID, topic, messages);
                if (messages.Length == 0)
                {
                    return(0);
                }
            }

            var ms = new List <AppMessage>();

            var total = 0;
            var now   = DateTime.Now;
            // 延迟需要基于任务开始时间,而不能用使用当前时间,防止回头跑数据时无法快速执行
            var dTime = delayTime.ToDateTime();

            if (ji != null)
            {
                dTime = ji.Start.AddSeconds(delayTime);
            }
            else if (dTime.Year < 2000)
            {
                dTime = now.AddSeconds(delayTime);
            }

            foreach (var item in messages)
            {
                var jm = new AppMessage
                {
                    AppID = app.ID,
                    //JobID = job.ID,
                    Topic = topic,
                    Data  = item,
                };
                if (job != null)
                {
                    jm.JobID = job.ID;
                }

                jm.CreateTime = jm.UpdateTime = now;
                if (delayTime > 0)
                {
                    jm.UpdateTime = dTime;
                }

                ms.Add(jm);
            }

            // 记录消息积压数
            total = ms.BatchInsert();

            // 增加消息数
            if (total < 0)
            {
                total = messages.Length;
            }
            if (total > 0)
            {
                var job2 = app.Jobs?.FirstOrDefault(e => e.Topic == topic);
                if (job2 != null)
                {
                    job2.MessageCount += total;
                    job2.SaveAsync();
                }

                app.MessageCount += total;
                app.SaveAsync();
            }

            return(total);
        }