122 lines
3.6 KiB
C#
122 lines
3.6 KiB
C#
using New_College.Common.Helper;
|
||
using New_College.IServices;
|
||
using Microsoft.Extensions.Hosting;
|
||
using System;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace New_College.Tasks
|
||
{
|
||
public class ExcelExportJobTimedService : IHostedService, IDisposable
|
||
{
|
||
private Timer _timer;
|
||
private readonly IV_CustomerInfoServices _customerInfoServices;
|
||
// private readonly Action _taskAction;
|
||
|
||
// 这里可以注入
|
||
public ExcelExportJobTimedService(IV_CustomerInfoServices v_CustomerInfoServices)
|
||
{
|
||
_customerInfoServices = v_CustomerInfoServices;
|
||
|
||
// _taskAction = taskAction ?? throw new ArgumentNullException(nameof(taskAction));
|
||
|
||
// 初始化定时器
|
||
_timer = new Timer(async state =>
|
||
{
|
||
// 执行任务
|
||
await ExecuteTaskAsync();
|
||
|
||
// 计算下一次执行时间
|
||
DateTime nextExecutionTime = CalculateNextMondayMorning(8);
|
||
|
||
// 重新设置定时器间隔,确保在下周一早上8点触发
|
||
TimeSpan timeUntilNextExecution = nextExecutionTime - DateTime.Now;
|
||
if (timeUntilNextExecution.TotalMilliseconds > 0)
|
||
{
|
||
_timer.Change(timeUntilNextExecution, TimeSpan.FromMilliseconds(-1));
|
||
}
|
||
}, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(-1));
|
||
|
||
}
|
||
|
||
|
||
|
||
private async Task ExecuteTaskAsync()
|
||
{
|
||
await Task.Run(async () =>
|
||
{
|
||
try
|
||
{
|
||
// await _customerInfoServices.CustomeBillExport();
|
||
Console.WriteLine($"ExportJob 启动成功");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine($"Error:{ex.Message}");
|
||
}
|
||
|
||
ConsoleHelper.WriteSuccessLine($"Job ExportJob: {DateTime.Now}");
|
||
});
|
||
}
|
||
|
||
private static DateTime CalculateNextMondayMorning(int hour)
|
||
{
|
||
DateTime now = DateTime.Now;
|
||
DateTime nextMonday = GetNextMonday(now);
|
||
return new DateTime(nextMonday.Year, nextMonday.Month, nextMonday.Day, hour, 0, 0, DateTimeKind.Local);
|
||
}
|
||
|
||
private static DateTime GetNextMonday(DateTime input)
|
||
{
|
||
DayOfWeek dayOfWeek = input.DayOfWeek;
|
||
int daysUntilMonday = ((int)DayOfWeek.Monday - (int)dayOfWeek + 7) % 7;
|
||
return input.AddDays(daysUntilMonday);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
public Task StartAsync(CancellationToken cancellationToken)
|
||
{
|
||
Console.WriteLine("ExcelExportJob is starting.");
|
||
|
||
// 启动定时器
|
||
_timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(-1));
|
||
|
||
return Task.CompletedTask;
|
||
}
|
||
|
||
//private async void DoWork(object state)
|
||
//{
|
||
// try
|
||
// {
|
||
// await _customerInfoServices.CustomeBillExport();
|
||
// Console.WriteLine($"ExportJob 启动成功");
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// Console.WriteLine($"Error:{ex.Message}");
|
||
// }
|
||
|
||
// ConsoleHelper.WriteSuccessLine($"Job ExportJob: {DateTime.Now}");
|
||
//}
|
||
|
||
public Task StopAsync(CancellationToken cancellationToken)
|
||
{
|
||
Console.WriteLine("ExportJob is stopping.");
|
||
|
||
// 停止定时器
|
||
_timer.Change(Timeout.Infinite, Timeout.Infinite);
|
||
|
||
return Task.CompletedTask;
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
_timer?.Dispose();
|
||
}
|
||
}
|
||
}
|