完善agent任务
This commit is contained in:
parent
ec7303269c
commit
499e72bd2e
|
@ -9,6 +9,10 @@
|
|||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\AntJob.Extensions\AntJob.Extensions.csproj" />
|
||||
<ProjectReference Include="..\..\AntJob\AntJob.csproj" />
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AntJob;
|
||||
using AntJob.Providers;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NewLife;
|
||||
using NewLife.Configuration;
|
||||
using NewLife.Log;
|
||||
|
||||
namespace HisAgent
|
||||
{
|
||||
public class JobHost : BackgroundService
|
||||
{
|
||||
private readonly IConfigProvider _config;
|
||||
private readonly ITracer _tracer;
|
||||
private Scheduler _scheduler;
|
||||
|
||||
public JobHost(IConfigProvider config, ITracer tracer, ILog log)
|
||||
{
|
||||
_config = config;
|
||||
_tracer = tracer;
|
||||
// 设置日志
|
||||
XTrace.Log = log;
|
||||
}
|
||||
|
||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var set = AntSetting.Current;
|
||||
|
||||
var server = _config["antServer"];
|
||||
if (!server.IsNullOrEmpty())
|
||||
{
|
||||
set.Server = server;
|
||||
set.Save();
|
||||
}
|
||||
|
||||
// 实例化调度器
|
||||
var sc = new Scheduler
|
||||
{
|
||||
Tracer = _tracer,
|
||||
|
||||
// 使用分布式调度引擎替换默认的本地文件调度
|
||||
Provider = new NetworkJobProvider
|
||||
{
|
||||
Server = set.Server,
|
||||
AppID = set.AppID,
|
||||
Secret = set.Secret,
|
||||
Debug = false
|
||||
}
|
||||
};
|
||||
|
||||
// 添加作业
|
||||
sc.AddHandler<HelloJob>();
|
||||
|
||||
|
||||
// 启动调度引擎,调度器内部多线程处理
|
||||
sc.Start();
|
||||
_scheduler = sc;
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public override Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_scheduler.TryDispose();
|
||||
_scheduler = null;
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
using System;
|
||||
using AntJob;
|
||||
using AntJob.Providers;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NewLife.Log;
|
||||
|
||||
namespace HisAgent
|
||||
|
@ -11,29 +13,23 @@ namespace HisAgent
|
|||
{
|
||||
XTrace.UseConsole();
|
||||
|
||||
var set = AntSetting.Current;
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
// 实例化调度器
|
||||
var sc = new Scheduler();
|
||||
/// <summary></summary>
|
||||
/// <param name="args"></param>
|
||||
/// <returns></returns>
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureServices((hostContext, services) => ConfigureServices(services));
|
||||
|
||||
// 使用分布式调度引擎替换默认的本地文件调度
|
||||
sc.Provider = new NetworkJobProvider
|
||||
{
|
||||
Server = set.Server,
|
||||
AppID = set.AppID,
|
||||
Secret = set.Secret,
|
||||
};
|
||||
|
||||
// 添加作业处理器
|
||||
sc.AddHandler<HelloJob>();
|
||||
sc.AddHandler<BuildPatient>();
|
||||
sc.AddHandler<BuildWill>();
|
||||
|
||||
// 启动调度引擎,调度器内部多线程处理
|
||||
sc.Start();
|
||||
|
||||
Console.WriteLine("OK!");
|
||||
Console.ReadKey();
|
||||
/// <summary></summary>
|
||||
/// <param name="hostBuilderContext"></param>
|
||||
/// <param name="services"></param>
|
||||
public static void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
// 添加后台调度服务
|
||||
services.AddHostedService<JobHost>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue