1
0
Преглед на файлове

任务服务添加新增,暂停,启动,立即运行任务功能
新增任务接口初始化数据

zhontai преди 2 години
родител
ревизия
1f4a35599e

+ 3 - 3
src/hosts/ZhonTai.Host/Configs/dbconfig.json

@@ -19,14 +19,14 @@
   "createDbSql": "CREATE DATABASE `admindb` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci'",
 
   //同步结构
-  "syncStructure": true,
+  "syncStructure": false,
   //同步数据
-  "syncData": true,
+  "syncData": false,
 
   //项目初始化不开启生成数据,发布生产环境前,如果开发环境有配置数据需要更新数据包,可以开启生成数据包,使用完记得关闭
   //开启生成数据前先关闭syncStructure syncData createDb
   //生成数据
-  "generateData": false,
+  "generateData": true,
 
   //数据库配置 https://github.com/dotnetcore/FreeSql/wiki/入门
   //数据库类型 MySql = 0, SqlServer = 1, PostgreSQL = 2, Oracle = 3, Sqlite = 4, OdbcOracle = 5, OdbcSqlServer = 6, OdbcMySql = 7, OdbcPostgreSQL = 8, Odbc = 9, OdbcDameng = 10, MsAccess = 11, Dameng = 12, OdbcKingbaseES = 13, ShenTong = 14, KingbaseES = 15, Firebird = 16

+ 26 - 14
src/hosts/ZhonTai.Host/InitData/Admin/ad_api.json

@@ -708,7 +708,6 @@
         "path": "/api/admin/user/get-login-user",
         "httpMethods": "get",
         "description": "",
-        "enabled": true,
         "childs": []
       },
       {
@@ -1182,7 +1181,6 @@
     "label": "员工管理",
     "path": "staff",
     "description": "",
-    "enabled": true,
     "childs": [
       {
         "id": 174219034734666,
@@ -1191,7 +1189,6 @@
         "path": "/api/admin/staff/get",
         "httpMethods": "get",
         "description": "",
-        "enabled": true,
         "childs": []
       },
       {
@@ -1201,7 +1198,6 @@
         "path": "/api/admin/staff/get-page",
         "httpMethods": "post",
         "description": "",
-        "enabled": true,
         "childs": []
       },
       {
@@ -1211,7 +1207,6 @@
         "path": "/api/admin/staff/add",
         "httpMethods": "post",
         "description": "",
-        "enabled": true,
         "childs": []
       },
       {
@@ -1221,7 +1216,6 @@
         "path": "/api/admin/staff/update",
         "httpMethods": "put",
         "description": "",
-        "enabled": true,
         "childs": []
       },
       {
@@ -1231,7 +1225,6 @@
         "path": "/api/admin/staff/soft-delete",
         "httpMethods": "delete",
         "description": "",
-        "enabled": true,
         "childs": []
       },
       {
@@ -1241,7 +1234,6 @@
         "path": "/api/admin/staff/batch-soft-delete",
         "httpMethods": "put",
         "description": "",
-        "enabled": true,
         "childs": []
       }
     ]
@@ -1286,7 +1278,7 @@
       {
         "id": 174219034738758,
         "parentId": 174219034718277,
-        "label": "批量删除",
+        "label": "删除",
         "path": "/api/admin/org/soft-delete",
         "httpMethods": "delete",
         "description": "",
@@ -1463,7 +1455,7 @@
       {
         "id": 336555241427016,
         "parentId": 336555241193541,
-        "label": "彻底删除",
+        "label": "删除任务",
         "path": "/api/admin/task/delete",
         "httpMethods": "delete",
         "description": "",
@@ -1471,11 +1463,31 @@
         "childs": []
       },
       {
-        "id": 336555241427017,
+        "id": 343342310699077,
         "parentId": 336555241193541,
-        "label": "批量彻底删除",
-        "path": "/api/admin/task/batch-delete",
-        "httpMethods": "put",
+        "label": "暂停任务",
+        "path": "/api/admin/task/pause",
+        "httpMethods": "post",
+        "description": "",
+        "enabled": true,
+        "childs": []
+      },
+      {
+        "id": 343342310703173,
+        "parentId": 336555241193541,
+        "label": "启动任务",
+        "path": "/api/admin/task/resume",
+        "httpMethods": "post",
+        "description": "",
+        "enabled": true,
+        "childs": []
+      },
+      {
+        "id": 343342310703174,
+        "parentId": 336555241193541,
+        "label": "执行任务",
+        "path": "/api/admin/task/run",
+        "httpMethods": "post",
         "description": "",
         "enabled": true,
         "childs": []

+ 102 - 1
src/hosts/ZhonTai.Host/InitData/Admin/ad_permission.json

@@ -970,7 +970,108 @@
             "newWindow": false,
             "external": false,
             "sort": 0,
-            "description": ""
+            "description": "",
+            "childs": [
+              {
+                "id": 343342945812549,
+                "parentId": 342340463398981,
+                "label": "查询任务",
+                "code": "api:admin:task:get",
+                "type": 3,
+                "enabled": true,
+                "sort": 0,
+                "description": "",
+                "childs": []
+              },
+              {
+                "id": 343343120306245,
+                "parentId": 342340463398981,
+                "label": "查询任务列表",
+                "code": "api:admin:task:get-page",
+                "type": 3,
+                "enabled": true,
+                "sort": 0,
+                "description": "",
+                "childs": []
+              },
+              {
+                "id": 343343209164869,
+                "parentId": 342340463398981,
+                "label": "新增",
+                "code": "api:admin:task:add",
+                "type": 3,
+                "enabled": true,
+                "sort": 0,
+                "description": "",
+                "childs": []
+              },
+              {
+                "id": 343343735894085,
+                "parentId": 342340463398981,
+                "label": "修改",
+                "code": "api:admin:task:update",
+                "type": 3,
+                "enabled": true,
+                "sort": 0,
+                "description": "",
+                "childs": []
+              },
+              {
+                "id": 343343926595653,
+                "parentId": 342340463398981,
+                "label": "暂停任务",
+                "code": "api:admin:task:pause",
+                "type": 3,
+                "enabled": true,
+                "sort": 0,
+                "description": "",
+                "childs": []
+              },
+              {
+                "id": 343343989960773,
+                "parentId": 342340463398981,
+                "label": "启动任务",
+                "code": "api:admin:task:resume",
+                "type": 3,
+                "enabled": true,
+                "sort": 0,
+                "description": "",
+                "childs": []
+              },
+              {
+                "id": 343344066166853,
+                "parentId": 342340463398981,
+                "label": "执行任务",
+                "code": "api:admin:task:run",
+                "type": 3,
+                "enabled": true,
+                "sort": 0,
+                "description": "",
+                "childs": []
+              },
+              {
+                "id": 343344158351429,
+                "parentId": 342340463398981,
+                "label": "删除任务",
+                "code": "api:admin:task:delete",
+                "type": 3,
+                "enabled": true,
+                "sort": 0,
+                "description": "",
+                "childs": []
+              },
+              {
+                "id": 343344363630661,
+                "parentId": 342340463398981,
+                "label": "查询任务日志",
+                "code": "api:admin:task-log:get-page",
+                "type": 3,
+                "enabled": true,
+                "sort": 0,
+                "description": "",
+                "childs": []
+              }
+            ]
           }
         ]
       },

+ 50 - 0
src/hosts/ZhonTai.Host/InitData/Admin/ad_permission_api.json

@@ -613,5 +613,55 @@
     "id": 340890633531462,
     "permissionId": 340890633515077,
     "apiId": 336555241422920
+  },
+  {
+    "id": 343342945849413,
+    "permissionId": 343342945812549,
+    "apiId": 336555241422922
+  },
+  {
+    "id": 343343121375302,
+    "permissionId": 343343120306245,
+    "apiId": 336555241427013
+  },
+  {
+    "id": 343343209173062,
+    "permissionId": 343343209164869,
+    "apiId": 336555241427014
+  },
+  {
+    "id": 343343795257413,
+    "permissionId": 343343735894085,
+    "apiId": 336555241422922
+  },
+  {
+    "id": 343343795261509,
+    "permissionId": 343343735894085,
+    "apiId": 336555241427015
+  },
+  {
+    "id": 343343926603846,
+    "permissionId": 343343926595653,
+    "apiId": 343342310699077
+  },
+  {
+    "id": 343343989968966,
+    "permissionId": 343343989960773,
+    "apiId": 343342310703173
+  },
+  {
+    "id": 343344066175046,
+    "permissionId": 343344066166853,
+    "apiId": 343342310703174
+  },
+  {
+    "id": 343344158363718,
+    "permissionId": 343344158351429,
+    "apiId": 336555241427016
+  },
+  {
+    "id": 343344474333253,
+    "permissionId": 343344363630661,
+    "apiId": 336555241427018
   }
 ]

+ 0 - 5
src/platform/ZhonTai.Admin/Services/TaskScheduler/Dto/TaskAddInput.cs

@@ -31,9 +31,4 @@ public class TaskAddInput
     /// 定时参数值 60,60,60,120,120,1200,1200
     /// </summary>
     public string IntervalArgument { get; set; }
-
-    /// <summary>
-    /// 任务状态
-    /// </summary>
-    public TaskStatus Status { get; set; }
 }

+ 7 - 3
src/platform/ZhonTai.Admin/Services/TaskScheduler/ITaskService.cs

@@ -14,11 +14,15 @@ public interface ITaskService
 
     Task<IResultOutput> GetPageAsync(PageInput<TaskGetPageDto> input);
 
-    Task<IResultOutput> AddAsync(TaskAddInput input);
+    IResultOutput Add(TaskAddInput input);
 
     Task<IResultOutput> UpdateAsync(TaskUpdateInput input);
 
-    Task<IResultOutput> DeleteAsync(string id);
+    IResultOutput Pause(string id);
 
-    Task<IResultOutput> BatchDeleteAsync(string[] ids);
+    IResultOutput Resume(string id);
+
+    IResultOutput Run(string id);
+
+    IResultOutput Delete(string id);
 }

+ 90 - 12
src/platform/ZhonTai.Admin/Services/TaskScheduler/TaskService.cs

@@ -9,6 +9,9 @@ using ZhonTai.DynamicApi.Attributes;
 using Microsoft.AspNetCore.Mvc;
 using ZhonTai.Admin.Core.Consts;
 using FreeScheduler;
+using Microsoft.AspNetCore.Mvc.ModelBinding;
+using TaskStatus = FreeScheduler.TaskStatus;
+using ZhonTai.Common.Extensions;
 
 namespace ZhonTai.Admin.Services.TaskScheduler;
 
@@ -68,10 +71,46 @@ public class TaskService : BaseService, ITaskService, IDynamicApi
     /// </summary>
     /// <param name="input"></param>
     /// <returns></returns>
-    public async Task<IResultOutput> AddAsync(TaskAddInput input)
+    public IResultOutput Add(TaskAddInput input)
     {
-        var entity = Mapper.Map<TaskInfo>(input);
-        await _taskInfoRepository.InsertAsync(entity);
+        if (input.IntervalArgument.IsNull())
+        {
+            return ResultOutput.NotOk("请输入定时参数");
+        }
+
+        var scheduler = LazyGetRequiredService<Scheduler>();
+
+        if(input.Interval == TaskInterval.SEC && input.Round == -1)
+        {
+            scheduler.AddTask(input.Topic, input.Body, input.Round, input.IntervalArgument.ToInt());
+        } 
+        else if (input.Interval == TaskInterval.SEC && input.Round > 0)
+        {
+            int[] seconds = System.Array.Empty<int>();
+            var intervalArguments = input.IntervalArgument.Split(",");
+            foreach(var arg in intervalArguments)
+            {
+                seconds.Append(arg.ToInt());
+            }
+            scheduler.AddTask(input.Topic, input.Body, seconds);
+        }
+        else if (input.Interval == TaskInterval.RunOnDay && input.Round > 0)
+        {
+            scheduler.AddTaskRunOnDay(input.Topic, input.Body, input.Round, input.IntervalArgument);
+        }
+        else if (input.Interval == TaskInterval.RunOnWeek && input.Round > 0)
+        {
+            scheduler.AddTaskRunOnWeek(input.Topic, input.Body, input.Round, input.IntervalArgument);
+        }
+        else if (input.Interval == TaskInterval.RunOnMonth && input.Round > 0)
+        {
+            scheduler.AddTaskRunOnMonth(input.Topic, input.Body, input.Round, input.IntervalArgument);
+        }
+        else if (input.Interval == TaskInterval.Custom && input.Round > 0)
+        {
+            scheduler.AddTaskCustom(input.Topic, input.Body, input.IntervalArgument);
+        }
+
         return ResultOutput.Ok();
     }
 
@@ -95,39 +134,78 @@ public class TaskService : BaseService, ITaskService, IDynamicApi
 
         Mapper.Map(input, entity);
         await _taskInfoRepository.UpdateAsync(entity);
+
+        return ResultOutput.Ok();
+    }
+
+    /// <summary>
+    /// 暂停任务
+    /// </summary>
+    /// <param name="id"></param>
+    /// <returns></returns>
+    public IResultOutput Pause([BindRequired]string id)
+    {
+        if (id.IsNull())
+        {
+            return ResultOutput.NotOk();
+        }
+
+        var scheduler = LazyGetRequiredService<Scheduler>();
+        scheduler.PauseTask(id);
+
+        return ResultOutput.Ok();
+    }
+
+    /// <summary>
+    /// 启动任务
+    /// </summary>
+    /// <param name="id"></param>
+    /// <returns></returns>
+    public IResultOutput Resume([BindRequired] string id)
+    {
+        if (id.IsNull())
+        {
+            return ResultOutput.NotOk();
+        }
+
+        var scheduler = LazyGetRequiredService<Scheduler>();
+        scheduler.ResumeTask(id);
+
         return ResultOutput.Ok();
     }
 
     /// <summary>
-    /// 彻底删除
+    /// 执行任务
     /// </summary>
     /// <param name="id"></param>
     /// <returns></returns>
-    public async Task<IResultOutput> DeleteAsync(string id)
+    public IResultOutput Run([BindRequired] string id)
     {
         if (id.IsNull())
         {
             return ResultOutput.NotOk();
         }
 
-        await _taskInfoRepository.DeleteAsync(m => m.Id == id);
-        
+        var scheduler = LazyGetRequiredService<Scheduler>();
+        scheduler.RunNowTask(id);
+
         return ResultOutput.Ok();
     }
 
     /// <summary>
-    /// 批量彻底删除
+    /// 删除任务
     /// </summary>
-    /// <param name="ids"></param>
+    /// <param name="id"></param>
     /// <returns></returns>
-    public async Task<IResultOutput> BatchDeleteAsync(string[] ids)
+    public IResultOutput Delete([BindRequired] string id)
     {
-        if(!(ids?.Length > 0))
+        if (id.IsNull())
         {
             return ResultOutput.NotOk();
         }
 
-        await _taskInfoRepository.DeleteAsync(a => ids.Contains(a.Id));
+        var scheduler = LazyGetRequiredService<Scheduler>();
+        scheduler.RemoveTask(id);
 
         return ResultOutput.Ok();
     }

+ 1 - 1
src/platform/ZhonTai.Admin/Tools/TaskScheduler/TaskSchedulerServiceExtensions.cs

@@ -32,7 +32,7 @@ public static class TaskSchedulerServiceExtensions
             a.Name("ad_task");
             a.Property(b => b.Id).IsPrimary(true);
             a.Property(b => b.Body).StringLength(-1);
-            a.Property(b => b.Interval).MapType(typeof(string));
+            a.Property(b => b.Interval).MapType(typeof(int));
             a.Property(b => b.IntervalArgument).StringLength(1024);
             a.Property(b => b.Status).MapType(typeof(int));
             a.Property(b => b.CreateTime).ServerTime(DateTimeKind.Local);

+ 20 - 11
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -5149,11 +5149,6 @@
             定时参数值 60,60,60,120,120,1200,1200
             </summary>
         </member>
-        <member name="P:ZhonTai.Admin.Services.TaskScheduler.Dto.TaskAddInput.Status">
-            <summary>
-            任务状态
-            </summary>
-        </member>
         <member name="P:ZhonTai.Admin.Services.TaskScheduler.Dto.TaskListOutput.Id">
             <summary>
             主键
@@ -5260,7 +5255,7 @@
             <param name="input"></param>
             <returns></returns>
         </member>
-        <member name="M:ZhonTai.Admin.Services.TaskScheduler.TaskService.AddAsync(ZhonTai.Admin.Services.TaskScheduler.Dto.TaskAddInput)">
+        <member name="M:ZhonTai.Admin.Services.TaskScheduler.TaskService.Add(ZhonTai.Admin.Services.TaskScheduler.Dto.TaskAddInput)">
             <summary>
             新增
             </summary>
@@ -5274,18 +5269,32 @@
             <param name="input"></param>
             <returns></returns>
         </member>
-        <member name="M:ZhonTai.Admin.Services.TaskScheduler.TaskService.DeleteAsync(System.String)">
+        <member name="M:ZhonTai.Admin.Services.TaskScheduler.TaskService.Pause(System.String)">
             <summary>
-            彻底删除
+            暂停任务
             </summary>
             <param name="id"></param>
             <returns></returns>
         </member>
-        <member name="M:ZhonTai.Admin.Services.TaskScheduler.TaskService.BatchDeleteAsync(System.String[])">
+        <member name="M:ZhonTai.Admin.Services.TaskScheduler.TaskService.Resume(System.String)">
             <summary>
-            批量彻底删除
+            启动任务
             </summary>
-            <param name="ids"></param>
+            <param name="id"></param>
+            <returns></returns>
+        </member>
+        <member name="M:ZhonTai.Admin.Services.TaskScheduler.TaskService.Run(System.String)">
+            <summary>
+            执行任务
+            </summary>
+            <param name="id"></param>
+            <returns></returns>
+        </member>
+        <member name="M:ZhonTai.Admin.Services.TaskScheduler.TaskService.Delete(System.String)">
+            <summary>
+            删除任务
+            </summary>
+            <param name="id"></param>
             <returns></returns>
         </member>
         <member name="T:ZhonTai.Admin.Services.Tenant.Dto.TenantAddInput">