服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2024-10-28 c5c41deac02df2ac552f7eb51e8b9b5f5dcc5472
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Rhea.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tiger.IBusiness;
using Tiger.Model;
using Tiger.Model.Minsun;
using FluentScheduler;
using Newtonsoft.Json;
using static Tiger.Model.TrigArgs;
using System.Reflection;
 
namespace Tiger.Business.MES
{
    /// <summary>
    /// 任务相关
    /// </summary>
    public partial class TskJob : ITskJob
    {
        /// <summary>
        /// 保存计划任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction> SaveTskJob(TskParameter input)
        {
            var result = new ApiAction();
            try
            {
                if (input.type == EveryType.Milliseconds && input.ToRunEvery<100)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"时间是毫秒时,至少要大于100毫秒");
                    return result;
                }
                if (Biz.Db.Queryable<TSK_JOB>().Any(x =>
                    (x.JobName != input.JobName && x.JobType == input.JobType) ||
                    (x.JobName == input.JobName && x.JobType != input.JobType)))
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"相同的类名不能有不同的任务名或者相同的任务名不能有不同的类名");
                    return result;
                }
                var _job = Biz.Db.Queryable<TSK_JOB>().Where(x => x.JobName == input.JobName && x.JobType == input.JobType).First() ?? new()
                {
                    JobName = input.JobName,
                    JobType = input.JobType,
                    AssemblyName = input.AssemblyName,
                };
                _job.Remark = input.Remark;
 
                //计划任务参数
                var Args = new TrigArgs()
                {
                    ToRunOnceAtDt = input.ToRunOnceAtDt,
                    NowAddMinutes = input.NowAddMinutes,
                    ToRunEvery = input.ToRunEvery,
                    ToRunOnceIn = input.ToRunOnceIn,
                    NonReentrant = input.NonReentrant,
                    Type = input.type,
                    runType = input.runType,
                    Minutes = input.Minutes,
                    Hours = input.Hours,
                    Days = input.Days,
                    Interval = input.Interval,
                };
                TSK_TRIG tskTrig = Biz.Db.Queryable<TSK_TRIG>().Where(x => x.JobId == _job.ID).First() ?? new()
                {
                    JobId = _job.ID,
                    Status = TSK_TRIG.Statuss.Ready.GetValue(),
                    StartTime = DateTime.Now,
                    NumberOfRuns = 1,
                };
                tskTrig.LastRunTime = input.LastRunTime;
                tskTrig.Args = JsonConvert.SerializeObject(Args);
 
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    var y = db.Storageable(_job)
                       .WhereColumns(t => new { t.JobName, t.JobType, t.GHOST_ROW })
                       .ToStorage();
                    y.AsInsertable.ExecuteCommand();
                    y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
 
                    var z = db.Storageable(tskTrig)
                       .WhereColumns(t => new { t.JobId, t.GHOST_ROW })
                       .ToStorage();
                    z.AsInsertable.ExecuteCommand();
                    z.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.Message = $"保存计划任务异常";
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "保存计划任务异常");
            }
            return await Task.FromResult(result);
        }
    } //endClass
}