Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,11 @@ class SetupNotificationSettings(INotificationsDataStore notificationsDataStore)
{
public async Task StartAsync(CancellationToken cancellationToken = default)
{
await using var notificationsManager = await notificationsDataStore.CreateNotificationsManager(cancellationToken);

var settings = await notificationsManager.LoadSettings(cancellationToken);
var settings = await notificationsDataStore.LoadSettings(cancellationToken);
settings.Email.Enabled = true;
settings.Email.From = "YouServiceControl@particular.net";
settings.Email.To = "WhoeverMightBeConcerned@particular.net";
await notificationsManager.SaveChanges(cancellationToken);
await notificationsDataStore.SaveSettings(settings, cancellationToken);
}

public Task StopAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
namespace ServiceControl.Persistence.EFCore.Implementation;

using DbContexts;
using Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Notifications;

public class NotificationsDataStore(IServiceProvider serviceProvider) : INotificationsDataStore
public class NotificationsDataStore(IServiceScopeFactory scopeFactory) : DataStoreBase(scopeFactory), INotificationsDataStore
{
public Task<INotificationsManager> CreateNotificationsManager(CancellationToken cancellationToken = default)
{
var scope = serviceProvider.CreateAsyncScope();
ServiceControlDbContext serviceControlDbContext = scope.ServiceProvider.GetRequiredService<ServiceControlDbContext>();
return Task.FromResult<INotificationsManager>(new NotificationsManager(scope, serviceControlDbContext));
}
}
public Task<NotificationsSettings> LoadSettings(CancellationToken cancellationToken = default) =>
ExecuteWithDbContext(async (dbContext, ct) =>
{
var email = await dbContext.GetSetting<EmailNotifications>(SettingKeys.NotificationEmails, ct);
return new NotificationsSettings { Email = email ?? new EmailNotifications() };
}, cancellationToken);

public Task SaveSettings(NotificationsSettings settings, CancellationToken cancellationToken = default) =>
ExecuteWithDbContext((dbContext, ct) => dbContext.StoreSetting(SettingKeys.NotificationEmails, settings.Email, ct), cancellationToken);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,46 @@ namespace ServiceControl.Persistence.RavenDB.Editing
{
using System.Threading;
using System.Threading.Tasks;
using Notifications;

class NotificationsDataStore(IRavenSessionProvider sessionProvider) : INotificationsDataStore
{
public async Task<INotificationsManager> CreateNotificationsManager(CancellationToken cancellationToken = default) =>
// the notifications manager manages the lifetime of the session
new NotificationsManager(await sessionProvider.OpenSession(cancellationToken: cancellationToken));
const string SingleDocumentId = "NotificationsSettings/All";

public async Task<NotificationsSettings> LoadSettings(CancellationToken cancellationToken = default)
{
using var session = await sessionProvider.OpenSession(cancellationToken: cancellationToken);
var document = await session.LoadAsync<NotificationsSettingsDocument>(SingleDocumentId, cancellationToken);

return new NotificationsSettings
{
Email = Copy(document?.Email ?? new EmailNotifications())
};
}

public async Task SaveSettings(NotificationsSettings settings, CancellationToken cancellationToken = default)
{
using var session = await sessionProvider.OpenSession(cancellationToken: cancellationToken);
var document = new NotificationsSettingsDocument
{
Id = SingleDocumentId,
Email = Copy(settings.Email)
};

await session.StoreAsync(document, SingleDocumentId, cancellationToken);
await session.SaveChangesAsync(cancellationToken);
}

static EmailNotifications Copy(EmailNotifications source) => new()
{
Enabled = source.Enabled,
SmtpServer = source.SmtpServer,
SmtpPort = source.SmtpPort,
AuthenticationAccount = source.AuthenticationAccount,
AuthenticationPassword = source.AuthenticationPassword,
EnableTLS = source.EnableTLS,
To = source.To,
From = source.From
};
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ namespace ServiceControl.Persistence.Tests;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NUnit.Framework;
using Raven.Client.Documents;
using Raven.Client.Documents.Session;
using ServiceControl.Contracts.Operations;
using ServiceControl.MessageFailures;
using ServiceControl.Persistence;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ namespace ServiceControl.Persistence.Tests;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using EFCore.DbContexts;
using EFCore.Entities;
using EFCore.Infrastructure;
using MessageFailures;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Time.Testing;
using NServiceBus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
namespace ServiceControl.Persistence.Tests;

using System.Threading.Tasks;
Expand All @@ -18,4 +19,4 @@ public interface IPersistenceTestsContext

string GenerateFailedMessageRecordId(string messageId);
Task InsertFailedMessages(params FailedMessage[] messages);
}
}
Loading
Loading