sunpengfei
2025-11-19 7b47c91bcf89d667a5c99cfafe0d899280f7fbe3
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
using Furion;
using Furion.DatabaseAccessor;
using Furion.Schedule;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace ApiTools.Core
{
    /// <summary>
    /// 作业持久化(数据库)
    /// </summary>
    public class DbJobPersistence : IJobPersistence
    {
        private readonly IServiceScopeFactory serviceScopeFactory;
 
        public DbJobPersistence(IServiceScopeFactory serviceScopeFactory)
        {
            this.serviceScopeFactory = serviceScopeFactory;
        }
 
        /// <summary>
        /// 作业调度服务启动时
        /// </summary>
        /// <returns></returns>
        public Task<IEnumerable<SchedulerBuilder>> PreloadAsync(CancellationToken stoppingToken)
        {
            return Task.FromResult(App.EffectiveTypes.ScanToBuilders().AsEnumerable());
        }
 
        public Task<SchedulerBuilder> OnLoadingAsync(SchedulerBuilder builder, CancellationToken stoppingToken)
        {
            return Task.FromResult(builder);
        }
 
        public Task OnChangedAsync(PersistenceContext context)
        {
            return Task.CompletedTask;
        }
 
        public Task OnTriggerChangedAsync(PersistenceTriggerContext context)
        {
            return Task.CompletedTask;
        }
 
        public async Task OnExecutionRecordAsync(PersistenceExecutionRecordContext context)
        {
            using (var scope = serviceScopeFactory.CreateScope())
            {
                var rep = scope.ServiceProvider.GetRequiredService<IRepository<ScheduleJobTriggerTimeline, LogDbContextLocator>>();
                var entity = new ScheduleJobTriggerTimeline();
                context.Timeline.Adapt(entity);
                await rep.InsertNowAsync(entity);
            }
        }
    }
}