ibms-dome/BackendWorkerService/Program.cs

155 lines
6.9 KiB
C#
Raw Normal View History

2022-10-14 16:08:54 +08:00
using Backend.Models;
using BackendWorkerService.Quartz;
using BackendWorkerService.Quartz.Jobs;
using BackendWorkerService.Services.Implement;
using BackendWorkerService.Services.Interface;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using Repository.BackendRepository.Implement;
using Repository.BackendRepository.Interface;
using Repository.BaseRepository.Implement;
using Repository.BaseRepository.Interface;
using Repository.FrontendRepository.Implement;
using Repository.FrontendRepository.Interface;
using Repository.Helper;
using Repository.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BackendWorkerService
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
IConfiguration configuration = hostContext.Configuration;
services.AddLogging(
builder =>
{
builder.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("NToastNotify", LogLevel.Warning)
.AddConsole();
});
//services.AddHostedService<Worker>();
#region <EFBFBD>T<EFBFBD><EFBFBD><EFBFBD>q<EFBFBD><EFBFBD>
#region SMPT<EFBFBD>t<EFBFBD>m
services.AddTransient<ISendEmailService, SendEmailService>();
services.Configure<SMTPConfig>(configuration.GetSection("SMTPConfig"));
#endregion
#region SMS<EFBFBD>t<EFBFBD>m
services.AddTransient<ISendSMSService, SendSMSService>();
services.Configure<SMSConfig>(configuration.GetSection("SMSConfig"));
#endregion
#region Line Notify<EFBFBD>t<EFBFBD>m
services.AddTransient<ISendLineNotifyService, SendLineNotifyService>();
#endregion
#endregion <EFBFBD>T<EFBFBD><EFBFBD><EFBFBD>q<EFBFBD><EFBFBD>
#region DBHelper <EFBFBD>`<EFBFBD>J
services.Configure<DBConfig>(configuration.GetSection("DBConfig"));
services.AddTransient<IDatabaseHelper, DatabaseHelper>();
#endregion DBHelper <EFBFBD>`<EFBFBD>J
#region Repository <EFBFBD>`<EFBFBD>J
services.AddTransient<IBackendRepository, BackendRepository>();
services.AddTransient<IBackgroundServiceRepository, BackgroundServiceRepository>();
services.AddTransient<IFrontendRepository, FrontendRepository>();
services.AddTransient<IBaseRepository, BaseRepository>();
#endregion Repository <EFBFBD>`<EFBFBD>J
//<2F>K<EFBFBD>[Quartz<74>A<EFBFBD><41>
services.AddTransient<IJobFactory, SingletonJobFactory>();
services.AddTransient<ISchedulerFactory, StdSchedulerFactory>();
services.AddHostedService<QuartzHostedService>();
#region <EFBFBD>I<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>p<EFBFBD>e(<EFBFBD>]<EFBFBD>w1min<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>@<EFBFBD><EFBFBD>)
services.AddSingleton<ExecutionBackgroundServicePlanJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(ExecutionBackgroundServicePlanJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ExecutionBackgroundServicePlanJob"))
);
#endregion
#region <EFBFBD>T<EFBFBD><EFBFBD><EFBFBD>q<EFBFBD><EFBFBD>(<EFBFBD>]<EFBFBD>w<EFBFBD>C 30min <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>@<EFBFBD><EFBFBD>)
services.AddSingleton<MessageNotificationJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(MessageNotificationJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:MessageNotificationJob"))
);
#endregion
#region <EFBFBD>w<EFBFBD>ɱN<EFBFBD>S<EFBFBD>w<EFBFBD><EFBFBD><EFBFBD>ƪ<EFBFBD><EFBFBD>[<EFBFBD>J<EFBFBD>ܬ<EFBFBD><EFBFBD>e<EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD>]<EFBFBD>w<EFBFBD>C<EFBFBD><EFBFBD> 2AM <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>@<EFBFBD><EFBFBD>)
services.AddSingleton<RegularUpdateDBTableJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(RegularUpdateDBTableJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:RegularUpdateDBTableJob"))
);
#endregion
#region <EFBFBD><EFBFBD><EFBFBD>Ƭ<EFBFBD><EFBFBD>e(<EFBFBD>]<EFBFBD>w<EFBFBD>C 5min <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>@<EFBFBD><EFBFBD>)
services.AddSingleton<DataDeliveryJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(DataDeliveryJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:DataDeliveryJob"))
);
#endregion
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޲z(<EFBFBD>]<EFBFBD>w<EFBFBD>C 5<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>@<EFBFBD><EFBFBD>)
services.AddSingleton<ParkingJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(ParkingJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ParkingJob"))
);
#endregion
#region <EFBFBD>q<EFBFBD><EFBFBD><EFBFBD>k<EFBFBD><EFBFBD>(<EFBFBD>]<EFBFBD>w<EFBFBD>C <EFBFBD>p<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>@<EFBFBD><EFBFBD>)
services.AddSingleton<ArchiveElectricMeterHourJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(ArchiveElectricMeterHourJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ArchiveElectricMeterHourJob"))
);
#endregion
#region <EFBFBD>q<EFBFBD><EFBFBD><EFBFBD>k<EFBFBD><EFBFBD>(<EFBFBD>]<EFBFBD>w<EFBFBD>C <EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>@<EFBFBD><EFBFBD>)
services.AddSingleton<ArchiveElectricMeterDayJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(ArchiveElectricMeterDayJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ArchiveElectricMeterDayJob"))
);
#endregion
#region <EFBFBD>w<EFBFBD>ɨ<EFBFBD><EFBFBD>o<EFBFBD><EFBFBD><EFBFBD>HAPI
services.AddSingleton<Quartz.Jobs.WeatherAPIJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(Quartz.Jobs.WeatherAPIJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:WeatherAPIJob"))
);
#endregion
}).ConfigureLogging((hostContext, logFactory) => {
IConfiguration configuration = hostContext.Configuration;
//logFactory.AddFile("Logs/log-{Date}.txt");
logFactory.AddFile(configuration.GetValue<string>("LoggerPath") + "/log-{Date}.txt");
});
}
}