wxw
3 天以前 aea5b5b55049b7099466b3d235d8e04e55de2947
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Utility.Tools;
using Wms.Tools;
using WMS.BLL.BllSoServer;
using WMS.BLL.DataServer;
using WMS.IBLL.IDataServer;
 
namespace Wms
{
    public class DailyTaskService : IHostedService, IDisposable
    {
        private Timer _timer;
        private Timer _timer2;
        private Timer _timer3;
        private string _wcsUrl;
        private string _agvUrl;
        private Timer _periodicReport;
        public DailyTaskService(string wcsUrl,string agvUrl) 
        {
            _wcsUrl = wcsUrl;
            _agvUrl = agvUrl;
        }
 
        //创建定时任务
        public Task StartAsync(CancellationToken cancellationToken)
        {
            #region 每天0点执行
            //计算距离下一个 0 点的时间间隔
            DateTime now = DateTime.Now;
            DateTime nextZeroHour = now.AddDays(1).Date;
            TimeSpan delay = nextZeroHour - now;
 
            //创建定时器,并设置回调函数
            _timer = new Timer(ExecuteDailyTask, null, delay, TimeSpan.FromDays(1));
            _periodicReport = new Timer(PeriodicReport, null, delay, TimeSpan.FromDays(1));
            //TimeSpan delay2 = TimeSpan.FromMinutes(2);
 
            // 创建定时器,并设置回调函数
            // _timer2 = new Timer(ExecuteDailyTask2, null, delay2, TimeSpan.FromMinutes(3));
 
            //TimeSpan delay3 = TimeSpan.FromMinutes(2);
 
            // 创建定时器,并设置回调函数
            //_timer3 = new Timer(ExecuteDailyTask3, null, delay3, TimeSpan.FromMinutes(3));
 
            return Task.CompletedTask;
            #endregion
 
        }
 
        /// <summary>
        /// 在应用程序关闭时被调用,用于执行清理操作和释放资源。在 ASP.NET Core 应用程序中,
        /// 当主机关闭时,StopAsync 方法会被调用来停止后台服务,以确保服务能够正确地停止并释放它所占用的资源,比如关闭定时器、释放数据库连接等。
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task StopAsync(CancellationToken cancellationToken)
        {
            // 停止定时器
            _timer?.Change(Timeout.Infinite, 0);
            _timer2?.Change(Timeout.Infinite, 0);
            _timer3?.Change(Timeout.Infinite, 0);
            _periodicReport?.Change(Timeout.Infinite, 0);
 
            return Task.CompletedTask;
        }
        //执行任务逻辑
        private void ExecuteDailyTask(object state)
        {
            try
            {
                StockDetailServer _stockDetail = new StockDetailServer();
                //任务逻辑
                _stockDetail.StockCheck();
            }
            catch (Exception ex)
            {
                var logStr = $@".\log\WMS\WMS定时任务" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                LogFile.SaveLogToFile($"WMS定时任务异常:( {ex.Message} ),", logStr);
            }
        }
 
        //执行任务逻辑
        private void ExecuteDailyTask2(object state)
        {
            try
            {
                ExportNoticeServer _exNotice = new ExportNoticeServer();
                //任务逻辑
                var list = _exNotice.BeiLiaoIssueOutHouse(_wcsUrl);
            }
            catch (Exception ex)
            {
                var logStr = $@".\log\WMS\WMS定时备料任务" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                LogFile.SaveLogToFile($"WMS定时备料任务异常:( {ex.Message} ),", logStr);
            }
        }
 
 
        //执行任务逻辑
        private void ExecuteDailyTask3(object state)
        {
            try
            {
                ExportNoticeServer _exNotice = new ExportNoticeServer();
                //任务逻辑
                _exNotice.ProCallIssueOutHouse(_agvUrl);
            }
            catch (Exception ex)
            {
                var logStr = $@".\log\WMS\WMS定时备料任务" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                LogFile.SaveLogToFile($"WMS定时备料任务异常:( {ex.Message} ),", logStr);
            }
        }
 
        //定时执行周期报表
        private void PeriodicReport(object state)
        {
            try
            {
                StockInfoServer _stock = new StockInfoServer();
                var arr = _stock.ByDayInsertStock();
 
                var logStr = $@".\log\WMS\WMS定时任务" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                LogFile.SaveLogToFile($"WMS定时任务执行异常:{arr},", logStr);
                //释放资源
                _periodicReport?.Dispose();
            }
            catch (Exception ex)
            {
                var logStr = $@".\log\WMS\WMS定时任务" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                LogFile.SaveLogToFile($"WMS定时任务执行异常:{DateTime.Now}:( {ex.Message} ),", logStr);
            }
        }
 
        /// <summary>
        /// 在服务结束运行时,定时器得到正确地清理和释放
        /// </summary>
        public void Dispose()
        {
            //释放资源
            _timer?.Dispose();
            _timer2?.Dispose();
            _timer3?.Dispose();
            _periodicReport?.Dispose();
        }
    }
}