-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkspaceTeardownService.cs
More file actions
65 lines (58 loc) · 3.09 KB
/
Copy pathWorkspaceTeardownService.cs
File metadata and controls
65 lines (58 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using RustPlusBot.Features.Workspace.Gateway;
using RustPlusBot.Features.Workspace.Reconciler;
using RustPlusBot.Persistence.Workspace;
namespace RustPlusBot.Features.Workspace.Teardown;
/// <summary>Deletes provisioned resources under the per-guild lock, then clears the records.</summary>
/// <param name="gateway">Discord operations.</param>
/// <param name="store">Provisioning persistence.</param>
/// <param name="provisioningLock">Per-guild serialization (shared with the reconciler).</param>
internal sealed class WorkspaceTeardownService(
IWorkspaceGateway gateway,
IWorkspaceStore store,
IProvisioningLock provisioningLock) : IWorkspaceTeardownService, IServerWorkspaceRemover
{
/// <inheritdoc />
public async Task RemoveServerAsync(ulong guildId, Guid serverId, CancellationToken cancellationToken = default)
{
using var handle = await provisioningLock.AcquireAsync(guildId, cancellationToken).ConfigureAwait(false);
await DeleteScopeAsync(guildId, serverId, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc />
public async Task ResetGuildAsync(ulong guildId, CancellationToken cancellationToken = default)
{
using var handle = await provisioningLock.AcquireAsync(guildId, cancellationToken).ConfigureAwait(false);
await ResetGuildCoreAsync(guildId, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Deletes the guild's provisioned resources and records WITHOUT taking the provisioning lock.
/// The caller MUST already hold the guild's <see cref="IProvisioningLock"/> — <see cref="GuildPurgeService"/>
/// uses this so it can hold the lock across the wider purge. External callers use <see cref="ResetGuildAsync"/>.
/// </summary>
/// <param name="guildId">The guild to reset.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task.</returns>
internal async Task ResetGuildCoreAsync(ulong guildId, CancellationToken cancellationToken = default)
{
var categories = await store.GetAllCategoriesAsync(guildId, cancellationToken).ConfigureAwait(false);
foreach (var category in categories)
{
await DeleteScopeAsync(guildId, category.RustServerId, cancellationToken).ConfigureAwait(false);
}
}
private async Task DeleteScopeAsync(ulong guildId, Guid? serverId, CancellationToken cancellationToken)
{
foreach (var channel in await store.GetChannelsAsync(guildId, serverId, cancellationToken)
.ConfigureAwait(false))
{
await gateway.DeleteChannelAsync(guildId, channel.DiscordChannelId, cancellationToken)
.ConfigureAwait(false);
}
var category = await store.GetCategoryAsync(guildId, serverId, cancellationToken).ConfigureAwait(false);
if (category is not null)
{
await gateway.DeleteCategoryAsync(guildId, category.DiscordCategoryId, cancellationToken)
.ConfigureAwait(false);
}
await store.DeleteScopeAsync(guildId, serverId, cancellationToken).ConfigureAwait(false);
}
}