重启定时任务

This commit is contained in:
alex
2023-08-15 16:46:41 +08:00
parent 7152bbddb3
commit 0b23512a57
5 changed files with 105 additions and 40 deletions

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tnb.WarehouseMgr.Entities.Enums
{
public enum TimeSpanUnit
{
Milliseconds,
Seconds,
Minutes,
Hours,
Days
}
}

View File

@@ -27,6 +27,7 @@ using Natasha.CSharp;
using Tnb.Common.Extension; using Tnb.Common.Extension;
using Tnb.WarehouseMgr.Entities.Attributes; using Tnb.WarehouseMgr.Entities.Attributes;
using Tnb.WarehouseMgr.Entities.Dto.Inputs; using Tnb.WarehouseMgr.Entities.Dto.Inputs;
using Tnb.WarehouseMgr.Entities.Enums;
using Tnb.WarehouseMgr.Entities.Exceptions; using Tnb.WarehouseMgr.Entities.Exceptions;
using Tnb.WarehouseMgr.Interfaces; using Tnb.WarehouseMgr.Interfaces;
@@ -42,12 +43,14 @@ namespace Tnb.WarehouseMgr
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
private static Dictionary<string, Func<CancellationTokenSource?, Task>> _timedFuncMap = new(StringComparer.OrdinalIgnoreCase); private static Dictionary<string, Func<CancellationTokenSource?, Task>> _timedFuncMap = new(StringComparer.OrdinalIgnoreCase);
static TimedTaskBackgroundService() static TimedTaskBackgroundService()
{
Task.Run(() =>
{ {
_timedFuncMap = App.EffectiveTypes.AsParallel().Where(t => !t.Namespace.IsNullOrWhiteSpace() && t.Namespace.Contains("Tnb.WarehouseMgr")).SelectMany(t => t.GetMethods()) _timedFuncMap = App.EffectiveTypes.AsParallel().Where(t => !t.Namespace.IsNullOrWhiteSpace() && t.Namespace.Contains("Tnb.WarehouseMgr")).SelectMany(t => t.GetMethods())
.Where(m => m.GetCustomAttribute<TimedAttribute>() != null) .Where(m => m.GetCustomAttribute<TimedAttribute>() != null)
.ToDictionary(x => x.Name, x => .ToDictionary(x => x.Name, x =>
(Func<CancellationTokenSource?, Task>)Delegate.CreateDelegate(typeof(Func<CancellationTokenSource?, Task>), App.GetService(x.DeclaringType), x)); (Func<CancellationTokenSource?, Task>)Delegate.CreateDelegate(typeof(Func<CancellationTokenSource?, Task>), App.GetService(x.DeclaringType), x));
});
} }
public TimedTaskBackgroundService(IServiceProvider serviceProvider) public TimedTaskBackgroundService(IServiceProvider serviceProvider)
{ {
@@ -74,27 +77,24 @@ namespace Tnb.WarehouseMgr
#region #region
//_eventPublisher = App.GetRequiredService<IEventPublisher>(); //_eventPublisher = App.GetRequiredService<IEventPublisher>();
//List<string> toUserIds = new List<string>() { "25398501929509" }; CancellationTokenSource kittingOutAddCts = new();
////生成任务执行 CancellationTokenSource kittingOutShippedCts = new();
//CancellationTokenSource genTaskCTS = new(); CancellationTokenSource setSortingCts = new();
//CancellationTokenSource kittingOutAddCts = new();
//CancellationTokenSource kittingOutShippedCts = new();
//CancellationTokenSource setSortingCts = new();
//var wareHouseService = App.GetRequiredService<IWareHouseService>(); //var wareHouseService = App.GetRequiredService<IWareHouseService>();
//TimedTask(cts => wareHouseService.GenTaskExecute(cts), genTaskCTS, toUserIds); //TimedTask(cts => wareHouseService.GenTaskExecute(cts), genTaskCTS);
////齐套出库 //齐套出库
//var kittingOutService = App.GetRequiredService<IWmskittingOutService>(); var kittingOutService = App.GetRequiredService<IWmskittingOutService>();
//TimedTask(cts => kittingOutService.KittingOutByAdd(cts), kittingOutAddCts, toUserIds); TimedTask(cts => kittingOutService.KittingOutByAdd(cts), kittingOutAddCts, 1);
//TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(cts), kittingOutShippedCts, toUserIds); TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(cts), kittingOutShippedCts, 1);
////齐套分拣 //齐套分拣
//var setSortingService = App.GetRequiredService<IWmsSetSortingService>(); var setSortingService = App.GetRequiredService<IWmsSetSortingService>();
//TimedTask(cts => setSortingService.PackSortingByAdd(cts), setSortingCts, toUserIds); TimedTask(cts => setSortingService.PackSortingByAdd(cts), setSortingCts, 1);
#endregion #endregion
}, stoppingToken); }, stoppingToken);
private Task TimedTask(Func<CancellationTokenSource, Task> action, CancellationTokenSource cts, List<string>? toUserIds = default) private Task TimedTask(Func<CancellationTokenSource, Task> action, CancellationTokenSource cts, int interval, TimeSpanUnit timeType = TimeSpanUnit.Seconds)
{ {
var token = cts.Token; var token = cts.Token;
return Task.Run(async () => return Task.Run(async () =>
@@ -120,10 +120,24 @@ namespace Tnb.WarehouseMgr
})); }));
} }
}); });
await Task.Delay(1000);
await GetDelayTask(timeType, interval);
} }
}, token); }, token);
} }
private Task GetDelayTask(TimeSpanUnit timeType, int interval)
{
Task delayTask = timeType switch
{
TimeSpanUnit.Milliseconds => Task.Delay(TimeSpan.FromMilliseconds(interval)),
TimeSpanUnit.Seconds => Task.Delay(TimeSpan.FromSeconds(interval)),
TimeSpanUnit.Minutes => Task.Delay(TimeSpan.FromMinutes(interval)),
TimeSpanUnit.Hours => Task.Delay(TimeSpan.FromHours(interval)),
TimeSpanUnit.Days => Task.Delay(TimeSpan.FromDays(interval)),
};
return delayTask;
}
} }
} }

View File

@@ -246,6 +246,10 @@ namespace Tnb.WarehouseMgr
kittingOut.carry_id = kittingIn.carry_id; kittingOut.carry_id = kittingIn.carry_id;
kittingOut.carry_code = kittingIn.carry_code; kittingOut.carry_code = kittingIn.carry_code;
await _db.Updateable(kittingOut).UpdateColumns(it => new { it.status, it.carry_id, it.carry_code }).ExecuteCommandAsync(); await _db.Updateable(kittingOut).UpdateColumns(it => new { it.status, it.carry_id, it.carry_code }).ExecuteCommandAsync();
//if (kittingOut.status == WmsWareHouseConst.BILLSTATUS_TOBESHIPPED_ID)
//{
// await Publish(nameof(IWmskittingOutService.KittingOutByIsToBeShipped));
//}
} }
} }

View File

@@ -8,12 +8,16 @@ using System.Threading.Tasks;
using JNPF; using JNPF;
using JNPF.Common.Const; using JNPF.Common.Const;
using JNPF.Common.Core.Manager; using JNPF.Common.Core.Manager;
using JNPF.Common.Dtos.VisualDev;
using JNPF.Common.Enums; using JNPF.Common.Enums;
using JNPF.Common.Extension; using JNPF.Common.Extension;
using JNPF.Common.Manager; using JNPF.Common.Manager;
using JNPF.Common.Security; using JNPF.Common.Security;
using JNPF.FriendlyException; using JNPF.FriendlyException;
using JNPF.Systems.Interfaces.System; using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys;
using JNPF.VisualDev.Interfaces;
using Mapster; using Mapster;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula; using NPOI.SS.Formula;
@@ -36,6 +40,7 @@ namespace Tnb.WarehouseMgr
/// 齐套分拣服务类 /// 齐套分拣服务类
/// </summary> /// </summary>
[ServiceModule(BizTypeId)] [ServiceModule(BizTypeId)]
[OverideVisualDev(ModuleConsts.MODULE_WMSSETSORTING_ID)]
public class WmsSetSortingService : BaseWareHouseService, IWmsSetSortingService public class WmsSetSortingService : BaseWareHouseService, IWmsSetSortingService
{ {
private readonly ISqlSugarClient _db; private readonly ISqlSugarClient _db;
@@ -43,15 +48,46 @@ namespace Tnb.WarehouseMgr
private readonly IBillRullService _billRullService; private readonly IBillRullService _billRullService;
private readonly IUserManager _userManager; private readonly IUserManager _userManager;
private readonly ICacheManager _cacheManager; private readonly ICacheManager _cacheManager;
private readonly IRunService _runService;
private readonly IVisualDevService _visualDevService;
private const string BizTypeId = "26186830379045"; private const string BizTypeId = "26186830379045";
public WmsSetSortingService(ISqlSugarRepository<WmsSetsortingH> repository, IWareHouseService wareHouseService, IUserManager userManager, IBillRullService billRullService, ICacheManager cacheManager) public WmsSetSortingService(
ISqlSugarRepository<WmsSetsortingH> repository,
IWareHouseService wareHouseService,
IUserManager userManager, IBillRullService billRullService,
ICacheManager cacheManager,
IRunService runService,
IVisualDevService visualDevService,
ITaskMessageNotify taskMessageNotify
) : base(taskMessageNotify.Writer)
{ {
_db = repository.AsSugarClient(); _db = repository.AsSugarClient();
_wareHouseService = wareHouseService; _wareHouseService = wareHouseService;
_billRullService = billRullService; _billRullService = billRullService;
_userManager = userManager; _userManager = userManager;
_cacheManager = cacheManager; _cacheManager = cacheManager;
_runService = runService;
_visualDevService = visualDevService;
OverideFuncs.CreateAsync = Create;
}
private async Task<dynamic> Create(VisualDevModelDataCrInput input)
{
//在线开发
try
{
VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleConsts.MODULE_WMSEMPTYINSTOCK_ID, true);
await _runService.Create(templateEntity, input);
await PackSortingByAdd();
}
catch (Exception)
{
throw;
}
return Task.FromResult(1);
} }
/// <summary> /// <summary>
@@ -61,9 +97,6 @@ namespace Tnb.WarehouseMgr
[HttpPost, Timed(Name = nameof(PackSortingByAdd))] [HttpPost, Timed(Name = nameof(PackSortingByAdd))]
public async Task PackSortingByAdd(CancellationTokenSource? cts = default) public async Task PackSortingByAdd(CancellationTokenSource? cts = default)
{ {
var aToken = await _cacheManager.GetAsync("AccessToken");
if (aToken.IsNullOrWhiteSpace()) return;
var curUser = await GetUserIdentity(aToken);
var curDb = _db.CopyNew(); var curDb = _db.CopyNew();
string firstLocationId = "27010980724501", secondLocationId = "27010987857941"; string firstLocationId = "27010980724501", secondLocationId = "27010987857941";
@@ -189,12 +222,12 @@ namespace Tnb.WarehouseMgr
{ {
var leftCarrys = carrys[..mid]; var leftCarrys = carrys[..mid];
var rightCarrys = carrys[mid..]; var rightCarrys = carrys[mid..];
await InnerGenPreTask(leftCarrys, locIds, firstLocationId, singleSorting.id, singleSorting.bill_code, preTasks, endLocation, curUser); await InnerGenPreTask(leftCarrys, locIds, firstLocationId, singleSorting.id, singleSorting.bill_code, preTasks, endLocation);
await InnerGenPreTask(rightCarrys, locIds, secondLocationId, singleSorting.id, singleSorting.bill_code, preTasks, endLocation, curUser); await InnerGenPreTask(rightCarrys, locIds, secondLocationId, singleSorting.id, singleSorting.bill_code, preTasks, endLocation);
} }
else else
{ {
await InnerGenPreTask(carrys, locIds, firstLocationId, singleSorting.id, singleSorting.bill_code, preTasks, endLocation, curUser); await InnerGenPreTask(carrys, locIds, firstLocationId, singleSorting.id, singleSorting.bill_code, preTasks, endLocation);
} }
List<WmsPretaskCode> pretaskCodes = new(); List<WmsPretaskCode> pretaskCodes = new();
foreach (var pt in preTasks) foreach (var pt in preTasks)
@@ -226,19 +259,13 @@ namespace Tnb.WarehouseMgr
{ {
JNPF.Logging.Log.Error("齐套分拣执行时出现错误", ex); JNPF.Logging.Log.Error("齐套分拣执行时出现错误", ex);
await curDb.Ado.RollbackTranAsync(); await curDb.Ado.RollbackTranAsync();
TimedTaskErrorInfo ei = new()
{
RequestURL = App.HttpContext?.Request?.Path,
RequestMethod = App.HttpContext?.Request?.Method,
userIdentity = curUser,
};
var timedTaskEx = ex.ToTimedTaskException(ei);
cts?.Cancel(); cts?.Cancel();
throw timedTaskEx; throw;
} }
} }
private async Task InnerGenPreTask(WmsCarryH[] carrys, List<string> locIds, string eLocationId, string requireId, string requireCode, List<WmsPretaskH> preTasks, BasLocation endLocation, ClaimsPrincipal curUser) private async Task InnerGenPreTask(WmsCarryH[] carrys, List<string> locIds, string eLocationId, string requireId, string requireCode, List<WmsPretaskH> preTasks, BasLocation endLocation)
{ {
foreach (var carry in carrys) foreach (var carry in carrys)
{ {
@@ -263,7 +290,7 @@ namespace Tnb.WarehouseMgr
WmsPretaskH preTask = new() WmsPretaskH preTask = new()
{ {
org_id = curUser.FindFirst(ClaimConst.CLAINMORGID)?.Value, org_id = _userManager.User.OrganizeId,
startlocation_id = sPoint?.location_id!, startlocation_id = sPoint?.location_id!,
startlocation_code = sPoint?.location_code!, startlocation_code = sPoint?.location_code!,
endlocation_id = ePoint?.location_id!, endlocation_id = ePoint?.location_id!,
@@ -280,7 +307,7 @@ namespace Tnb.WarehouseMgr
area_code = it.Key, area_code = it.Key,
require_id = requireId, require_id = requireId,
require_code = requireCode, require_code = requireCode,
create_id = curUser.FindFirst(ClaimConst.CLAINMUSERID)?.Value!, create_id = _userManager.UserId,
create_time = DateTime.Now, create_time = DateTime.Now,
source_id = carry.source_id, source_id = carry.source_id,
source_code = carry.source_code, source_code = carry.source_code,

View File

@@ -116,6 +116,7 @@ namespace Tnb.WarehouseMgr
ko.carry_id = firstCarry?.id; ko.carry_id = firstCarry?.id;
ko.carry_code = firstCarry?.carry_code; ko.carry_code = firstCarry?.carry_code;
await _db.Updateable(ko).UpdateColumns(it => new { it.status, it.carry_id, it.carry_code }).ExecuteCommandAsync(); await _db.Updateable(ko).UpdateColumns(it => new { it.status, it.carry_id, it.carry_code }).ExecuteCommandAsync();
//await KittingOutByIsToBeShipped();
if (firstCarry != null) if (firstCarry != null)
{ {
firstCarry.source_id = ko.source_id; firstCarry.source_id = ko.source_id;
@@ -153,6 +154,7 @@ namespace Tnb.WarehouseMgr
ko.status = WmsWareHouseConst.BILLSTATUS_CALLED_ID; ko.status = WmsWareHouseConst.BILLSTATUS_CALLED_ID;
await curDb.Updateable(ko).UpdateColumns(it => it.status).ExecuteCommandAsync(); await curDb.Updateable(ko).UpdateColumns(it => it.status).ExecuteCommandAsync();
isCalled = true; isCalled = true;
//await Publish(nameof(IWmsSetSortingService.PackSortingByAdd));
} }
} }
} }