定时作业修改

usertest
cjy 3 months ago
parent 19797c5919
commit f20ac7c907

@ -9,20 +9,20 @@ using Microsoft.AspNetCore.Mvc;
namespace DS.WMS.JobService namespace DS.WMS.JobService
{ {
public sealed class ArgumentExceptionHandler : IExceptionHandler //public sealed class ArgumentExceptionHandler : IExceptionHandler
{ //{
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken) // public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
{ // {
httpContext.RequestServices.GetRequiredService<ILogger<ArgumentExceptionHandler>>() // httpContext.RequestServices.GetRequiredService<ILogger<ArgumentExceptionHandler>>()
.LogError(exception, "Exception handled"); // .LogError(exception, "Exception handled");
if (exception is not ArgumentException) return false; // if (exception is not ArgumentException) return false;
httpContext.Response.StatusCode = 400; // httpContext.Response.StatusCode = 400;
await httpContext.Response.WriteAsJsonAsync(new // await httpContext.Response.WriteAsJsonAsync(new
{ // {
exception.Message // exception.Message
}, cancellationToken); // }, cancellationToken);
return true; // return true;
} // }
} //}
} }

@ -0,0 +1,35 @@
using Hangfire.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DS.WMS.JobService
{
public class GlobalExceptionFilter : IServerFilter
{
public void OnPerforming(PerformingContext context)
{
// 不需要在这里做任何事情
}
public void OnPerformed(PerformedContext context)
{
Console.WriteLine(context.BackgroundJob);
// 当job执行完毕后检查是否有异常
//if (context.BackgroundJob.State == JobState.Failed)
//{
// // 获取异常信息
// var exception = context.Exception;
// if (exception != null)
// {
// // 处理异常,例如发送邮件、记录日志等
// // 这里可以使用依赖注入来处理异常如记录日志到Logging Service
// Console.WriteLine($"Job failed with exception: {exception.Message}");
// }
//}
}
}
}

@ -11,20 +11,22 @@ using DS.WMS.Core.HangfireJob.Interface;
using DS.WMS.Core.HangfireJob.Method; using DS.WMS.Core.HangfireJob.Method;
var builder = Host.CreateApplicationBuilder(args); var builder = Host.CreateApplicationBuilder(args);
//允许 BackgroundService 中存在未经处理的异常,不停止主机
var environment = builder.Environment.EnvironmentName; builder.Services.Configure<HostOptions>(hostOptions =>
Console.WriteLine("当前开发环境:" + environment); {
hostOptions.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore;
});
//应用作为 Windows 服务
builder.Services.AddWindowsService();
//×¢²áÅäÖà //×¢²áÅäÖÃ
builder.Configuration builder.Configuration
// .SetBasePath(builder.Environment.ContentRootPath)
.AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true)
// .AddJsonFile(path: $"appsettings.{environment}.json", optional: true, reloadOnChange: true)
.Build(); .Build();
builder.Configuration.AddEnvironmentVariables(); builder.Configuration.AddEnvironmentVariables();
//Autofac×¢Èë //Autofac×¢Èë
builder.ConfigureContainer(new AutofacServiceProviderFactory(), builder => builder.RegisterModule(new AutofacModuleRegister())); builder.ConfigureContainer(new AutofacServiceProviderFactory(), builder => builder.RegisterModule(new AutofacModuleRegister()));
builder.Services.AddExceptionHandler<ArgumentExceptionHandler>(); //全局异常 //builder.Services.AddExceptionHandler<ArgumentExceptionHandler>(); //全局异常
builder.Services.AddUserModuleInstall(); //Óû§·þÎñ builder.Services.AddUserModuleInstall(); //Óû§·þÎñ
builder.Services.AddRedisModuleInstall();//redis builder.Services.AddRedisModuleInstall();//redis
builder.Services.AddSqlSugarInstall(); builder.Services.AddSqlSugarInstall();
@ -34,14 +36,6 @@ builder.Services.AddWorkServiceHangfireModuleInstall();
builder.Services.AddHostedService<Worker>(); builder.Services.AddHostedService<Worker>();
//允许 BackgroundService 中存在未经处理的异常,不停止主机
builder.Services.Configure<HostOptions>(hostOptions =>
{
hostOptions.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore;
});
//应用作为 Windows 服务
builder.Services.AddWindowsService();
var host = builder.Build(); var host = builder.Build();
host.Run(); host.Run();

@ -25,7 +25,8 @@ namespace DS.WMS.JobService
{ {
try try
{ {
_server = new BackgroundJobServer(options); _server = new BackgroundJobServer(options);
//_server = new BackgroundJobServer();
Console.WriteLine("Hangfire Server started. Press any key to exit..."); Console.WriteLine("Hangfire Server started. Press any key to exit...");
Console.ReadKey(); Console.ReadKey();
} }

@ -9,7 +9,6 @@
"DefaultDbConnId": "1288018625843826688", "DefaultDbConnId": "1288018625843826688",
"DefaultDbType": 0, "DefaultDbType": 0,
"DefaultDbString": "server=rm-m5e06xxqpa68a68ry5o.mysql.rds.aliyuncs.com;port=3306;uid=rulesengine_admin;pwd=Rule1qaz2wsx!QAZ;database=shippingweb8_dev", "DefaultDbString": "server=rm-m5e06xxqpa68a68ry5o.mysql.rds.aliyuncs.com;port=3306;uid=rulesengine_admin;pwd=Rule1qaz2wsx!QAZ;database=shippingweb8_dev",
"HangfireDbString": "server=rm-m5e06xxqpa68a68ry5o.mysql.rds.aliyuncs.com;port=3306;uid=rulesengine_admin;pwd=Rule1qaz2wsx!QAZ;database=shippingweb8_hangfire",
"DBS": [ "DBS": [
{ {
"ConnId": "1288018625843826680", "ConnId": "1288018625843826680",

Loading…
Cancel
Save