服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2024-10-27 6f3b05c06125b457d0c78e259f3befe4910881cf
计划任务更新
已修改6个文件
已重命名1个文件
175 ■■■■■ 文件已修改
Tiger.Business.MES/Common/WoContext.cs 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
Tiger.Business/Services/Base/InterfaceService.cs 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Tiger.Business/Services/Base/InterfaceServiceNew.cs 78 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Tiger.Controllers.System/Controllers/TSK/TskController.Base.cs 67 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Tiger.IBusiness/Service/IInterfaceService.cs 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Tiger.IBusiness/Service/ITigerJob.cs 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
Tiger.Model.Net/Entitys/TSK/TSK_JOB.cs 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Tiger.Business.MES/Common/WoContext.cs
@@ -50,7 +50,7 @@
        /// <returns></returns>
        public static bool ExistsBatch(string workorder, string lineCode, string batchNo = "", bool canDoWork = false)
        {
            return WoBatchDic.WhereIF(canDoWork, q => q.Value.Batch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Release.GetValue() || q.Value.Batch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Working.GetValue())
            return WoBatchDic.WhereIF(canDoWork, q => !q.Value.Batch.IsNullOrEmpty() && (q.Value.Batch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Release.GetValue() || q.Value.Batch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Working.GetValue()))
                .Any(q => q.Value.Batch.ORDER_NO == workorder && q.Value.Batch.ACT_LINE == lineCode && (batchNo.IsNullOrEmpty() || q.Value.Batch.BATCH_NO == batchNo));
        }
Tiger.Business/Services/Base/InterfaceService.cs
@@ -175,5 +175,15 @@
        {
            JobManager.Initialize();
        }
        ApiAction IInterfaceService.AddTskJob<TJob>(TJob newEntity, FluentJobParam jobParam)
        {
            throw new NotImplementedException();
        }
        public void RemovefJob(string jobname)
        {
            throw new NotImplementedException();
        }
    }
}
Tiger.Business/Services/Base/InterfaceServiceNew.cs
@@ -1,6 +1,7 @@
using FluentScheduler;
using Newtonsoft.Json;
using Rhea.Common;
using Sundial;
using System;
using System.Collections;
using System.Collections.Generic;
@@ -277,7 +278,8 @@
            SaveRunningStatus(); //保存状态
        }
        private void SaveRunningStatus() {
        private void SaveRunningStatus()
        {
            List<TSK_TRIG> listTrig = new List<TSK_TRIG>();
            foreach (var schedule in JobManager.RunningSchedules)
            {
@@ -313,58 +315,59 @@
        /// <param name="newEntity"></param>
        /// <param name="jobParam"></param>
        /// <returns></returns>
        public ApiAction AddJob<TJob>(TJob newEntity, FluentJobParam jobParam) where TJob : class, ITJob
        public ApiAction AddTskJob<TJob>(TJob newEntity, FluentJobParam jobParam) where TJob : class, ITJob
        {
            ApiAction apiAction = new();
            try
            {
                JobManager.AddJob<TJob>((s) => GenerateSchedule(jobParam.Args, s.WithName(typeof(TJob).Name)));
                string guid = Guid.NewGuid().ToString("N");
                var trigger = new TSK_TRIG()
                if (!JobManager.AllSchedules.Any(q => q.Name == jobParam.JobName))
                {
                    JobId = guid,
                    Args = JsonConvert.SerializeObject(jobParam.Args),
                    Status = TSK_TRIG.Statuss.Running.GetValue(),
                    StartTime = DateTime.Now,
                    NumberOfRuns = 1,
                };
                TSK_JOB job = new()
                    JobManager.AddJob<TJob>((s) => GenerateSchedule(jobParam.Args, s.WithName(jobParam.JobName)));
                    string guid = Guid.NewGuid().ToString("N");
                    TSK_JOB job = Biz.Db.Queryable<TSK_JOB>().Where(x => x.JobName == jobParam.JobName).First() ?? new()
                {
                    ID = guid,
                    Remark = jobParam.Remark,
                    JobType = typeof(TJob).Name,
                        JobType = jobParam.JobName,
                    AssemblyName = jobParam.AssemblyName,
                    JobName = jobParam.JobName,
                };
                if (Biz.Db.Queryable<TSK_JOB>().Any(q=>q.JobName == typeof(TJob).Name)) {
                    apiAction.IsSuccessed = false;
                    apiAction.LocaleMsg = new($"任务已经存在");
                    return apiAction;
                }
                    var trigger = Biz.Db.Queryable<TSK_TRIG>().Where(x => x.JobId == job.ID).First() ?? new TSK_TRIG()
                    {
                        JobId = guid,
                        StartTime = DateTime.Now,
                        NumberOfRuns = 1,
                    };
                    trigger.Args = JsonConvert.SerializeObject(jobParam.Args);
                    trigger.Status = TSK_TRIG.Statuss.Running.GetValue();
                var db = Biz.Db;
                //保存到数据库
                var dbTran = db.UseTran(() =>
                {
                    if (job != null)
                    {
                        db.Insertable(job, "system").ExecuteCommand();
                    }
                    if (trigger != null)
                    {
                        db.Insertable(trigger, "system").ExecuteCommand();
                    }
                        var y = db.Storageable(job)
                           .WhereColumns(t => new { t.JobName, t.GHOST_ROW })
                           .ToStorage();
                        y.AsInsertable.ExecuteCommand();
                        y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                        var z = db.Storageable(trigger)
                           .WhereColumns(t => new { t.JobId, t.GHOST_ROW })
                           .ToStorage();
                        z.AsInsertable.ExecuteCommand();
                        z.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    Logger.Scheduler.Trace(dbTran.ErrorException, $"添加工作任务时保存到数据库异常");
                        apiAction.CatchExceptionWithLog(dbTran.ErrorException, $"添加工作任务时保存到数据库异常");
                }
                //apiAction.Data = entity;
                }
            }
            catch (System.Exception ex)
            {
                Logger.Console.Fatal(ex, "Add Job From DB Exception");
                apiAction.CatchExceptionWithLog(ex);
            }
            if (!apiAction.IsSuccessed) { JobManager.RemoveJob(jobParam.JobName); }
            return apiAction;
        }
@@ -374,6 +377,23 @@
        /// <param name="jobname"></param>
        public void RemovefJob(string jobname)
        {
            if (JobManager.AllSchedules.Any(q => q.Name == jobname))
            {
                string guid = Guid.NewGuid().ToString("N");
                TSK_JOB job = Biz.Db.Queryable<TSK_JOB>().Where(x => x.JobName == jobname).First();
                var trigger = Biz.Db.Queryable<TSK_TRIG>().Where(x => x.JobId == job.ID).First();
                trigger.Status = TSK_TRIG.Statuss.Stop.GetValue();
                var db = Biz.Db;
                //保存到数据库
                var dbTran = db.UseTran(() =>
                {
                    db.Updateable(trigger, "system_job").UpdateColumns(q=>new { q.Status,q.UPDATE_TIME,q.UPDATE_USER}).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    Logger.Scheduler.Fatal(dbTran.ErrorException, $"添加工作任务时保存到数据库异常");
                }
            }
            JobManager.RemoveJob(jobname);
        }
Tiger.Controllers.System/Controllers/TSK/TskController.Base.cs
@@ -29,27 +29,6 @@
            return Ok(response);
        }
        /// <summary>
        /// æ ¹æ®Job实体名称和Job名称添加任务
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/[controller]/[action]")]
        public async Task<IActionResult> AddJob(ApiAction<FluentJobParam> action)
        {
            ApiAction response = new();
            try
            {
                response = response.GetResponse(DI.Resolve<IBizContext>().GetInterfaceService()?.AddJob(action.NewDataEntity(), action.Data) ?? new ApiAction($"任务未启用", false));
            }
            catch (System.Exception ex)
            {
                response = response.GetResponse().CatchExceptionWithLog(ex);
            }
            return Ok(response);
        }
        [HttpPost]
        [Route("api/[controller]/[action]")]
        public async Task<IActionResult> UpdateJob(ApiAction<TSK_JOB> action)
@@ -189,5 +168,51 @@
            }
            return Ok(response);
        }
        #region æ–°ç‰ˆè®¡åˆ’任务
        /// <summary>
        /// æ ¹æ®Job实体名称和Job名称添加任务
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/[controller]/[action]")]
        public async Task<IActionResult> AddTskJob(ApiAction<FluentJobParam> action)
        {
            ApiAction response = new();
            try
            {
                response = response.GetResponse(DI.Resolve<IBizContext>().GetInterfaceService()?.AddTskJob(action.NewDataEntity(), action.Data) ?? new ApiAction($"任务未启用", false));
            }
            catch (System.Exception ex)
            {
                response = response.GetResponse().CatchExceptionWithLog(ex);
            }
            return Ok(response);
        }
        /// <summary>
        /// åˆ é™¤ä½œä¸š
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/[controller]/[action]")]
        public async Task<IActionResult> RemoveTskJob(ApiAction action)
        {
            ApiAction response = new();
            try
            {
                DI.Resolve<IBizContext>().GetInterfaceService()?.RemovefJob(action.Data?.ToString());
            }
            catch (System.Exception ex)
            {
                response = response.GetResponse().CatchExceptionWithLog(ex);
            }
            return Ok(response);
        }
        #endregion
    }
}
Tiger.IBusiness/Service/IInterfaceService.cs
@@ -1,4 +1,5 @@
using Rhea.Common;
using FluentScheduler;
using Rhea.Common;
using Sundial;
using System;
using System.Collections.Generic;
@@ -11,16 +12,18 @@
{
    public interface IInterfaceService:IDependency
    {
        public void SetSchedulerFactory(ISchedulerFactory schedulerFactory);
        public ApiAction AddJob<TJob>(TJob newEntity, TskJobParam jobParam) where TJob : class, IJob;
        public ApiAction AddJob<TJob>(string jobname, params TriggerBuilder[] triggerBuilders) where TJob : class, IJob;
        public void SetSchedulerFactory(Sundial.ISchedulerFactory schedulerFactory);
        public ApiAction AddJob<TJob>(TJob newEntity, TskJobParam jobParam) where TJob : class, Sundial.IJob;
        public ApiAction AddTskJob<TJob>(TJob newEntity, FluentJobParam jobParam) where TJob : class, ITJob;
        public ApiAction AddJob<TJob>(string jobname, params Sundial.TriggerBuilder[] triggerBuilders) where TJob : class, Sundial.IJob;
        public ApiAction UpdateJob(TSK_JOB job);
        public void StartAllJob();
        public void StartJob(string jobname);
        public IScheduler GetJob(string jobname);
        public List<IScheduler> GetJobs();
        public Sundial.IScheduler GetJob(string jobname);
        public List<Sundial.IScheduler> GetJobs();
        public void PauseJob(string jobname);
        public void RemoveJob(string jobname);
        public void RemovefJob(string jobname);
        public void AddJobFromDB();
        public void JobInitialize();
    }
Tiger.IBusiness/Service/ITigerJob.cs
ÎļþÃû´Ó Tiger.Business/Services/Base/ITigerJob.cs ÐÞ¸Ä
@@ -6,7 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
namespace Tiger.Business
namespace Tiger.IBusiness
{
    /// <summary>
    /// Sundial任务基类
Tiger.Model.Net/Entitys/TSK/TSK_JOB.cs
@@ -161,6 +161,7 @@
    public class FluentJobParam: TskJobParam
    {
        public string AssemblyName { get; set; }
        public string DataType { get; set; }
        /// <summary>
        /// å‚æ•°