Skip to content

Commit

Permalink
adding back hangfire
Browse files Browse the repository at this point in the history
  • Loading branch information
scottAtTai committed Mar 19, 2023
1 parent aad3f34 commit 655ddb4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 55 deletions.
12 changes: 12 additions & 0 deletions EQToolApis/DB/Models/EQitem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@ public class EQitem

[MaxLength(64)]
public string ItemName { get; set; }

public int TotalAuctionCount { get; set; }

public int TotalLast30DaysCount { get; set; }

public int TotalLast60DaysCount { get; set; }

public int TotalLast90DaysCount { get; set; }

public int TotalLast6MonthsCount { get; set; }

public int TotalLastYearCount { get; set; }
}
}
2 changes: 2 additions & 0 deletions EQToolApis/EQToolApis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.8.2" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.33" />
<PackageReference Include="Hangfire.SqlServer" Version="1.7.33" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.15.0" />
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="5.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
Expand Down
42 changes: 39 additions & 3 deletions EQToolApis/Program.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
using EQToolApis.DB;
using EQToolApis.Services;
using Hangfire;
using Hangfire.SqlServer;
using Microsoft.EntityFrameworkCore;
using static EQToolApis.Services.DiscordService;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddDbContext<EQToolContext>(opts => opts.UseSqlServer(builder.Configuration.GetConnectionString("eqtooldb"))).AddScoped<EQToolContext>();
builder.Services.AddRazorPages();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(builder.Configuration.GetConnectionString("HangfireConnection"), new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true,
DashboardJobListLimit = 2
}));
builder.Services.AddHangfireServer();
builder.Services.Configure<DiscordServiceOptions>(options =>
{
options.token = builder.Configuration.GetValue<string>("DiscordToken");
})
.AddSingleton<IDiscordService, DiscordService>();
builder.Services.AddHostedService<TimedHostedService>();

builder.Services.AddMvc();
var app = builder.Build();
Expand All @@ -27,6 +40,29 @@

app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHangfireDashboard();
});
app.MapControllers();
app.MapRazorPages();

var isrelease = false;

#if !DEBUG
isrelease = true;
#endif

if (isrelease)
{
using (var scope = app.Services.CreateScope())
{
var backgroundclient = scope.ServiceProvider.GetRequiredService<IRecurringJobManager>();
backgroundclient.AddOrUpdate<DiscordService.DiscordJob>(nameof(DiscordService.DiscordJob.ReadFutureMessages), (a) => a.ReadFutureMessages(), Cron.Minutely());
backgroundclient.AddOrUpdate<DiscordService.DiscordJob>(nameof(DiscordService.DiscordJob.ReadPastMessages), (a) => a.ReadPastMessages(), Cron.Minutely());
backgroundclient.AddOrUpdate<DiscordService.DiscordJob>(nameof(DiscordService.DiscordJob.StartItemPricing), (a) => a.StartItemPricing(), Cron.Daily());
}
}
app.Run();
70 changes: 19 additions & 51 deletions EQToolApis/Services/Discrod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,28 +169,19 @@ public List<Message> ReadMessageHistory(long? lastid)
}
}

public class TimedHostedService : IHostedService, IDisposable
public class DiscordJob
{
private readonly ILogger<TimedHostedService> _logger;
private Timer? _timer = null;
private readonly ILogger<DiscordJob> _logger;
private readonly IDiscordService discordService;
private readonly IServiceProvider services;
private bool Processing = false;
private readonly EQToolContext dbcontext;

public TimedHostedService(ILogger<TimedHostedService> logger, IDiscordService discordService, IServiceProvider services)
public DiscordJob(ILogger<DiscordJob> logger, IDiscordService discordService, EQToolContext dbcontext)
{
this.services = services;
this.dbcontext = dbcontext;
_logger = logger;
this.discordService = discordService;
}

public Task StartAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Timed Hosted Service running.");
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(10));
return Task.CompletedTask;
}

private void AddMessages(List<Message> messages, EQToolContext dbcontext)
{
foreach (var item in messages)
Expand Down Expand Up @@ -241,50 +232,27 @@ private void AddMessages(List<Message> messages, EQToolContext dbcontext)
}
_ = dbcontext.SaveChanges();
}
private void DoWork(object? state)
{
if (Processing)
{
return;
}
#if DEBUG
return;
#endif
Processing = true;
try
{
discordService.Login();
using (var scope = services.CreateScope())
{
var dbcontext = scope.ServiceProvider.GetRequiredService<EQToolContext>();
var lastidread = dbcontext.EQTunnelMessages.Where(a => a.Server == Servers.Green).Select(a => (long?)a.DiscordMessageId).OrderByDescending(a => a).FirstOrDefault();
AddMessages(discordService.ReadMessages(lastidread), dbcontext);
lastidread = dbcontext.EQTunnelMessages.Where(a => a.Server == Servers.Green).Select(a => (long?)a.DiscordMessageId).OrderBy(a => a).FirstOrDefault();
AddMessages(discordService.ReadMessageHistory(lastidread), dbcontext);
}

_logger.LogInformation("Timed Hosted Service is working.");
}
catch (Exception e)
{
_logger.LogError(e.ToString());
}
finally
{
Processing = false;
}
public void ReadFutureMessages()
{
discordService.Login();
var lastidread = dbcontext.EQTunnelMessages.Where(a => a.Server == Servers.Green).Select(a => (long?)a.DiscordMessageId).OrderByDescending(a => a).FirstOrDefault();
AddMessages(discordService.ReadMessages(lastidread), dbcontext);
}

public Task StopAsync(CancellationToken stoppingToken)
public void ReadPastMessages()
{
_logger.LogInformation("Timed Hosted Service is stopping.");
_ = (_timer?.Change(Timeout.Infinite, 0));
return Task.CompletedTask;
discordService.Login();
var lastidread = dbcontext.EQTunnelMessages.Where(a => a.Server == Servers.Green).Select(a => (long?)a.DiscordMessageId).OrderBy(a => a).FirstOrDefault();
AddMessages(discordService.ReadMessageHistory(lastidread), dbcontext);
}

public void Dispose()
public void StartItemPricing()
{
_timer?.Dispose();
return;
discordService.Login();
var lastidread = dbcontext.EQTunnelMessages.Where(a => a.Server == Servers.Green).Select(a => (long?)a.DiscordMessageId).OrderBy(a => a).FirstOrDefault();
AddMessages(discordService.ReadMessageHistory(lastidread), dbcontext);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion EQToolApis/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Microsoft.AspNetCore": "Warning",
"Hangfire": "Information"
}
},
"AllowedHosts": "*",
Expand Down

0 comments on commit 655ddb4

Please sign in to comment.