/// <summary>处理一个数据对象</summary> /// <param name="ctx">上下文</param> /// <param name="entity"></param> /// <returns></returns> protected virtual Boolean ProcessItem(JobContext ctx, IEntity entity) => true;
/// <summary>已完成</summary> /// <param name="ctx"></param> internal protected virtual void OnFinish(JobContext ctx) => _timer?.SetNext(-1);
/// <summary>执行Sql集合</summary> /// <param name="sections"></param> /// <param name="ctx"></param> /// <param name="callback"></param> /// <returns></returns> public static Int32 ExecuteSql(SqlSection[] sections, JobContext ctx, Action <SqlSection, DbTable> callback = null) { if (sections == null || sections.Length == 0) { return(-1); } var rs = 0; ctx.Total = 0; // 打开事务 foreach (var item in sections) { if (item.Action != SqlActions.Query) { DAL.Create(item.ConnName).BeginTransaction(); } } try { // 按顺序执行处理Sql语句 DbTable dt = null; foreach (var section in sections) { switch (section.Action) { case SqlActions.Query: dt = section.Query(); if (dt != null) { ctx.Total += dt.Rows.Count; } // 处理生产消息 callback?.Invoke(section, dt); break; case SqlActions.Execute: rs += section.Execute(); break; case SqlActions.Insert: if (dt.Rows.Count > 0) { rs += section.BatchInsert(dt); } break; default: break; } } // 提交事务 foreach (var item in sections) { if (item.Action != SqlActions.Query) { DAL.Create(item.ConnName).Commit(); } } } catch { // 回滚事务 foreach (var item in sections) { if (item.Action != SqlActions.Query) { DAL.Create(item.ConnName).Rollback(); } } throw; } return(rs); }
/// <summary>任务出错,每个任务多次调用</summary> /// <param name="ctx">上下文</param> public virtual void Error(JobContext ctx) { }
/// <summary>完成任务,每个任务只调用一次</summary> /// <param name="ctx">上下文</param> public virtual void Finish(JobContext ctx) { }
/// <summary>报告进度,每个任务多次调用</summary> /// <param name="ctx">上下文</param> public virtual void Report(JobContext ctx) { }