Instance manager is now responsible for job manager

Prevents race conditions
This commit is contained in:
Cyberboss
2018-08-01 16:54:28 -04:00
parent ae16578ea6
commit 913d91464d
4 changed files with 19 additions and 8 deletions
@@ -41,6 +41,11 @@ namespace Tgstation.Server.Host.Components
/// </summary>
readonly IApplication application;
/// <summary>
/// The <see cref="IJobManager"/> for the <see cref="InstanceManager"/>
/// </summary>
readonly IJobManager jobManager;
/// <summary>
/// The <see cref="ILogger"/> for the <see cref="InstanceManager"/>
/// </summary>
@@ -62,13 +67,15 @@ namespace Tgstation.Server.Host.Components
/// <param name="ioManager">The value of <paramref name="ioManager"/></param>
/// <param name="databaseContextFactory">The value of <paramref name="databaseContextFactory"/></param>
/// <param name="application">The value of <see cref="application"/></param>
/// <param name="jobManager">The value of <see cref="jobManager"/></param>
/// <param name="logger">The value of <see cref="logger"/></param>
public InstanceManager(IInstanceFactory instanceFactory, IIOManager ioManager, IDatabaseContextFactory databaseContextFactory, IApplication application, ILogger<InstanceManager> logger)
public InstanceManager(IInstanceFactory instanceFactory, IIOManager ioManager, IDatabaseContextFactory databaseContextFactory, IApplication application, IJobManager jobManager, ILogger<InstanceManager> logger)
{
this.instanceFactory = instanceFactory ?? throw new ArgumentNullException(nameof(instanceFactory));
this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager));
this.databaseContextFactory = databaseContextFactory ?? throw new ArgumentNullException(nameof(databaseContextFactory));
this.application = application ?? throw new ArgumentNullException(nameof(application));
this.jobManager = jobManager ?? throw new ArgumentNullException(nameof(jobManager));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
instances = new Dictionary<long, IInstance>();
@@ -161,6 +168,7 @@ namespace Tgstation.Server.Host.Components
try
{
await databaseContext.Initialize(cancellationToken).ConfigureAwait(false);
await jobManager.StartAsync(cancellationToken).ConfigureAwait(false);
var dbInstances = databaseContext.Instances.Where(x => x.Online.Value)
.Include(x => x.RepositorySettings)
.Include(x => x.ChatSettings)
@@ -179,7 +187,11 @@ namespace Tgstation.Server.Host.Components
});
/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken) => Task.WhenAll(instances.Select(x => x.Value.StopAsync(cancellationToken)));
public async Task StopAsync(CancellationToken cancellationToken)
{
await Task.WhenAll(instances.Select(x => x.Value.StopAsync(cancellationToken))).ConfigureAwait(false);
await jobManager.StopAsync(cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc />
public IInteropContext Register(string accessIdentifier, IInteropConsumer consumer)
@@ -215,11 +215,9 @@ namespace Tgstation.Server.Host.Core
services.AddSingleton<InstanceManager>();
services.AddSingleton<IInstanceManager>(x => x.GetRequiredService<InstanceManager>());
services.AddSingleton<IHostedService>(x => x.GetRequiredService<InstanceManager>());
services.AddSingleton<JobManager>();
services.AddSingleton<IJobManager>(x => x.GetRequiredService<JobManager>());
services.AddSingleton<IHostedService>(x => x.GetRequiredService<JobManager>());
services.AddSingleton<IJobManager, JobManager>();
services.AddSingleton<IIOManager>(ioManager);
services.AddSingleton<DatabaseContextFactory>();
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
@@ -9,7 +10,7 @@ namespace Tgstation.Server.Host.Core
/// <summary>
/// Manages the runtime of <see cref="Job"/>s
/// </summary>
public interface IJobManager
public interface IJobManager : IHostedService
{
/// <summary>
/// Get the <see cref="Api.Models.Job.Progress"/> for a job
+1 -1
View File
@@ -10,7 +10,7 @@ using Tgstation.Server.Host.Models;
namespace Tgstation.Server.Host.Core
{
/// <inheritdoc />
sealed class JobManager : IHostedService, IJobManager, IDisposable
sealed class JobManager : IJobManager, IDisposable
{
/// <summary>
/// The <see cref="IServiceProvider"/> for the <see cref="JobManager"/>