Skip to content

Commit

Permalink
Better CLI Event Subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Feb 15, 2024
1 parent e84a481 commit 91cca30
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Shoko.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public static void Main()
settingsProvider.LoadSettings();
Utils.SettingsProvider = settingsProvider;
var startup = new Startup(logFactory.CreateLogger<Startup>(), settingsProvider);
startup.AboutToStart += (_, args) => AddEventHandlers(args.ServiceProvider);
startup.Start();
AddEventHandlers();
startup.WaitForShutdown();
}
catch (Exception e)
Expand All @@ -46,11 +46,11 @@ public static void Main()
}
}

private static void AddEventHandlers()
private static void AddEventHandlers(IServiceProvider provider)
{
Utils.YesNoRequired += OnUtilsOnYesNoRequired;
ServerState.Instance.PropertyChanged += OnInstanceOnPropertyChanged;
var queueStateEventHandler = Utils.ServiceContainer.GetRequiredService<QueueStateEventHandler>();
var queueStateEventHandler = provider.GetRequiredService<QueueStateEventHandler>();
queueStateEventHandler.QueueChanged += QueueStateEventHandlerOnQueueChanged;
}

Expand Down
5 changes: 2 additions & 3 deletions Shoko.Server/Scheduling/QueueStateEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public void InvokeQueueStarted()
QueueStarted?.Invoke(null, EventArgs.Empty);
}

// TODO fire when jobs are added and keep track of executing jobs
public void OnJobExecuting(IJobDetail jobDetail, QueueStateContext queueContext)
{
var job = _jobFactory.CreateJob(jobDetail);
Expand All @@ -42,7 +41,7 @@ public void OnJobExecuting(IJobDetail jobDetail, QueueStateContext queueContext)
{
new()
{
Key = jobDetail.Key.ToString(), JobType = job?.Name, Description = job?.Description.formatMessage()
Key = jobDetail.Key.ToString(), JobType = job?.Name ?? jobDetail.JobType.Name, Description = job?.Description.formatMessage()
}
},
WaitingJobsCount = queueContext.WaitingTriggersCount,
Expand All @@ -62,7 +61,7 @@ public void OnJobCompleted(IJobDetail jobDetail, QueueStateContext queueContext)
{
new()
{
Key = jobDetail.Key.ToString(), JobType = job?.Name, Description = job?.Description.formatMessage()
Key = jobDetail.Key.ToString(), JobType = job?.Name ?? jobDetail.JobType.Name, Description = job?.Description.formatMessage()
}
},
WaitingJobsCount = queueContext.WaitingTriggersCount,
Expand Down
4 changes: 2 additions & 2 deletions Shoko.Server/Scheduling/ThreadPooledJobStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,10 @@ private async Task JobFiringQueueEvents(ConnectionAndTransactionHolder conn, IJo
lock (_executingJobs)
executing = _executingJobs.Select(a =>
{
var job = _jobFactory.CreateJob(jobDetail);
var job = _jobFactory.CreateJob(a.Value);
return new QueueItem
{
Key = a.Key.ToString(), JobType = job?.Name, Description = job?.Description.formatMessage()
Key = a.Key.ToString(), JobType = job?.Name ?? a.Value.JobType.Name, Description = job?.Description.formatMessage()
};
}).ToArray();

Expand Down
8 changes: 8 additions & 0 deletions Shoko.Server/Server/ServerAboutToStartEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Shoko.Server.Server;

public class ServerAboutToStartEventArgs : EventArgs
{
public IServiceProvider ServiceProvider { get; init; }
}
5 changes: 5 additions & 0 deletions Shoko.Server/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Startup
private readonly ILogger<Startup> _logger;
private readonly ISettingsProvider _settingsProvider;
private IWebHost _webHost;
public event EventHandler<ServerAboutToStartEventArgs> AboutToStart;

public Startup(ILogger<Startup> logger, ISettingsProvider settingsProvider)
{
Expand Down Expand Up @@ -108,6 +109,10 @@ private bool StartWebHost(ISettingsProvider settingsProvider)
try
{
_webHost ??= InitWebHost(settingsProvider);
AboutToStart?.Invoke(null, new ServerAboutToStartEventArgs
{
ServiceProvider = Utils.ServiceContainer
});
_webHost.Start();
return true;
}
Expand Down

0 comments on commit 91cca30

Please sign in to comment.