WCS
dengjunjie
2024-10-14 901c64f294ee75784f3fec80b47ae6b77932fff3
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
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_QuartzJob
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:调度服务层实体映射
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 * 
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
 
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core.Seed;
using WIDESEAWCS_Core;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using WIDESEAWCS_QuartzJob.Seed;
using WIDESEAWCS_QuartzJob.Service;
 
namespace WIDESEAWCS_QuartzJob.QuartzExtensions
{
    public sealed class QuartzJobDataTableHostedService : IHostedService
    {
        private readonly DBContext _dbContext;
        private readonly ILogger<QuartzJobDataTableHostedService> _logger;
        private readonly string _webRootPath;
        private readonly IServiceProvider _serviceProvider;
 
        public QuartzJobDataTableHostedService(
            IServiceProvider serviceProvider,
            IWebHostEnvironment webHostEnvironment,
            ILogger<QuartzJobDataTableHostedService> logger)
        {
            _serviceProvider = serviceProvider;
            _logger = logger;
            _webRootPath = webHostEnvironment.WebRootPath;
            using var scope = _serviceProvider.CreateScope();
            DBContext dbContext = scope.ServiceProvider.GetService<DBContext>();
            _dbContext = dbContext;
        }
 
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("开始创建定时器调度数据表");
            await DoWork();
        }
 
        private async Task DoWork()
        {
            try
            {
                await QuartzJobCreateDataTabel.SeedAsync(_dbContext, _webRootPath);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "定时器调度数据表创建错误");
                throw;
            }
        }
 
        public Task StopAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("定时器调度数据表结束");
            return Task.CompletedTask;
        }
    }
}