refactor: 根据最新的Tasks组件重构代码

This commit is contained in:
Argo 2019-07-15 15:53:39 +08:00
parent 3ba18ac948
commit 7388a54f20
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
8 changed files with 63 additions and 29 deletions

View File

@ -14,7 +14,7 @@
<ItemGroup>
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.11" />
<PackageReference Include="Longbow.Configuration" Version="2.2.4" />
<PackageReference Include="Longbow.Tasks" Version="1.6.0" />
<PackageReference Include="Longbow.Tasks" Version="1.7.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="3.1.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" PrivateAssets="All" />

View File

@ -1,5 +1,6 @@
using Longbow.Tasks;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -20,8 +21,12 @@ namespace Bootstrap.Admin.Controllers.Api
[HttpGet]
public IEnumerable<object> Get()
{
TaskServicesManager.GetOrAdd("测试任务", token => Task.Delay(1000), TriggerBuilder.WithCronExpression("*/5 * * * * *"));
return TaskServicesManager.ToList().Select(s => new { s.Name, s.Enabled, s.Status, s.LastRuntime, s.CreatedTime, s.NextRuntime, Triggers = s.Triggers.Count, s.LastRunResult, TriggerExpression = s.Triggers.FirstOrDefault().ToString() });
TaskServicesManager.GetOrAdd("单次任务", token => Task.Delay(1000));
TaskServicesManager.GetOrAdd("周期任务", token => Task.Delay(1000), TriggerBuilder.Build(Cron.Secondly(5)));
TaskServicesManager.GetOrAdd("超时任务", token => Task.Delay(2000), TriggerBuilder.Default.WithTimeout(1000).WithInterval(1000).WithRepeatCount(2).Build());
TaskServicesManager.GetOrAdd("故障任务", token => throw new Exception("故障任务"));
TaskServicesManager.GetOrAdd("取消任务", token => Task.Delay(1000)).Triggers.First().Enabled = false;
return TaskServicesManager.ToList().Select(s => new { s.Name, Status = s.Status.ToString(), s.LastRuntime, s.CreatedTime, s.NextRuntime, LastRunResult = s.Triggers.First().LastResult.ToString(), TriggerExpression = s.Triggers.FirstOrDefault().ToString() }).OrderBy(s => s.Name);
}
}
}

View File

@ -2,6 +2,7 @@
using Longbow.Web.SignalR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
@ -22,12 +23,11 @@ namespace Bootstrap.Admin.Controllers.Api
public bool Get([FromQuery]string name, [FromServices]IHubContext<SignalRHub> hub)
{
var sche = TaskServicesManager.GetOrAdd(name);
sche.Triggers[0].RegisterPulseCallback(async t =>
sche.Triggers.First().PulseCallback = async t =>
{
var success = t.Cancelled ? "Cancelled" : "Success";
var result = $"{t.Scheduler.LastRuntime.Value.DateTime}: Trigger({t.GetType().Name}) Run({success}) NextRuntime: {t.NextRuntime.Value.DateTime} Elapsed: {t.LastRunElapsedTime.Seconds}s";
var result = $"{{\"name\": \"{name}\", \"msg\": \"{sche.LastRuntime}: Trigger({t.GetType().Name}) Run({t.LastResult}) NextRuntime: {sche.NextRuntime} Elapsed: {t.LastRunElapsedTime.Seconds}s\"}}";
await SignalRManager.SendTaskLog(hub.Clients.All, result);
});
};
return true;
}
}

View File

@ -33,14 +33,14 @@
<p>
<code>
var trigger = TriggerBuilder.Default.WithInterval(TimeSpan.FromSeconds(5)).WithRepeatCount(2).WithStartTime(DateTimeOffset.Now.AddMinutes(1)).Build();
<br/>
<br />
TaskServicesManager.GetOrAdd("测试任务", token => Task.Delay(1000), trigger);
</code>
</p>
<p>3. Cron表达式任务 (间隔 5 秒循环执行任务)</p>
<p>
<code>
TaskServicesManager.GetOrAdd("Cron表达式任务", token => Task.Delay(1000), TriggerBuilder.WithCronExpression("*/5 * * * * *"));
TaskServicesManager.GetOrAdd("Cron表达式任务", token => Task.Delay(1000), TriggerBuilder.Build("*/5 * * * * *"));
</code>
</p>
</div>
@ -86,9 +86,11 @@
</div>
<div class="modal-body" style="background-color: #174482; color: #fff;">
<div id="taskMsg" class="form-group" style="height: 500px; overflow: auto;">
<div></div>
</div>
</div>
<div class="modal-footer">
<a class="btn"><i class="fa fa-check-square-o" style="width: 13px;"></i><span>自动滚屏</span></a>
<button type="button" class="btn btn-secondary" data-dismiss="modal">
<i class="fa fa-times"></i>
<span>关闭</span>

View File

@ -4,24 +4,35 @@
var stateFormatter = function (value) {
var template = "<button class='btn btn-sm btn-{0}'><i class='fa fa-{1}'></i><span>{2}<span></button>";
var content = "";
if (value === "0") {
if (value === "Ready") {
content = $.format(template, 'info', 'fa', '未开始');
}
else if (value === "1") {
else if (value === "Running") {
content = $.format(template, 'success', 'play-circle', '运行中');
}
else if (value === "2") {
content = $.format(template, 'primary', 'stop-circle', '已停止');
}
else if (value === "3") {
else if (value === "Disabled") {
content = $.format(template, 'danger', 'times-circle', '已禁用');
}
return content;
};
var enabledFormatter = function (value) {
var template = "<i class='fa fa-toggle-{0}'></i>";
return $.format(template, value ? 'on' : 'off');
var resultFormatter = function (value) {
var template = "<button class='btn btn-sm btn-{0}'><span>{1}<span></button>";
var content = "";
if (value === "Success") {
content = $.format(template, 'success', '成功');
}
else if (value === "Error") {
content = $.format(template, 'danger', '故障');
}
else if (value === "Cancelled") {
content = $.format(template, 'info', '取消');
}
else if (value === "Timeout") {
content = $.format(template, 'warning', '超时');
}
return content;
};
$('.card-body table').lgbTable({
url: Tasks.url,
dataBinder: {
@ -31,6 +42,7 @@
}
},
smartTable: {
sidePagination: "client",
sortName: 'CreateTime',
sortOrder: 'desc',
queryParams: function (params) { return $.extend(params, { operateType: $("#txt_operate_type").val(), OperateTimeStart: $("#txt_operate_start").val(), OperateTimeEnd: $("#txt_operate_end").val() }); },
@ -40,7 +52,7 @@
{ title: "上次执行时间", field: "LastRuntime", sortable: true },
{ title: "下次执行时间", field: "NextRuntime", sortable: true },
{ title: "触发条件", field: "TriggerExpression", sortable: false },
{ title: "是否启用", field: "Enabled", sortable: true, formatter: enabledFormatter },
{ title: "执行结果", field: "LastRunResult", sortable: false, align: 'center', formatter: resultFormatter },
{ title: "状态", field: "Status", sortable: true, align: 'center', width: 106, formatter: stateFormatter }
],
editButtons: {
@ -53,23 +65,29 @@
$('#dialogLog').modal('show').on('hide.bs.modal', function () {
// close hub
if ($taskMsg.hub) $taskMsg.hub.stop();
$taskMsg.html('');
$taskMsg.html('<div></div>');
});
var lastMsg = "";
// var lastMsg = "";
// open hub
$taskMsg.notifi({
url: 'NotiHub',
method: 'taskRev',
callback: function (result) {
if (lastMsg === result) return;
lastMsg = result;
while (this.children().length > 50) {
this.children().first().remove();
var content = this.children();
while (content.children().length > 50) {
content.children().first().remove();
}
var data = JSON.parse(result);
if (data.name !== row.Name) return;
result = data.msg;
result = result.replace("Run(Cancelled)", "<span class='text-danger'>Run(Cancelled)</span>");
result = result.replace("Run(Success)", "<span class='text-success'>Run(Success)</span>");
this.append('<div>' + result + '</div>');
content.append('<div>' + result + '</div>');
// auto scroll
if ($autoScroll.find('i').hasClass(check[0])) this.scrollTop(content.height());
},
onclose: function (error) {
console.log(error);
@ -80,4 +98,13 @@
}
}
});
var $autoScroll = $('#dialogLog').find('.modal-footer > a.btn');
var check = ["fa-check-square-o", "fa-square-o"];
$autoScroll.on('click', function () {
var $this = $(this).find('i');
if ($this.hasClass(check[0])) $this.addClass(check[1]).removeClass(check[0]);
else $this.addClass(check[0]).removeClass(check[1]);
});
});

View File

@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security" Version="1.6.0-preview-1" />
<PackageReference Include="Bootstrap.Security" Version="1.7.0" />
<PackageReference Include="MongoDB.Driver" Version="2.8.1" />
</ItemGroup>

View File

@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security.DataAccess" Version="2.2.8-preview-1" />
<PackageReference Include="Bootstrap.Security.DataAccess" Version="2.2.10" />
<PackageReference Include="Longbow.Data" Version="2.3.3" />
<PackageReference Include="Longbow.Logging" Version="2.2.9" />
<PackageReference Include="Longbow.Security.Cryptography" Version="1.3.0" />

View File

@ -15,7 +15,7 @@ namespace Bootstrap.Admin.Api.SqlServer
Assert.NotNull(resp);
// receive log
var recv = await Client.GetAsJsonAsync<bool>("/api/TasksLog?name=测试任务");
var recv = await Client.GetAsJsonAsync<bool>("/api/TasksLog?name=周期任务");
Assert.True(recv);
// for test SignalRManager.SendTaskLog