Skip to content

Commit

Permalink
增加重试次数
Browse files Browse the repository at this point in the history
  • Loading branch information
linzhiqiang committed Apr 30, 2021
1 parent 135d3f5 commit 7aa2c3d
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 52 deletions.
8 changes: 5 additions & 3 deletions docs/db.dmx
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ Top=29.00</GraphDesc>

<DataModels_items1_Tables_items2_MetaFields_items3>
<ID>2</ID>
<Name>status</Name>
<Name>task_status</Name>
<Memo>0=禁用 1=启动</Memo>
<OrderNo>3</OrderNo>
<DisplayName>状态</DisplayName>
<DataType>6</DataType>
<DataType>2</DataType>
<DefaultValue>0</DefaultValue>
</DataModels_items1_Tables_items2_MetaFields_items3>

Expand Down Expand Up @@ -270,6 +270,7 @@ Top=23.00</GraphDesc>
<DataModels_items1_Tables_items3_MetaFields_items4>
<ID>55</ID>
<Name>trigger_code</Name>
<Memo>0=初始化 2=执行成功 9=执行失败 </Memo>
<CreateDate>2021/4/23 10:04:22</CreateDate>
<OrderNo>4</OrderNo>
<DisplayName>调度code</DisplayName>
Expand Down Expand Up @@ -301,6 +302,7 @@ Top=23.00</GraphDesc>
<DataModels_items1_Tables_items3_MetaFields_items7>
<ID>342</ID>
<Name>result_code</Name>
<Memo>0=初始化 2=执行成功 9=执行失败 </Memo>
<OrderNo>7</OrderNo>
<DisplayName>结果code</DisplayName>
<DataType>2</DataType>
Expand Down Expand Up @@ -333,7 +335,7 @@ Top=23.00</GraphDesc>
<CreateDate>2021/4/29 15:44:45</CreateDate>
<OrderNo>10</OrderNo>
<DisplayName>告警状态</DisplayName>
<DataType>6</DataType>
<DataType>2</DataType>
<DefaultValue>0</DefaultValue>
</DataModels_items1_Tables_items3_MetaFields_items10>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private async Task _scheduleTaskExecutor_OnHandleMessage(ScheduleTaskContext arg
{
_logger.LogInformation($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}执行任务:{arg.LogId}——{arg.TaskContent}");
// await Task.Delay(TimeSpan.FromSeconds(5));

throw new Exception("测试重试");
}
//catch(BizException ex)
//{
Expand All @@ -63,7 +63,10 @@ private async Task _scheduleTaskExecutor_OnHandleMessage(ScheduleTaskContext arg
code = -1;
message = ex.Message;
}
await _scheduleTaskService.SaveExecuteResult(new ExecuteResultDTO { LogId = arg.LogId, Code = 0, Message = "success" });
finally
{
await _scheduleTaskService.SaveExecuteResult(new ExecuteResultDTO { LogId = arg.LogId, Code = code, Message = message });
}
}
}
}
35 changes: 18 additions & 17 deletions scripts/db/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,36 @@ alter table `aix_distribution_lock`
insert into aix_distribution_lock(lock_name) values('AixScheduleTaskLock');


create table `aix_schedule_task_info`
create table aix_schedule_task_info
(
`id` INT auto_increment primary key not null comment '主键',
`task_group` VARCHAR(50) comment '所属组 根据需要进行扩展',
`status` TINYINT default 0 not null comment '状态 0=禁用 1=启动',
`task_name` VARCHAR(50) not null comment '任务名称',
`task_desc` VARCHAR(200) comment '任务描述',
`cron` VARCHAR(50) not null comment '定时表达式',
`task_content` VARCHAR(500) not null comment '内容',
`last_execute_time` BIGINT default 0 not null comment '上次执行时间',
`next_execute_time` BIGINT default 0 not null comment '下次执行时间',
`max_retry_count` INT default 0 not null comment '最大重试次数 0=不重试',
`creator_id` VARCHAR(50) not null comment '创建人编号',
`create_time` DATETIME default now() not null comment '创建日期',
`modifier_id` VARCHAR(50) not null comment '修改人编号',
`modify_time` DATETIME default now() not null comment '修改日期'
id INT primary key auto_increment not null comment '主键',
task_group VARCHAR(50) comment '所属组 根据需要进行扩展',
task_status INT default 0 not null comment '状态 0=禁用 1=启动',
task_name VARCHAR(50) not null comment '任务名称',
task_desc VARCHAR(200) comment '任务描述',
cron VARCHAR(50) not null comment '定时表达式',
task_content VARCHAR(500) not null comment '内容',
last_execute_time BIGINT default 0 not null comment '上次执行时间',
next_execute_time BIGINT default 0 not null comment '下次执行时间',
max_retry_count INT default 0 not null comment '最大重试次数 0=不重试',
creator_id VARCHAR(50) not null comment '创建人编号',
create_time TIMESTAMP default current_timestamp not null comment '创建日期',
modifier_id VARCHAR(50) not null comment '修改人编号',
modify_time TIMESTAMP default current_timestamp not null comment '修改日期'
) comment '定时任务';

create table aix_schedule_task_log
(
id INT primary key auto_increment not null comment '主键',
schedule_task_id INT not null comment '定时任务id',
retry_count INT default 0 not null comment '重试次数',
trigger_code INT default 0 not null comment '调度code',
trigger_code INT default 0 not null comment '调度code 0=初始化 2=执行成功 9=执行失败 ',
trigger_message VARCHAR(500) comment '调度信息',
trigger_time DATETIME comment '调度时间',
result_code INT not null comment '结果code',
result_code INT not null comment '结果code 0=初始化 2=执行成功 9=执行失败 ',
result_message VARCHAR(500) comment '结果信息',
result_time DATETIME comment '结果时间',
alarm_status INT default 0 not null comment '告警状态 告警状态:0-默认、-1-锁定状态、1-无需告警、2-告警成功、9-告警失败',
create_time TIMESTAMP default current_timestamp not null comment '创建日期',
modify_time TIMESTAMP default current_timestamp not null comment '修改日期'
) comment '定时任务log';
16 changes: 9 additions & 7 deletions scripts/db/sqlserver.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ EXEC sp_addextendedproperty 'MS_Description', '
insert into aix_distribution_lock(lock_name) values('AixScheduleTaskLock');



create table aix_schedule_task_info
(
id INT identity(1, 1) not null /*主键*/,
task_group VARCHAR(50) /*所属组 根据需要进行扩展*/,
status TINYINT default 0 not null /*状态 0=禁用 1=启动*/,
task_status INT default 0 not null /*状态 0=禁用 1=启动*/,
task_name VARCHAR(50) not null /*任务名称*/,
task_desc VARCHAR(200) /*任务描述*/,
cron VARCHAR(50) not null /*定时表达式*/,
Expand All @@ -34,7 +33,7 @@ alter table aix_schedule_task_info
EXEC sp_addextendedproperty 'MS_Description', '定时任务', 'user', dbo, 'table', aix_schedule_task_info, NULL, NULL;
EXEC sp_addextendedproperty 'MS_Description', '主键', 'user', dbo, 'table', aix_schedule_task_info, 'column', id;
EXEC sp_addextendedproperty 'MS_Description', '所属组 根据需要进行扩展', 'user', dbo, 'table', aix_schedule_task_info, 'column', task_group;
EXEC sp_addextendedproperty 'MS_Description', '状态 0=禁用 1=启动', 'user', dbo, 'table', aix_schedule_task_info, 'column', status;
EXEC sp_addextendedproperty 'MS_Description', '状态 0=禁用 1=启动', 'user', dbo, 'table', aix_schedule_task_info, 'column', task_status;
EXEC sp_addextendedproperty 'MS_Description', '任务名称', 'user', dbo, 'table', aix_schedule_task_info, 'column', task_name;
EXEC sp_addextendedproperty 'MS_Description', '任务描述', 'user', dbo, 'table', aix_schedule_task_info, 'column', task_desc;
EXEC sp_addextendedproperty 'MS_Description', '定时表达式', 'user', dbo, 'table', aix_schedule_task_info, 'column', cron;
Expand All @@ -49,17 +48,19 @@ EXEC sp_addextendedproperty 'MS_Description', '




create table aix_schedule_task_log
(
id INT identity(1, 1) not null /*主键*/,
schedule_task_id INT not null /*定时任务id*/,
retry_count INT default 0 not null /*重试次数*/,
trigger_code INT default 0 not null /*调度code*/,
trigger_code INT default 0 not null /*调度code 0=初始化 2=执行成功 9=执行失败 */,
trigger_message VARCHAR(500) /*调度信息*/,
trigger_time DATETIME /*调度时间*/,
result_code INT not null /*结果code*/,
result_code INT not null /*结果code 0=初始化 2=执行成功 9=执行失败 */,
result_message VARCHAR(500) /*结果信息*/,
result_time DATETIME /*结果时间*/,
alarm_status INT default 0 not null /*告警状态 告警状态:0-默认、-1-锁定状态、1-无需告警、2-告警成功、9-告警失败*/,
create_time DATETIME default getdate() not null /*创建日期*/,
modify_time DATETIME default getdate() not null /*修改日期*/
);
Expand All @@ -69,11 +70,12 @@ EXEC sp_addextendedproperty 'MS_Description', '
EXEC sp_addextendedproperty 'MS_Description', '主键', 'user', dbo, 'table', aix_schedule_task_log, 'column', id;
EXEC sp_addextendedproperty 'MS_Description', '定时任务id', 'user', dbo, 'table', aix_schedule_task_log, 'column', schedule_task_id;
EXEC sp_addextendedproperty 'MS_Description', '重试次数', 'user', dbo, 'table', aix_schedule_task_log, 'column', retry_count;
EXEC sp_addextendedproperty 'MS_Description', '调度code', 'user', dbo, 'table', aix_schedule_task_log, 'column', trigger_code;
EXEC sp_addextendedproperty 'MS_Description', '调度code 0=初始化 2=执行成功 9=执行失败 ', 'user', dbo, 'table', aix_schedule_task_log, 'column', trigger_code;
EXEC sp_addextendedproperty 'MS_Description', '调度信息', 'user', dbo, 'table', aix_schedule_task_log, 'column', trigger_message;
EXEC sp_addextendedproperty 'MS_Description', '调度时间', 'user', dbo, 'table', aix_schedule_task_log, 'column', trigger_time;
EXEC sp_addextendedproperty 'MS_Description', '结果code', 'user', dbo, 'table', aix_schedule_task_log, 'column', result_code;
EXEC sp_addextendedproperty 'MS_Description', '结果code 0=初始化 2=执行成功 9=执行失败 ', 'user', dbo, 'table', aix_schedule_task_log, 'column', result_code;
EXEC sp_addextendedproperty 'MS_Description', '结果信息', 'user', dbo, 'table', aix_schedule_task_log, 'column', result_message;
EXEC sp_addextendedproperty 'MS_Description', '结果时间', 'user', dbo, 'table', aix_schedule_task_log, 'column', result_time;
EXEC sp_addextendedproperty 'MS_Description', '告警状态 告警状态:0-默认、-1-锁定状态、1-无需告警、2-告警成功、9-告警失败', 'user', dbo, 'table', aix_schedule_task_log, 'column', alarm_status;
EXEC sp_addextendedproperty 'MS_Description', '创建日期', 'user', dbo, 'table', aix_schedule_task_log, 'column', create_time;
EXEC sp_addextendedproperty 'MS_Description', '修改日期', 'user', dbo, 'table', aix_schedule_task_log, 'column', modify_time;
Binary file modified scripts/orm/Aix.EntityGenerator.dll
Binary file not shown.
6 changes: 3 additions & 3 deletions src/Aix.ScheduleTask/Aix.ScheduleTask.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>分布式定时任务:数据库实现支持 sqlserver、mysql</Description>
<Version>1.0.18</Version>
<PackageReleaseNotes>优化</PackageReleaseNotes>
<Version>1.0.19</Version>
<PackageReleaseNotes>增加错误重试功能</PackageReleaseNotes>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>

Expand All @@ -19,7 +19,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aix.MultithreadExecutor" Version="1.0.2" />
<PackageReference Include="Aix.MultithreadExecutor" Version="1.0.3" />
<PackageReference Include="Aix.ORM" Version="1.0.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
Expand Down
11 changes: 11 additions & 0 deletions src/Aix.ScheduleTask/Aix.ScheduleTask.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Aix.ScheduleTask/AixScheduleTaskOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Aix.ScheduleTask
{
public class AixScheduleTaskOptions
public class AixScheduleTaskOptions
{
/// <summary>
/// 数据库连接字符串
Expand Down Expand Up @@ -46,5 +46,10 @@ public class AixScheduleTaskOptions
/// </summary>
public int LogExpireHour { get; set; } = 168;

/// <summary>
/// 错误任务 重试间隔时间 默认10秒
/// </summary>
public int RetryIntervalMillisecond { get; set; } = 10;

}
}
35 changes: 35 additions & 0 deletions src/Aix.ScheduleTask/DTO/ReturnT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;

namespace Aix.ScheduleTask.DTO
{
[DataContract]
public class ReturnT
{
public const int SUCCESS_CODE = 2;
public const int FAIL_CODE = 9;
public int Code { get; set; }
public string Message { get; set; }

private ReturnT() { }

private ReturnT(int code, string message)
{
Code = code;
Message = message;
}


public static ReturnT Failed(string msg)
{
return new ReturnT(FAIL_CODE, msg);
}
public static ReturnT Success(string msg = "success")
{
return new ReturnT(SUCCESS_CODE, msg);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
该文件为自动生成,不要修改。
生成时间:2021-04-29 16:02:55
生成时间:2021-04-30 13:24:32
*/
using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
该文件为自动生成,不要修改。
生成时间:2021-04-29 16:02:55
生成时间:2021-04-30 13:24:32
*/
using System;
using System.Collections.Generic;
Expand All @@ -18,7 +18,7 @@ public partial class AixScheduleTaskInfo : BaseEntity
{
private int _id;
private string _task_group;
private sbyte _status;
private int _status;
private string _task_name;
private string _task_desc;
private string _cron;
Expand Down Expand Up @@ -52,10 +52,10 @@ public string TaskGroup
set { _task_group = value; OnPropertyChanged("task_group"); }
}
/// <summary>
/// 状态 0=禁用 1=启动 tinyint(1)
/// 状态 0=禁用 1=启动 int(4)
/// <summary>
[Column("status",IsNullable=false,DefaultValue="0")]
public sbyte Status
public int Status
{
get { return _status; }
set { _status = value; OnPropertyChanged("status"); }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
该文件为自动生成,不要修改。
生成时间:2021-04-29 16:02:55
生成时间:2021-04-30 13:24:32
*/
using System;
using System.Collections.Generic;
Expand All @@ -25,7 +25,7 @@ public partial class AixScheduleTaskLog : BaseEntity
private int _result_code;
private string _result_message;
private DateTime? _result_time;
private sbyte _alarm_status;
private int _alarm_status;
private DateTime _create_time;
private DateTime _modify_time;

Expand Down Expand Up @@ -113,10 +113,10 @@ public DateTime? ResultTime
set { _result_time = value; OnPropertyChanged("result_time"); }
}
/// <summary>
/// 告警状态 告警状态:0-默认、-1-锁定状态、1-无需告警、2-告警成功、9-告警失败 tinyint(1)
/// 告警状态 告警状态:0-默认、-1-锁定状态、1-无需告警、2-告警成功、9-告警失败 int(4)
/// <summary>
[Column("alarm_status",IsNullable=false,DefaultValue="0")]
public sbyte AlarmStatus
public int AlarmStatus
{
get { return _alarm_status; }
set { _alarm_status = value; OnPropertyChanged("alarm_status"); }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Aix.ScheduleTask.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -7,6 +8,11 @@ namespace Aix.ScheduleTask.Repository
{
public interface IAixScheduleTaskLogRepository : ICommonRepository
{
Task<AixScheduleTaskLog> GetById(int id);
Task<int> Delete(DateTime expiration);

Task<List<int>> QueryFailJobLogIds(int count = 1000);

Task<int> UpdateAlarmStatus(int id, int oldAlarmStatus, int newAlarmStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Aix.ScheduleTask.Repository
{
public interface IAixScheduleTaskRepository: ICommonRepository
{
Task<AixScheduleTaskInfo> GetById(int id);
Task<PagedList<AixScheduleTaskInfo>> PageQuery(PageView pageView);

Task<List<AixScheduleTaskInfo>> QueryAllEnabled(long nextExecuteTime);
Expand Down
Loading

0 comments on commit 7aa2c3d

Please sign in to comment.