Quartz.net调度程序会收集垃圾

问题描述 投票:0回答:1

当前使用crystal.net运行12个作业。

前10个作业需要每30分钟运行一次。第二个作业需要每3分钟运行一次。

问题是每3分钟运行2个作业。该程序停止工作。它不会崩溃,控制台窗口仍在响应,但该应用程序不再触发作业。

我认为这是因为调度程序收集了垃圾。有人对此有解决方案吗?

StartAsync:

public async Task StartAsync(CancellationToken cancellationToken)
{
    var scheduler = await GetScheduler();
    await scheduler.Start();

    await scheduler.ScheduleJob(GetAbsenceJob(), GetDefaultTrigger(nameof(AbsenceJob)));
    await scheduler.ScheduleJob(GetAddressJob(), GetDefaultTrigger(nameof(AddressJob)));
    await scheduler.ScheduleJob(GetCustomerJob(), GetDefaultTrigger(nameof(CustomerJob)));
    await scheduler.ScheduleJob(GetDriverJob(), GetDefaultTrigger(nameof(DriverJob)));
    await scheduler.ScheduleJob(GetPlanCombJob(), GetDefaultTrigger(nameof(PlanCombJob)));
    await scheduler.ScheduleJob(GetPlanGroupJob(), GetDefaultTrigger(nameof(PlanGroupJob)));
    await scheduler.ScheduleJob(GetSupplierJob(), GetDefaultTrigger(nameof(SupplierJob)));
    await scheduler.ScheduleJob(GetTrailerJob(), GetDefaultTrigger(nameof(TrailerJob)));
    await scheduler.ScheduleJob(GetTransportTypeJob(), GetDefaultTrigger(nameof(TransportTypeJob)));
    await scheduler.ScheduleJob(GetVehicleJob(), GetDefaultTrigger(nameof(VehicleJob)));

    await scheduler.ScheduleJob(GetImportFilesJob(), GetImportTrigger(nameof(ImportFilesJob)));
    await scheduler.ScheduleJob(GetExtractFilesJob(), GetImportTrigger(nameof(ExtractFilesJob)));

}

GetSchedular

private static async Task<IScheduler> GetScheduler()
{
    var props = new NameValueCollection {
        { "quartz.threadPool.threadCount",      "20" },
        { "quartz.jobStore.misfireThreshold",   "60000" },
        { "quartz.serializer.type",             "binary" },
        { "quartz.scheduler.instanceName",      "SynchroScheduler"},
        { "quartz.jobStore.type",               "Quartz.Simpl.RAMJobStore, Quartz" },
        { "quartz.threadPool.type",             "Quartz.Simpl.SimpleThreadPool, Quartz" }
    };
    var factory = new StdSchedulerFactory(props);
    var scheduler = await factory.GetScheduler();

    return scheduler;
}

编辑:

如何启动服务:

var host = new HostBuilder()
                .ConfigureHostConfiguration(configHost =>
                {
                    configHost.SetBasePath(Directory.GetCurrentDirectory());
                    configHost.AddJsonFile(Text.Hostsettings, optional: true);
                    configHost.AddEnvironmentVariables(prefix: Text.Prefix);
                    configHost.AddCommandLine(args);
                })
                .ConfigureAppConfiguration((hostContext, configApp) =>
                {
                    configApp.SetBasePath(Directory.GetCurrentDirectory());
                    configApp.AddJsonFile(Text.Appsettings, optional: true);
                    configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
                    configApp.AddEnvironmentVariables(prefix: Text.Prefix);
                    configApp.AddCommandLine(args);
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddLogging();
                    services.Configure<SynchroDbOptions>(hostContext.Configuration.GetSection("SynchoDbOptions"));
                    services.AddHostedService<DbSyncService>();
                })
                .ConfigureLogging((hostContext, configLogging) =>
                {
                    configLogging.AddConfiguration(hostContext.Configuration.GetSection("Logging"));
            configLogging.AddConsole();

        })
        .UseConsoleLifetime()
        .Build();

host.RunAsync();
c# cron quartz.net cron-task
1个回答
0
投票

是的,你是对的。 ScheduleJob仅注册该作业,它不等待其执行。在某个时候,垃圾收集器将出现并回收内存,而我记得有一个终结器可以对任何作业进行调度。因此,您需要存储要调度到的IScheduler实例。

一种选择是将IScheduler返回给调用者,以便他们可以决定如何存储它:

public async Task<IScheduler> StartAsync(CancellationToken cancellationToken)
{
    ...
    ...
    return scheduler;
}

[另一种方法是让您的实例方法将其存储在成员变量中,并让类管理调度程序的生存期。如果不知道代码的全部范围,就无法说出哪一个最好。

© www.soinside.com 2019 - 2024. All rights reserved.