From cb268e8144796b2f94dd444525e0685c1ac00109 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 11 Nov 2022 09:42:28 -0600 Subject: allow the app to start if the controller can't be connected --- src/slskd/Application.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index f732594d..5d614d28 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -401,7 +401,7 @@ namespace slskd else if (OptionsAtStartup.Network.Mode.ToEnum() == OperationMode.Agent) { Log.Information("Running in Agent mode; not connecting to the Soulseek server."); - await Network.Client.StartAsync(cancellationToken); + _ = Network.Client.StartAsync(cancellationToken); } else { -- cgit v1.2.3 From 526d389da45dd86f01922615ce9145d7e9e0b152 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 11 Nov 2022 17:39:15 -0600 Subject: add endpoints for connect and disconnect --- src/slskd/Network/API/Controllers/NetworkController.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/slskd') diff --git a/src/slskd/Network/API/Controllers/NetworkController.cs b/src/slskd/Network/API/Controllers/NetworkController.cs index eb0c01e3..b0e15663 100644 --- a/src/slskd/Network/API/Controllers/NetworkController.cs +++ b/src/slskd/Network/API/Controllers/NetworkController.cs @@ -47,6 +47,22 @@ namespace slskd.Network private ILogger Log { get; } = Serilog.Log.ForContext(); private INetworkService Network { get; } + [HttpPut("")] + [Authorize(Policy = AuthPolicy.Any)] + public async Task Connect() + { + await Network.Client.StartAsync(); + return Ok(); + } + + [HttpDelete("")] + [Authorize(Policy = AuthPolicy.Any)] + public async Task Disconnect() + { + await Network.Client.StopAsync(); + return NoContent(); + } + /// /// Uploads a file. /// -- cgit v1.2.3 From 74a1d79dd559891aae4d8a5a9d472dc9bfc49269 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 15 Nov 2022 10:16:06 -0600 Subject: fix Network references --- src/slskd/Relay/API/Controllers/RelayController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd') diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs index 3440c8ea..952507ad 100644 --- a/src/slskd/Relay/API/Controllers/RelayController.cs +++ b/src/slskd/Relay/API/Controllers/RelayController.cs @@ -51,7 +51,7 @@ namespace slskd.Relay [Authorize(Policy = AuthPolicy.Any)] public async Task Connect() { - await Network.Client.StartAsync(); + await Relay.Client.StartAsync(); return Ok(); } @@ -59,7 +59,7 @@ namespace slskd.Relay [Authorize(Policy = AuthPolicy.Any)] public async Task Disconnect() { - await Network.Client.StopAsync(); + await Relay.Client.StopAsync(); return NoContent(); } -- cgit v1.2.3 From c0e5c4b3646a2294c50adddaa11ba8379f0d7a1b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 15 Nov 2022 10:52:59 -0600 Subject: update state after disconnect --- src/slskd/Relay/RelayClient.cs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/slskd') diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs index 854399dd..193f8cbb 100644 --- a/src/slskd/Relay/RelayClient.cs +++ b/src/slskd/Relay/RelayClient.cs @@ -181,6 +181,8 @@ namespace slskd.Relay LoggedIn = false; + State.SetValue(_ => TranslateState(HubConnectionState.Disconnected)); + Log.Information("Relay controller connection disconnected"); } } -- cgit v1.2.3 From b2b8042a5e722725ae077bf3312778977342bcdb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 16 Nov 2022 08:37:06 -0600 Subject: synchronize start --- src/slskd/Relay/RelayClient.cs | 92 ++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 39 deletions(-) (limited to 'src/slskd') diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs index 193f8cbb..df4a3e89 100644 --- a/src/slskd/Relay/RelayClient.cs +++ b/src/slskd/Relay/RelayClient.cs @@ -95,7 +95,8 @@ namespace slskd.Relay private ManagedState State { get; } = new(); private IShareService Shares { get; } - private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); + private SemaphoreSlim ConfigurationSyncRoot { get; } = new SemaphoreSlim(1, 1); + private SemaphoreSlim StateSyncRoot { get; } = new SemaphoreSlim(1, 1); private string LastOptionsHash { get; set; } private IOptionsMonitor OptionsMonitor { get; } private IHttpClientFactory HttpClientFactory { get; } @@ -114,56 +115,69 @@ namespace slskd.Relay /// public async Task StartAsync(CancellationToken cancellationToken = default) { - var mode = OptionsMonitor.CurrentValue.Relay.Mode.ToEnum(); - - if (mode != OperationMode.Agent && mode != OperationMode.Debug) + if (!StateSyncRoot.Wait(0, cancellationToken)) { - throw new InvalidOperationException($"Relay client can only be started when operation mode is {OperationMode.Agent}"); + // we're already attempting to connect, let the existing attempt handle it + return; } - StartCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); - LoggedInTaskCompletionSource = new TaskCompletionSource(); - StartRequested = true; + try + { + var mode = OptionsMonitor.CurrentValue.Relay.Mode.ToEnum(); - Log.Information("Attempting to connect to the relay controller {Address}", OptionsMonitor.CurrentValue.Relay.Controller.Address); + if (mode != OperationMode.Agent && mode != OperationMode.Debug) + { + throw new InvalidOperationException($"Relay client can only be started when operation mode is {OperationMode.Agent}"); + } - State.SetValue(_ => TranslateState(HubConnectionState.Connecting)); + StartCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + LoggedInTaskCompletionSource = new TaskCompletionSource(); + StartRequested = true; - // retry indefinitely - await Retry.Do( - task: () => HubConnection.StartAsync(StartCancellationTokenSource.Token), - isRetryable: (attempts, ex) => true, - onFailure: (attempts, ex) => - { - Log.Debug(ex, "Relay hub connection failure"); - Log.Warning("Failed attempt #{Attempts} to connect to relay controller: {Message}", attempts, ex.Message); - }, - maxAttempts: int.MaxValue, - maxDelayInMilliseconds: 30000, - StartCancellationTokenSource.Token); + Log.Information("Attempting to connect to the relay controller {Address}", OptionsMonitor.CurrentValue.Relay.Controller.Address); - State.SetValue(_ => TranslateState(HubConnection.State)); - Log.Information("Relay controller connection established. Awaiting authentication..."); + State.SetValue(_ => TranslateState(HubConnectionState.Connecting)); - await LoggedInTaskCompletionSource.Task; + // retry indefinitely + await Retry.Do( + task: () => HubConnection.StartAsync(StartCancellationTokenSource.Token), + isRetryable: (attempts, ex) => true, + onFailure: (attempts, ex) => + { + Log.Debug(ex, "Relay hub connection failure"); + Log.Warning("Failed attempt #{Attempts} to connect to relay controller: {Message}", attempts, ex.Message); + }, + maxAttempts: int.MaxValue, + maxDelayInMilliseconds: 30000, + StartCancellationTokenSource.Token); - LoggedIn = true; + State.SetValue(_ => TranslateState(HubConnection.State)); + Log.Information("Relay controller connection established. Awaiting authentication..."); - Log.Information("Authenticated. Uploading shares..."); + await LoggedInTaskCompletionSource.Task; - try - { - await UploadSharesAsync(); + LoggedIn = true; + + Log.Information("Authenticated. Uploading shares..."); + + try + { + await UploadSharesAsync(); + } + catch (Exception ex) + { + Log.Error(ex, ex.Message); + Log.Error("Disconnecting from the relay controller"); + await StopAsync(); + throw; + } + + Log.Information("Shares uploaded. Ready to upload files."); } - catch (Exception ex) + finally { - Log.Error(ex, ex.Message); - Log.Error("Disconnecting from the relay controller"); - await StopAsync(); - throw; + StateSyncRoot.Release(); } - - Log.Information("Shares uploaded. Ready to upload files."); } /// @@ -425,7 +439,7 @@ namespace slskd.Relay return; } - SyncRoot.Wait(); + ConfigurationSyncRoot.Wait(); try { @@ -482,7 +496,7 @@ namespace slskd.Relay } finally { - SyncRoot.Release(); + ConfigurationSyncRoot.Release(); } } -- cgit v1.2.3