Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from IOptions to IOptionsSnapshot and IOptionsMonitor #1931

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/check-pr-has-milestone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ concurrency:

jobs:
fail-on-bad-milestone:
if: github.event.pull_request.draft != true
name: Fail if Pull Request has no Associated Version Milestone
runs-on: ubuntu-latest
steps:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci-security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ concurrency:
jobs:
security-checkpoint:
name: Check CI Clearance
if: github.event_name == 'pull_request_target' && (github.event.pull_request.head.repo.id != github.event.pull_request.base.repo.id || github.event.pull_request.user.id == 49699333) && github.event.pull_request.state == 'open'
if: github.event_name == 'pull_request_target' && (github.event.pull_request.head.repo.id != github.event.pull_request.base.repo.id || github.event.pull_request.user.id == 49699333) && github.event.pull_request.state == 'open' && github.event.pull_request.draft != true
runs-on: ubuntu-latest
steps:
- name: Generate App Token
Expand Down Expand Up @@ -60,7 +60,7 @@ jobs:
ci-pipline-workflow-call:
name: CI Pipeline
needs: security-checkpoint
if: (!(cancelled() || failure()) && (needs.security-checkpoint.result == 'success' || (github.event_name != 'pull_request_target' && github.event.pull_request.head.repo.id == github.event.pull_request.base.repo.id && github.event.pull_request.user.id != 49699333)))
if: (!(cancelled() || failure()) && (needs.security-checkpoint.result == 'success' || (github.event_name != 'pull_request_target' && github.event.pull_request.head.repo.id == github.event.pull_request.base.repo.id && github.event.pull_request.user.id != 49699333)) && github.event.pull_request.draft != true)
uses: ./.github/workflows/ci-pipeline.yml
secrets: inherit
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

using Remora.Discord.API.Abstractions.Gateway.Commands;
using Remora.Discord.API.Abstractions.Gateway.Events;
Expand Down Expand Up @@ -72,9 +73,9 @@ public override string BotMention
readonly IAssemblyInformationProvider assemblyInformationProvider;

/// <summary>
/// The <see cref="GeneralConfiguration"/> for the <see cref="DiscordProvider"/>.
/// The <see cref="GeneralConfiguration"/> <see cref="IOptionsMonitor{TOptions}"/> for the <see cref="DiscordProvider"/>.
/// </summary>
readonly GeneralConfiguration generalConfiguration;
readonly IOptionsMonitor<GeneralConfiguration> generalConfigurationOptions;

/// <summary>
/// The <see cref="ServiceProvider"/> containing Discord services.
Expand Down Expand Up @@ -141,18 +142,18 @@ public override string BotMention
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="Provider"/>.</param>
/// <param name="assemblyInformationProvider">The value of <see cref="assemblyInformationProvider"/>.</param>
/// <param name="chatBot">The <see cref="ChatBot"/> for the <see cref="Provider"/>.</param>
/// <param name="generalConfiguration">The value of <see cref="generalConfiguration"/>.</param>
/// <param name="generalConfigurationOptions">The value of <see cref="generalConfigurationOptions"/>.</param>
public DiscordProvider(
IJobManager jobManager,
IAsyncDelayer asyncDelayer,
ILogger<DiscordProvider> logger,
IAssemblyInformationProvider assemblyInformationProvider,
ChatBot chatBot,
GeneralConfiguration generalConfiguration)
IOptionsMonitor<GeneralConfiguration> generalConfigurationOptions,
ChatBot chatBot)
: base(jobManager, asyncDelayer, logger, chatBot)
{
this.assemblyInformationProvider = assemblyInformationProvider ?? throw new ArgumentNullException(nameof(assemblyInformationProvider));
this.generalConfiguration = generalConfiguration ?? throw new ArgumentNullException(nameof(generalConfiguration));
this.generalConfigurationOptions = generalConfigurationOptions ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));

mappedChannels = new List<ulong>();
connectDisconnectLock = new object();
Expand Down Expand Up @@ -921,7 +922,7 @@ List<IEmbedField> BuildUpdateEmbedFields(
true),
EngineType.OpenDream => new EmbedField(
"OpenDream Version",
$"[{engineVersion.SourceSHA![..7]}]({generalConfiguration.OpenDreamGitUrl}/commit/{engineVersion.SourceSHA})",
$"[{engineVersion.SourceSHA![..7]}]({generalConfigurationOptions.CurrentValue.OpenDreamGitUrl}/commit/{engineVersion.SourceSHA})",
true),
_ => throw new InvalidOperationException($"Invaild EngineType: {engineVersion.Engine.Value}"),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using Meebey.SmartIrc4net;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

using Newtonsoft.Json;

Expand Down Expand Up @@ -91,7 +92,7 @@ sealed class IrcProvider : Provider
/// <summary>
/// The <see cref="FileLoggingConfiguration"/> for the <see cref="IrcProvider"/>.
/// </summary>
readonly FileLoggingConfiguration loggingConfiguration;
readonly IOptionsMonitor<FileLoggingConfiguration> loggingConfigurationOptions;

/// <summary>
/// The <see cref="IrcFeatures"/> client.
Expand All @@ -116,18 +117,18 @@ sealed class IrcProvider : Provider
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="Provider"/>.</param>
/// <param name="assemblyInformationProvider">The <see cref="IAssemblyInformationProvider"/> to get the <see cref="IAssemblyInformationProvider.VersionString"/> from.</param>
/// <param name="chatBot">The <see cref="Models.ChatBot"/> for the <see cref="Provider"/>.</param>
/// <param name="loggingConfiguration">The <see cref="FileLoggingConfiguration"/> for the <see cref="Provider"/>.</param>
/// <param name="loggingConfigurationOptions">The value of <see cref="loggingConfigurationOptions"/>.</param>
public IrcProvider(
IJobManager jobManager,
IAsyncDelayer asyncDelayer,
ILogger<IrcProvider> logger,
IAssemblyInformationProvider assemblyInformationProvider,
Models.ChatBot chatBot,
FileLoggingConfiguration loggingConfiguration)
IAssemblyInformationProvider assemblyInformationProvider,
IOptionsMonitor<FileLoggingConfiguration> loggingConfigurationOptions)
: base(jobManager, asyncDelayer, logger, chatBot)
{
ArgumentNullException.ThrowIfNull(assemblyInformationProvider);
ArgumentNullException.ThrowIfNull(loggingConfiguration);
ArgumentNullException.ThrowIfNull(loggingConfigurationOptions);

var builder = chatBot.CreateConnectionStringBuilder();
if (builder == null || !builder.Valid || builder is not IrcConnectionStringBuilder ircBuilder)
Expand All @@ -142,7 +143,7 @@ public IrcProvider(
passwordType = ircBuilder.PasswordType;

assemblyInfo = assemblyInformationProvider ?? throw new ArgumentNullException(nameof(assemblyInformationProvider));
this.loggingConfiguration = loggingConfiguration ?? throw new ArgumentNullException(nameof(loggingConfiguration));
this.loggingConfigurationOptions = loggingConfigurationOptions ?? throw new ArgumentNullException(nameof(loggingConfigurationOptions));

client = InstantiateClient();

Expand Down Expand Up @@ -741,7 +742,7 @@ IrcFeatures InstantiateClient()
newClient.OnChannelMessage += Client_OnChannelMessage;
newClient.OnQueryMessage += Client_OnQueryMessage;

if (loggingConfiguration.ProviderNetworkDebug)
if (loggingConfigurationOptions.CurrentValue.ProviderNetworkDebug)
{
newClient.OnReadLine += (sender, e) => Logger.LogTrace("READ: {line}", e.Line);
newClient.OnWriteLine += (sender, e) => Logger.LogTrace("WRITE: {line}", e.Line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ sealed class ProviderFactory : IProviderFactory
readonly ILoggerFactory loggerFactory;

/// <summary>
/// The <see cref="GeneralConfiguration"/> for the <see cref="ProviderFactory"/>.
/// The <see cref="GeneralConfiguration"/> <see cref="IOptionsMonitor{TOptions}"/> for the <see cref="ProviderFactory"/>.
/// </summary>
readonly GeneralConfiguration generalConfiguration;
readonly IOptionsMonitor<GeneralConfiguration> generalConfigurationOptions;

/// <summary>
/// The <see cref="FileLoggingConfiguration"/> for the <see cref="ProviderFactory"/>.
/// The <see cref="FileLoggingConfiguration"/> <see cref="IOptionsMonitor{TOptions}"/> for the <see cref="ProviderFactory"/>.
/// </summary>
readonly FileLoggingConfiguration loggingConfiguration;
readonly IOptionsMonitor<FileLoggingConfiguration> loggingConfigurationOptions;

/// <summary>
/// Initializes a new instance of the <see cref="ProviderFactory"/> class.
Expand All @@ -52,22 +52,22 @@ sealed class ProviderFactory : IProviderFactory
/// <param name="assemblyInformationProvider">The value of <see cref="assemblyInformationProvider"/>.</param>
/// <param name="asyncDelayer">The value of <see cref="asyncDelayer"/>.</param>
/// <param name="loggerFactory">The value of <see cref="loggerFactory"/>.</param>
/// <param name="generalConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="generalConfiguration"/>.</param>
/// <param name="loggingConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="loggingConfiguration"/>.</param>
/// <param name="generalConfigurationOptions">The value of <see cref="generalConfigurationOptions"/>.</param>
/// <param name="loggingConfigurationOptions">The value of <see cref="loggingConfigurationOptions"/>.</param>
public ProviderFactory(
IJobManager jobManager,
IAssemblyInformationProvider assemblyInformationProvider,
IAsyncDelayer asyncDelayer,
ILoggerFactory loggerFactory,
IOptions<GeneralConfiguration> generalConfigurationOptions,
IOptions<FileLoggingConfiguration> loggingConfigurationOptions)
IOptionsMonitor<GeneralConfiguration> generalConfigurationOptions,
IOptionsMonitor<FileLoggingConfiguration> loggingConfigurationOptions)
{
this.jobManager = jobManager ?? throw new ArgumentNullException(nameof(jobManager));
this.loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
this.asyncDelayer = asyncDelayer ?? throw new ArgumentNullException(nameof(asyncDelayer));
this.assemblyInformationProvider = assemblyInformationProvider ?? throw new ArgumentNullException(nameof(assemblyInformationProvider));
generalConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
loggingConfiguration = loggingConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(loggingConfigurationOptions));
this.generalConfigurationOptions = generalConfigurationOptions ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
this.loggingConfigurationOptions = loggingConfigurationOptions ?? throw new ArgumentNullException(nameof(loggingConfigurationOptions));
}

/// <inheritdoc />
Expand All @@ -80,16 +80,16 @@ public IProvider CreateProvider(Models.ChatBot settings)
jobManager,
asyncDelayer,
loggerFactory.CreateLogger<IrcProvider>(),
assemblyInformationProvider,
settings,
loggingConfiguration),
assemblyInformationProvider,
loggingConfigurationOptions),
ChatProvider.Discord => new DiscordProvider(
jobManager,
asyncDelayer,
loggerFactory.CreateLogger<DiscordProvider>(),
assemblyInformationProvider,
settings,
generalConfiguration),
generalConfigurationOptions,
settings),
_ => throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Invalid ChatProvider: {0}", settings.Provider)),
};
}
Expand Down
26 changes: 14 additions & 12 deletions src/Tgstation.Server.Host/Components/Engine/OpenDreamInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ class OpenDreamInstaller : EngineInstallerBase
/// <summary>
/// The <see cref="GeneralConfiguration"/> for the <see cref="OpenDreamInstaller"/>.
/// </summary>
protected GeneralConfiguration GeneralConfiguration { get; }
protected IOptionsMonitor<GeneralConfiguration> GeneralConfiguration { get; }

/// <summary>
/// The <see cref="Configuration.SessionConfiguration"/> for the <see cref="OpenDreamInstaller"/>.
/// </summary>
protected SessionConfiguration SessionConfiguration { get; }
protected IOptionsMonitor<SessionConfiguration> SessionConfiguration { get; }

/// <summary>
/// The <see cref="IPlatformIdentifier"/> for the <see cref="OpenDreamInstaller"/>.
Expand Down Expand Up @@ -101,17 +101,17 @@ public OpenDreamInstaller(
IRepositoryManager repositoryManager,
IAsyncDelayer asyncDelayer,
IAbstractHttpClientFactory httpClientFactory,
IOptions<GeneralConfiguration> generalConfigurationOptions,
IOptions<SessionConfiguration> sessionConfigurationOptions)
IOptionsMonitor<GeneralConfiguration> generalConfigurationOptions,
IOptionsMonitor<SessionConfiguration> sessionConfigurationOptions)
: base(ioManager, logger)
{
this.platformIdentifier = platformIdentifier ?? throw new ArgumentNullException(nameof(platformIdentifier));
ProcessExecutor = processExecutor ?? throw new ArgumentNullException(nameof(processExecutor));
this.repositoryManager = repositoryManager ?? throw new ArgumentNullException(nameof(repositoryManager));
this.asyncDelayer = asyncDelayer ?? throw new ArgumentNullException(nameof(asyncDelayer));
this.httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
GeneralConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
SessionConfiguration = sessionConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(sessionConfigurationOptions));
GeneralConfiguration = generalConfigurationOptions ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
SessionConfiguration = sessionConfigurationOptions ?? throw new ArgumentNullException(nameof(sessionConfigurationOptions));
}

/// <inheritdoc />
Expand Down Expand Up @@ -143,10 +143,11 @@ public override async ValueTask<IEngineInstallationData> DownloadVersion(EngineV

var progressSection1 = jobProgressReporter.CreateSection("Updating OpenDream git repository", 0.5f);
IRepository? repo;
var generalConfig = GeneralConfiguration.CurrentValue;
try
{
repo = await repositoryManager.CloneRepository(
GeneralConfiguration.OpenDreamGitUrl,
generalConfig.OpenDreamGitUrl,
null,
null,
null,
Expand Down Expand Up @@ -183,7 +184,7 @@ public override async ValueTask<IEngineInstallationData> DownloadVersion(EngineV
using (var progressSection2 = jobProgressReporter.CreateSection("Checking out OpenDream version", 0.5f))
{
var committish = version.SourceSHA
?? $"{GeneralConfiguration.OpenDreamGitTagPrefix}{version.Version!.Semver()}";
?? $"{generalConfig.OpenDreamGitTagPrefix}{version.Version!.Semver()}";

await repo.CheckoutObject(
committish,
Expand Down Expand Up @@ -259,24 +260,25 @@ await HandleExtremelyLongPathOperation(
async shortenedPath =>
{
var shortenedDeployPath = IOManager.ConcatPath(shortenedPath, DeployDir);
var generalConfig = GeneralConfiguration.CurrentValue;
await using var buildProcess = await ProcessExecutor.LaunchProcess(
dotnetPath,
shortenedPath,
$"run -c Release --project OpenDreamPackageTool -- --tgs -o {shortenedDeployPath}",
cancellationToken,
null,
null,
!GeneralConfiguration.OpenDreamSuppressInstallOutput,
!GeneralConfiguration.OpenDreamSuppressInstallOutput);
!generalConfig.OpenDreamSuppressInstallOutput,
!generalConfig.OpenDreamSuppressInstallOutput);

if (deploymentPipelineProcesses && SessionConfiguration.LowPriorityDeploymentProcesses)
if (deploymentPipelineProcesses && SessionConfiguration.CurrentValue.LowPriorityDeploymentProcesses)
buildProcess.AdjustPriority(false);

using (cancellationToken.Register(() => buildProcess.Terminate()))
buildExitCode = await buildProcess.Lifetime;

string? output;
if (!GeneralConfiguration.OpenDreamSuppressInstallOutput)
if (!GeneralConfiguration.CurrentValue.OpenDreamSuppressInstallOutput)
{
var buildOutputTask = buildProcess.GetCombinedOutput(cancellationToken);
if (!buildOutputTask.IsCompleted)
Expand Down
Loading