init
This commit is contained in:
71
DouyinApi.Extensions/HostedService/ConsulHostedService.cs
Normal file
71
DouyinApi.Extensions/HostedService/ConsulHostedService.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Consul;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DouyinApi.Extensions.HostedService;
|
||||
|
||||
public class ConsulHostedService : IHostedService
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IHostApplicationLifetime _hostApplicationLifetime;
|
||||
private readonly ILogger<ConsulHostedService> _logger;
|
||||
|
||||
public ConsulHostedService(IConfiguration configuration, IHostApplicationLifetime hostApplicationLifetime, ILogger<ConsulHostedService> logger)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_hostApplicationLifetime = hostApplicationLifetime;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Start Consul Service!");
|
||||
await DoWork();
|
||||
}
|
||||
|
||||
public async Task DoWork()
|
||||
{
|
||||
if (_configuration["Middleware:Consul:Enabled"].ObjToBool())
|
||||
{
|
||||
var consulClient = new ConsulClient(c =>
|
||||
{
|
||||
//consul地址
|
||||
c.Address = new Uri(_configuration["ConsulSetting:ConsulAddress"]);
|
||||
});
|
||||
|
||||
var registration = new AgentServiceRegistration()
|
||||
{
|
||||
ID = Guid.NewGuid().ToString(),//服务实例唯一标识
|
||||
Name = _configuration["ConsulSetting:ServiceName"],//服务名
|
||||
Address = _configuration["ConsulSetting:ServiceIP"], //服务IP
|
||||
Port = int.Parse(_configuration["ConsulSetting:ServicePort"]),//服务端口
|
||||
Check = new AgentServiceCheck()
|
||||
{
|
||||
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//服务启动多久后注册
|
||||
Interval = TimeSpan.FromSeconds(10),//健康检查时间间隔
|
||||
HTTP = $"http://{_configuration["ConsulSetting:ServiceIP"]}:{_configuration["ConsulSetting:ServicePort"]}{_configuration["ConsulSetting:ServiceHealthCheck"]}",//健康检查地址
|
||||
Timeout = TimeSpan.FromSeconds(5)//超时时间
|
||||
}
|
||||
};
|
||||
|
||||
//服务注册
|
||||
await consulClient.Agent.ServiceRegister(registration);
|
||||
|
||||
//应用程序终止时,取消注册
|
||||
_hostApplicationLifetime.ApplicationStopping.Register(async () =>
|
||||
{
|
||||
await consulClient.Agent.ServiceDeregister(registration.ID);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Stop Consul Service!");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
45
DouyinApi.Extensions/HostedService/EventBusHostedService.cs
Normal file
45
DouyinApi.Extensions/HostedService/EventBusHostedService.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using DouyinApi.Common;
|
||||
using DouyinApi.EventBus;
|
||||
using DouyinApi.EventBus.EventHandling;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DouyinApi.Extensions.HostedService;
|
||||
|
||||
public class EventBusHostedService : IHostedService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly ILogger<EventBusHostedService> _logger;
|
||||
|
||||
public EventBusHostedService(IServiceProvider serviceProvider, ILogger<EventBusHostedService> logger)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Start EventBus Service!");
|
||||
await DoWork();
|
||||
}
|
||||
|
||||
private Task DoWork()
|
||||
{
|
||||
if (AppSettings.app(new string[] { "EventBus", "Enabled" }).ObjToBool())
|
||||
{
|
||||
var eventBus = _serviceProvider.GetRequiredService<IEventBus>();
|
||||
eventBus.Subscribe<BlogQueryIntegrationEvent, BlogQueryIntegrationEventHandler>();
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Stop EventBus Service!");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
60
DouyinApi.Extensions/HostedService/Job1TimedService.cs
Normal file
60
DouyinApi.Extensions/HostedService/Job1TimedService.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using DouyinApi.Common;
|
||||
using DouyinApi.IServices;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DouyinApi.Extensions
|
||||
{
|
||||
public class Job1TimedService : IHostedService, IDisposable
|
||||
{
|
||||
private Timer _timer;
|
||||
private readonly IBlogArticleServices _blogArticleServices;
|
||||
|
||||
// 这里可以注入
|
||||
public Job1TimedService(IBlogArticleServices blogArticleServices)
|
||||
{
|
||||
_blogArticleServices = blogArticleServices;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Console.WriteLine("Job 1 is starting.");
|
||||
|
||||
_timer = new Timer(DoWork, null, TimeSpan.Zero,
|
||||
TimeSpan.FromSeconds(60 * 60));//一个小时
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void DoWork(object state)
|
||||
{
|
||||
try
|
||||
{
|
||||
var model = _blogArticleServices.GetBlogDetails(1).Result;
|
||||
Console.WriteLine($"Job 1 启动成功,获取id=1的博客title为:{model?.btitle}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error:{ex.Message}");
|
||||
}
|
||||
|
||||
ConsoleHelper.WriteSuccessLine($"Job 1: {DateTime.Now}");
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Console.WriteLine("Job 1 is stopping.");
|
||||
|
||||
_timer?.Change(Timeout.Infinite, 0);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
47
DouyinApi.Extensions/HostedService/Job2TimedService.cs
Normal file
47
DouyinApi.Extensions/HostedService/Job2TimedService.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using DouyinApi.Common;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DouyinApi.Extensions
|
||||
{
|
||||
public class Job2TimedService : IHostedService, IDisposable
|
||||
{
|
||||
private Timer _timer;
|
||||
|
||||
// 这里可以注入
|
||||
public Job2TimedService()
|
||||
{
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Console.WriteLine("Job 2 is starting.");
|
||||
|
||||
_timer = new Timer(DoWork, null, TimeSpan.Zero,
|
||||
TimeSpan.FromSeconds(60 * 60 * 2));//两个小时
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void DoWork(object state)
|
||||
{
|
||||
ConsoleHelper.WriteWarningLine($"Job 2: {DateTime.Now}");
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Console.WriteLine("Job 2 is stopping.");
|
||||
|
||||
_timer?.Change(Timeout.Infinite, 0);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
67
DouyinApi.Extensions/HostedService/QuartzJobHostedService.cs
Normal file
67
DouyinApi.Extensions/HostedService/QuartzJobHostedService.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using DouyinApi.Common;
|
||||
using DouyinApi.IServices;
|
||||
using DouyinApi.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DouyinApi.Extensions.HostedService;
|
||||
|
||||
public class QuartzJobHostedService : IHostedService
|
||||
{
|
||||
private readonly ITasksQzServices _tasksQzServices;
|
||||
private readonly ISchedulerCenter _schedulerCenter;
|
||||
private readonly ILogger<QuartzJobHostedService> _logger;
|
||||
|
||||
public QuartzJobHostedService(ITasksQzServices tasksQzServices, ISchedulerCenter schedulerCenter, ILogger<QuartzJobHostedService> logger)
|
||||
{
|
||||
_tasksQzServices = tasksQzServices;
|
||||
_schedulerCenter = schedulerCenter;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Start QuartzJob Service!");
|
||||
await DoWork();
|
||||
}
|
||||
|
||||
private async Task DoWork()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (AppSettings.app("Middleware", "QuartzNetJob", "Enabled").ObjToBool())
|
||||
{
|
||||
var allQzServices = await _tasksQzServices.Query();
|
||||
foreach (var item in allQzServices)
|
||||
{
|
||||
if (item.IsStart)
|
||||
{
|
||||
var result = await _schedulerCenter.AddScheduleJobAsync(item);
|
||||
if (result.success)
|
||||
{
|
||||
Console.WriteLine($"QuartzNetJob{item.Name}启动成功!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"QuartzNetJob{item.Name}启动失败!错误信息:{result.msg}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "An error was reported when starting the job service.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Stop QuartzJob Service!");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
61
DouyinApi.Extensions/HostedService/SeedDataHostedService.cs
Normal file
61
DouyinApi.Extensions/HostedService/SeedDataHostedService.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using DouyinApi.Common;
|
||||
using DouyinApi.Common.Seed;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DouyinApi.Extensions;
|
||||
|
||||
public sealed class SeedDataHostedService : IHostedService
|
||||
{
|
||||
private readonly MyContext _myContext;
|
||||
private readonly ILogger<SeedDataHostedService> _logger;
|
||||
private readonly string _webRootPath;
|
||||
|
||||
public SeedDataHostedService(
|
||||
MyContext myContext,
|
||||
IWebHostEnvironment webHostEnvironment,
|
||||
ILogger<SeedDataHostedService> logger)
|
||||
{
|
||||
_myContext = myContext;
|
||||
_logger = logger;
|
||||
_webRootPath = webHostEnvironment.WebRootPath;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Start Initialization Db Seed Service!");
|
||||
await DoWork();
|
||||
}
|
||||
|
||||
private async Task DoWork()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (AppSettings.app("AppSettings", "SeedDBEnabled").ObjToBool() || AppSettings.app("AppSettings", "SeedDBDataEnabled").ObjToBool())
|
||||
{
|
||||
await DBSeed.SeedAsync(_myContext, _webRootPath);
|
||||
|
||||
//日志
|
||||
DBSeed.MigrationLogs(_myContext);
|
||||
|
||||
//多租户 同步
|
||||
await DBSeed.TenantSeedAsync(_myContext);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error occured seeding the Database.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Stop Initialization Db Seed Service!");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user