Rea*_*ath 2 c# asp.net-mvc asp.net-mvc-5 hangfire
我有一个长时间运行的方法,我决定使用 HangFire 在后台执行并通过仪表板管理它们。我使用以下代码设置了仪表板和 Hangfire 本身:
HangFireConfiguration.cs
public static class HangFireConfiguration
{
public static string DashboardUrl = "/tasks";
public static void Configure(IAppBuilder app)
{
GlobalConfiguration.Configuration.UseSqlServerStorage("DefaultConnection");
var options = new DashboardOptions
{
Authorization = new[]
{
new HangFireAuthorization()
}
};
app.UseHangfireDashboard(DashboardUrl, options);
app.UseHangfireServer(new BackgroundJobServerOptions()
{
//ShutdownTimeout = TimeSpan.FromHours(24)
});
}
}
Run Code Online (Sandbox Code Playgroud)
HangFireAuthorization.cs
public class HangFireAuthorization : IDashboardAuthorizationFilter
{
public static string DashboardUrl = "/tasks";
public bool Authorize([NotNull] DashboardContext context)
{
if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
{
return HttpContext.Current.User.IsInRole("admin");
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
这是代码:
public static void FireMakeArbitarge(LimitBuySellOrdersBindingModel model)
{
BackgroundJob.Enqueue(() => MakeArbitrage(model));
}
public static async void MakeArbitrage(LimitBuySellOrdersBindingModel model)
{
var orders = await PlaceOrders(model);
Order buyOrder = orders[0];
Order sellOrder = orders[1];
if (buyOrder == null || sellOrder == null)
{
return; //Problem
}
ListenOrdersAndCreateArbitrage(buyOrder, sellOrder);
}
private static async Task<Order[]> PlaceOrders(LimitBuySellOrdersBindingModel model)
{
SignalRUtility.ShowInfoNotificationToAdmins("Executing Buy and Sell Orders", 2);
var buyOrderTask = Task<Order>.Factory.StartNew(() => TryPlacingOrder(model.BuyOrder));
var sellOrderTask = Task<Order>.Factory.StartNew(() => TryPlacingOrder(model.SellOrder));
var orders = await Task.WhenAll(buyOrderTask, sellOrderTask);
return orders;
}
private static Order TryPlacingOrder(LimitOrderBindingModel model)
{
try
{
var order = OrdersUtility.PlaceOrder(model);
return order;
}
catch (Exception e)
{
return null;
}
}
private static void ListenOrdersAndCreateArbitrage(Order buyOrder, Order sellOrder)
{
var arbitrage = new Arbitrage()
{
BuyOrder = buyOrder,
SellOrder = sellOrder
};
var isBuyFulfilled = false;
var isSellFulfilled = false;
using (var context = new CryptoStatisticsDbContext())
{
context.Orders.Attach(buyOrder);
context.Orders.Attach(sellOrder);
while (true)
{
if (!isBuyFulfilled)
{
...
}
if (!isSellFulfilled)
{
...
}
if (isSellFulfilled && isBuyFulfilled)
{
...
break; //This moment always comes, most often under 3-4 seconds, but theoritically can take up to 24h to finish
}
await Task.Delay(1000);
}
...
SignalRUtility.ShowSuccessNotificationToAdmins("Arbitrage successfuly made");
}
}
Run Code Online (Sandbox Code Playgroud)
我正在FireMakeArbitrage(model)使用 SignalR 调用:
public void MakeArbitrage(LimitBuySellOrdersBindingModel model)
{
ArbitrageUtility.FireMakeArbitarge(model);
}
Run Code Online (Sandbox Code Playgroud)
通过调试器我可以看到它FireMakeArbitrage()确实被调用,因此BackgroundJob.Enqueue(() => MakeArbitrage(model));运行,但绝对没有任何反应。数据库的列中没有添加任何内容,我也无法在仪表板中看到任何作业。
首先,我认为这与异步方法和方法中的任务调用有关,因此我尝试了以下操作:仅在作业中运行 while (true) 循环。
public static async void MakeArbitrage(LimitBuySellOrdersBindingModel model)
{
var orders = await PlaceOrders(model);
Order buyOrder = orders[0];
Order sellOrder = orders[1];
if (buyOrder == null || sellOrder == null)
{
return; //Problem
}
BackgroundJob.Enqueue(() => ListenOrdersAndCreateArbitrage(buyOrder, sellOrder));
}
Run Code Online (Sandbox Code Playgroud)
但又什么也没发生。
然后我认为我的 Hangfire 设置有问题,所以我尝试了一个简单的方法
BackgroundJob.Enqueue(() => SignalRUtility.ShowSuccessNotificationToAdmins("test"));
Run Code Online (Sandbox Code Playgroud)
这很有效,我收到了通知,并且可以在仪表板中看到成功的作业。所以 Hangfire 正在工作(所有表都正常,仪表板工作),但是当我尝试调用我的方法时,我收到此日志,运行时没有发生任何事情(我的断点从未被命中):
2018-01-18 16:16:20.9220 INFO | Start installing Hangfire SQL objects...
2018-01-18 16:16:20.9910 INFO | Hangfire SQL objects installed.
2018-01-18 16:16:21.0150 INFO | Starting Hangfire Server
2018-01-18 16:16:21.0150 INFO | Using job storage: 'SQL Server: .@Market'
2018-01-18 16:16:21.0150 INFO | Using the following options for SQL Server job storage:
2018-01-18 16:16:21.0150 INFO | Queue poll interval: 00:00:15.
2018-01-18 16:16:21.0150 INFO | Using the following options for Hangfire Server:
2018-01-18 16:16:21.0150 INFO | Worker count: 20
2018-01-18 16:16:21.0150 INFO | Listening queues: 'default'
2018-01-18 16:16:21.0150 INFO | Shutdown timeout: 00:00:15
2018-01-18 16:16:21.0150 INFO | Schedule polling interval: 00:00:15
2018-01-18 16:16:21.0410 DEBUG | Background process 'BackgroundProcessingServer' started.
2018-01-18 16:16:21.2490 DEBUG | Background process 'ServerHeartbeat' started.
2018-01-18 16:16:21.2800 DEBUG | Background process 'ServerWatchdog' started.
2018-01-18 16:16:21.2950 DEBUG | Background process 'Hangfire.SqlServer.ExpirationManager' started.
2018-01-18 16:16:21.2950 DEBUG | Removing outdated records from the 'AggregatedCounter' table...
2018-01-18 16:16:21.2950 INFO | 2 servers were removed due to timeout
2018-01-18 16:16:21.3260 DEBUG | Background process 'Hangfire.SqlServer.CountersAggregator' started.
2018-01-18 16:16:21.3260 DEBUG | Aggregating records in 'Counter' table...
2018-01-18 16:16:21.3260 DEBUG | Background process 'Worker #6e2ae928' started.
2018-01-18 16:16:21.3580 TRACE | Records from the 'Counter' table aggregated.
2018-01-18 16:16:21.3580 DEBUG | Background process 'Worker #3c3363a2' started.
2018-01-18 16:16:21.3580 TRACE | Outdated records removed from the 'AggregatedCounter' table.
2018-01-18 16:16:21.3580 DEBUG | Removing outdated records from the 'Job' table...
2018-01-18 16:16:21.3710 DEBUG | Background process 'Worker #50dfa590' started.
2018-01-18 16:16:21.3920 DEBUG | Background process 'Worker #6ad9aeb9' started.
2018-01-18 16:16:21.4140 DEBUG | Background process 'Worker #67fc5308' started.
2018-01-18 16:16:21.4240 TRACE | Outdated records removed from the 'Job' table.
2018-01-18 16:16:21.4240 DEBUG | Removing outdated records from the 'List' table...
2018-01-18 16:16:21.4360 DEBUG | Background process 'Worker #bbc2bf4e' started.
2018-01-18 16:16:21.4360 DEBUG | Background process 'Worker #115c5b69' started.
2018-01-18 16:16:21.4620 DEBUG | Background process 'Worker #43452cde' started.
2018-01-18 16:16:21.4640 TRACE | Outdated records removed from the 'List' table.
2018-01-18 16:16:21.4640 DEBUG | Removing outdated records from the 'Set' table...
2018-01-18 16:16:21.4790 DEBUG | Background process 'Worker #1447d454' started.
2018-01-18 16:16:21.4970 TRACE | Outdated records removed from the 'Set' table.
2018-01-18 16:16:21.4970 DEBUG | Removing outdated records from the 'Hash' table...
2018-01-18 16:16:21.4970 DEBUG | Background process 'Worker #363b3748' started.
2018-01-18 16:16:21.5220 DEBUG | Background process 'Worker #9e713cc5' started.
2018-01-18 16:16:21.5320 DEBUG | Background process 'Worker #82a5eb5f' started.
2018-01-18 16:16:21.5320 TRACE | Outdated records removed from the 'Hash' table.
2018-01-18 16:16:21.5610 DEBUG | Background process 'Worker #e3d98a1b' started.
2018-01-18 16:16:21.5770 DEBUG | Background process 'Worker #801593ad' started.
2018-01-18 16:16:21.5940 DEBUG | Background process 'Worker #5b7fdcd6' started.
2018-01-18 16:16:21.6170 DEBUG | Background process 'Worker #5506c4bf' started.
2018-01-18 16:16:21.6370 DEBUG | Background process 'Worker #0ed1b5a8' started.
2018-01-18 16:16:21.6640 DEBUG | Background process 'Worker #d3fdbe7b' started.
2018-01-18 16:16:21.6780 DEBUG | Background process 'Worker #ada59522' started.
2018-01-18 16:16:21.7010 DEBUG | Background process 'Worker #1e669bd5' started.
2018-01-18 16:16:21.7470 DEBUG | Background process 'RecurringJobScheduler' started.
2018-01-18 16:16:21.7470 DEBUG | Background process 'DelayedJobScheduler' started.
Run Code Online (Sandbox Code Playgroud)
我认为问题在于这一行:
2018-01-18 16:16:21.2950 INFO | 2 servers were removed due to timeout。首先,我不知道为什么有2台服务器。其次,它们在我调用该方法时被删除,我不明白为什么超时。删除它们并尝试再次调用该方法后,文件中不再记录任何内容。我必须重新启动应用程序并重试
2018-01-18 16:21:22.1050 INFO | Start installing Hangfire SQL objects...
2018-01-18 16:21:22.2440 INFO | Hangfire SQL objects installed.
2018-01-18 16:21:22.2720 INFO | Starting Hangfire Server
2018-01-18 16:21:22.2720 INFO | Using job storage: 'SQL Server: .@Market'
2018-01-18 16:21:22.2720 INFO | Using the following options for SQL Server job storage:
2018-01-18 16:21:22.2720 INFO | Queue poll interval: 00:00:15.
2018-01-18 16:21:22.2720 INFO | Using the following options for Hangfire Server:
2018-01-18 16:21:22.2720 INFO | Worker count: 20
2018-01-18 16:21:22.2720 INFO | Listening queues: 'default'
2018-01-18 16:21:22.2720 INFO | Shutdown timeout: 00:00:15
2018-01-18 16:21:22.2720 INFO | Schedule polling interval: 00:00:15
2018-01-18 16:21:22.3010 DEBUG | Background process 'BackgroundProcessingServer' started.
2018-01-18 16:21:22.5390 DEBUG | Background process 'ServerHeartbeat' started.
2018-01-18 16:21:22.5710 DEBUG | Background process 'ServerWatchdog' started.
2018-01-18 16:21:22.5840 DEBUG | Background process 'Hangfire.SqlServer.CountersAggregator' started.
2018-01-18 16:21:22.5840 DEBUG | Background process 'Hangfire.SqlServer.ExpirationManager' started.
2018-01-18 16:21:22.6130 DEBUG | Background process 'Worker #bb9381c4' started.
2018-01-18 16:21:22.6130 DEBUG | Removing outdated records from the 'AggregatedCounter' table...
2018-01-18 16:21:22.6130 DEBUG | Aggregating records in 'Counter' table...
2018-01-18 16:21:22.6470 DEBUG | Background process 'Worker #c0f3faa7' started.
2018-01-18 16:21:22.6780 DEBUG | Background process 'Worker #d4845d7a' started.
2018-01-18 16:21:22.6880 TRACE | Outdated records removed from the 'AggregatedCounter' table.
2018-01-18 16:21:22.6880 DEBUG | Removing outdated records from the 'Job' table...
2018-01-18 16:21:22.6880 TRACE | Records from the 'Counter' table aggregated.
2018-01-18 16:21:22.7250 DEBUG | Background process 'Worker #602982ac' started.
2018-01-18 16:21:22.7350 DEBUG | Background process 'Worker #82e492b8' started.
2018-01-18 16:21:22.7510 DEBUG | Background process 'Worker #9f981a7f' started.
2018-01-18 16:21:22.7670 TRACE | Outdated records removed from the 'Job' table.
2018-01-18 16:21:22.7670 DEBUG | Removing outdated records from the 'List' table...
2018-01-18 16:21:22.7800 DEBUG | Background process 'Worker #88198e45' started.
2018-01-18 16:21:22.7800 DEBUG | Background process 'Worker #c2fd7d9c' started.
2018-01-18 16:21:22.8030 DEBUG | Background process 'Worker #74c1711d' started.
2018-01-18 16:21:22.8170 TRACE | Outdated records removed from the 'List' table.
2018-01-18 16:21:22.8170 DEBUG | Removing outdated records from the 'Set' table...
2018-01-18 16:21:22.8760 DEBUG | Background process 'Worker #18785ae7' started.
2018-01-18 16:21:22.8920 DEBUG | Background process 'Worker #cd2e512e' started.
2018-01-18 16:21:22.9040 TRACE | Outdated records removed from the 'Set' table.
2018-01-18 16:21:22.9040 DEBUG | Removing outdated records from the 'Hash' table...
2018-01-18 16:21:22.9190 DEBUG | Background process 'Worker #11b64efe' started.
2018-01-18 16:21:22.9560 DEBUG | Background process 'Worker #506dc90a' started.
2018-01-18 16:21:22.9680 DEBUG | Background process 'Worker #a1ff6574' started.
2018-01-18 16:21:22.9680 TRACE | Outdated records removed from the 'Hash' table.
2018-01-18 16:21:22.9880 DEBUG | Background process 'Worker #48f5af2a' started.
2018-01-18 16:21:23.0080 DEBUG | Background process 'Worker #23240705' started.
2018-01-18 16:21:23.0190 DEBUG | Background process 'Worker #ddf2e718' started.
2018-01-18 16:21:23.0400 DEBUG | Background process 'Worker #c8fd3a14' started.
2018-01-18 16:21:23.0510 DEBUG | Background process 'Worker #9cbb1e99' started.
2018-01-18 16:21:23.0730 DEBUG | Background process 'Worker #eeaed48d' started.
2018-01-18 16:21:23.0770 DEBUG | Background process 'DelayedJobScheduler' started.
2018-01-18 16:21:23.1120 DEBUG | Background process 'RecurringJobScheduler' started.
2018-01-18 16:26:22.5900 INFO | 1 servers were removed due to timeout
2018-01-18 16:26:22.7060 DEBUG | Aggregating records in 'Counter' table...
2018-01-18 16:26:22.7060 TRACE | Records from the 'Counter' table aggregated.
Run Code Online (Sandbox Code Playgroud)
编辑:一目了然:
public static async void MakeArbitrage(LimitBuySellOrdersBindingModel model)
{
var msg = "this works";
BackgroundJob.Enqueue(() => SignalRUtility.ShowSuccessNotificationToAdmins(msg, 60));
}
Run Code Online (Sandbox Code Playgroud)
然而这不起作用:
public static async void MakeArbitrage(LimitBuySellOrdersBindingModel model)
{
var orders = await PlaceOrders(model);
Order buyOrder = orders[0];
Order sellOrder = orders[1];
if (buyOrder == null || sellOrder == null)
{
return; //Problem
}
BackgroundJob.Enqueue(() => ListenOrdersAndCreateArbitrage(buyOrder, sellOrder));
}
Run Code Online (Sandbox Code Playgroud)
在这两个示例中,我都是这样调用该方法的:ArbitrageUtility.MakeArbitrage(model);
| 归档时间: |
|
| 查看次数: |
5366 次 |
| 最近记录: |