feat: 完善 Task 任务管理功能
This commit is contained in:
parent
23dd2d05df
commit
3ebfe8cd56
|
@ -29,8 +29,14 @@
|
|||
</CardBody>
|
||||
</Card>
|
||||
|
||||
<AdminTable TItem="TasksModel" ExtendButtonColumnWidth="270" OnQueryAsync="OnQueryAsync" IsFixedHeader="false">
|
||||
<ColumnsTemplete>
|
||||
<AdminAlert Text="演示系统禁止修改内置后台任务" IsShow="IsDemo" />
|
||||
|
||||
<Table TItem="TasksModel" ExtendButtonColumnWidth="270" IsBordered="true" IsStriped="true"
|
||||
IsMultipleSelect="true" IsFixedHeader="false"
|
||||
ShowToolbar="true" ShowExtendButtons="true" SelectedRows="SelectedRows"
|
||||
ShowEditButtonCallback="OnShowButtonCallback" ShowDeleteButtonCallback="OnShowButtonCallback"
|
||||
OnQueryAsync="OnQueryAsync" OnDeleteAsync="OnDeleteAsync" OnSaveAsync="OnSaveAsync">
|
||||
<TableColumns>
|
||||
<TableColumn @bind-Field="@context.Name" Sortable="true" Filterable="true" Searchable="true" Width="80"></TableColumn>
|
||||
<TableColumn @bind-Field="@context.CreateTime" Sortable="true" Filterable="true" Searchable="true" FormatString="yyyy-MM-dd HH:mm:ss"></TableColumn>
|
||||
<TableColumn @bind-Field="@context.LastRuntime" Sortable="true" Filterable="true" Searchable="true" FormatString="yyyy-MM-dd HH:mm:ss"></TableColumn>
|
||||
|
@ -46,11 +52,14 @@
|
|||
<Tag Color="GetStatusColor(v.Value)" Icon="@GetStatusIcon(v.Value)">@FormatStatus(v.Value)</Tag>
|
||||
</Template>
|
||||
</TableColumn>
|
||||
</ColumnsTemplete>
|
||||
</TableColumns>
|
||||
<EditTemplate>
|
||||
<TaskEditor @bind-Value="context" />
|
||||
</EditTemplate>
|
||||
<RowButtonTemplate>
|
||||
<TableCellButton Size="Size.ExtraSmall" Color="Color.Warning" Icon="fa fa-pause-circle" Text="暂停" OnClick="() => OnPause(context)" IsShow="OnCheckTaskStatus(context)" AutoRenderTableWhenClick="true" />
|
||||
<TableCellButton Size="Size.ExtraSmall" Color="Color.Success" Icon="fa fa-play-circle" Text="运行" OnClick="() => OnRun(context)" IsShow="OnCheckTaskStatus(context)" AutoRenderTableWhenClick="true" />
|
||||
<TableCellButton Size="Size.ExtraSmall" Color="Color.Info" Icon="fa fa-info-circle" Text="日志" OnClick="() => OnLog(context)" IsShow="OnCheckTaskStatus(context)" AutoRenderTableWhenClick="true" />
|
||||
</RowButtonTemplate>
|
||||
</AdminTable>
|
||||
</Table>
|
||||
</div>
|
|
@ -1,4 +1,4 @@
|
|||
using BootstrapAdmin.Web.Components;
|
||||
using BootstrapAdmin.Web.Core;
|
||||
using BootstrapAdmin.Web.Extensions;
|
||||
using BootstrapAdmin.Web.Models;
|
||||
using Longbow.Tasks;
|
||||
|
@ -7,18 +7,76 @@ namespace BootstrapAdmin.Web.Pages.Admin;
|
|||
|
||||
public partial class Tasks
|
||||
{
|
||||
[NotNull]
|
||||
private AdminTable<TasksModel>? TaskTable { get; set; }
|
||||
private List<TasksModel> SelectedRows { get; set; } = new List<TasksModel>();
|
||||
|
||||
private static Task<QueryData<TasksModel>> OnQueryAsync(QueryPageOptions options)
|
||||
private static IEnumerable<string> Jobs => new string[]
|
||||
{
|
||||
var tasks = TaskServicesManager.ToList().ToTasksModelList();
|
||||
"单次任务",
|
||||
"周期任务",
|
||||
"Cron 任务",
|
||||
"超时任务",
|
||||
"取消任务",
|
||||
"禁用任务",
|
||||
"SQL日志",
|
||||
"健康检查"
|
||||
};
|
||||
|
||||
[Inject]
|
||||
[NotNull]
|
||||
private IDict? DictService { get; set; }
|
||||
|
||||
private bool IsDemo { get; set; }
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
base.OnInitialized();
|
||||
|
||||
IsDemo = DictService.IsDemo();
|
||||
}
|
||||
|
||||
private Task<QueryData<TasksModel>> OnQueryAsync(QueryPageOptions options)
|
||||
{
|
||||
var tasks = TaskServicesManager.ToList().OrderBy(i => i.Name).ToTasksModelList();
|
||||
var model = tasks.FirstOrDefault(i => i.Name == SelectedRows.FirstOrDefault()?.Name);
|
||||
SelectedRows.Clear();
|
||||
if (model != null)
|
||||
{
|
||||
SelectedRows.Add(model);
|
||||
}
|
||||
return Task.FromResult(new QueryData<TasksModel>()
|
||||
{
|
||||
Items = tasks
|
||||
});
|
||||
}
|
||||
|
||||
private static Task<bool> OnSaveAsync(TasksModel model, ItemChangedType changedType)
|
||||
{
|
||||
var taskExecutor = new DefaultTaskExecutor();
|
||||
TaskServicesManager.Remove(model.Name);
|
||||
TaskServicesManager.GetOrAdd(model.Name, token => taskExecutor.Execute(token), TriggerBuilder.Build(model.Trigger));
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
private Task<bool> OnDeleteAsync(IEnumerable<TasksModel> models)
|
||||
{
|
||||
// 演示模式下禁止删除内置任务
|
||||
if (IsDemo)
|
||||
{
|
||||
var m = models.ToList();
|
||||
m.RemoveAll(m => Jobs.Any(i => i == m.Name));
|
||||
models = m;
|
||||
}
|
||||
|
||||
// 循环删除任务
|
||||
foreach (var model in models)
|
||||
{
|
||||
TaskServicesManager.Remove(model.Name);
|
||||
}
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
private bool OnShowButtonCallback(TasksModel model) => !IsDemo || !Jobs.Any(i => i == model.Name);
|
||||
|
||||
private static Color GetResultColor(TriggerResult result) => result switch
|
||||
{
|
||||
TriggerResult.Success => Color.Success,
|
||||
|
@ -60,23 +118,27 @@ public partial class Tasks
|
|||
_ => "未知状态"
|
||||
};
|
||||
|
||||
private static Task OnPause(TasksModel model)
|
||||
private Task OnPause(TasksModel model)
|
||||
{
|
||||
var task = TaskServicesManager.ToList().FirstOrDefault(i => i.Name == model.Name);
|
||||
if (task != null)
|
||||
{
|
||||
task.Status = SchedulerStatus.Ready;
|
||||
}
|
||||
SelectedRows.Clear();
|
||||
SelectedRows.Add(model);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static Task OnRun(TasksModel model)
|
||||
private Task OnRun(TasksModel model)
|
||||
{
|
||||
var task = TaskServicesManager.ToList().FirstOrDefault(i => i.Name == model.Name);
|
||||
if (task != null)
|
||||
{
|
||||
task.Status = SchedulerStatus.Running;
|
||||
}
|
||||
SelectedRows.Clear();
|
||||
SelectedRows.Add(model);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
@ -86,4 +148,14 @@ public partial class Tasks
|
|||
}
|
||||
|
||||
private static bool OnCheckTaskStatus(TasksModel model) => model.Status != SchedulerStatus.Disabled;
|
||||
|
||||
class DefaultTaskExecutor : ITask
|
||||
{
|
||||
/// <summary>
|
||||
/// 任务执行方法
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public Task Execute(CancellationToken cancellationToken) => Task.Delay(1000, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue