diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application/Exceptions/ShareScanInProgressException.cs | 72 | ||||
| -rw-r--r-- | src/slskd/Application/Management/API/Controllers/ApplicationController.cs | 18 | ||||
| -rw-r--r-- | src/slskd/Application/Management/API/Controllers/ConfigurationController.cs | 76 | ||||
| -rw-r--r-- | src/slskd/Application/Management/API/DTO/ServerState.cs (renamed from src/slskd/Application/Management/Domain/ServerState.cs) | 0 | ||||
| -rw-r--r-- | src/slskd/Application/Management/IManagementService.cs | 12 | ||||
| -rw-r--r-- | src/slskd/Application/Management/ManagementService.cs | 38 | ||||
| -rw-r--r-- | src/slskd/Application/Shares/SharedFileCache.cs | 18 |
7 files changed, 210 insertions, 24 deletions
diff --git a/src/slskd/Application/Exceptions/ShareScanInProgressException.cs b/src/slskd/Application/Exceptions/ShareScanInProgressException.cs new file mode 100644 index 00000000..2e548930 --- /dev/null +++ b/src/slskd/Application/Exceptions/ShareScanInProgressException.cs @@ -0,0 +1,72 @@ +// <copyright file="ShareScanInProgressException.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd +{ + using System; + using System.Diagnostics.CodeAnalysis; + using System.Runtime.Serialization; + + /// <summary> + /// Represents an error that occurs when multiple share scans are attempted concurrently. + /// </summary> + [ExcludeFromCodeCoverage] + [Serializable] + public class ShareScanInProgressException : SlskdException + { + /// <summary> + /// Initializes a new instance of the <see cref="ShareScanInProgressException"/> class. + /// </summary> + public ShareScanInProgressException() + : base() + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="ShareScanInProgressException"/> class with a specified error message. + /// </summary> + /// <param name="message">The message that describes the error.</param> + public ShareScanInProgressException(string message) + : base(message) + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="ShareScanInProgressException"/> class with a specified error message and a + /// reference to the inner exception that is the cause of this exception. + /// </summary> + /// <param name="message">The message that describes the error.</param> + /// <param name="innerException"> + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no + /// inner exception is specified. + /// </param> + public ShareScanInProgressException(string message, Exception innerException) + : base(message, innerException) + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="ShareScanInProgressException"/> class with serialized data. + /// </summary> + /// <param name="info">The SerializationInfo that holds the serialized object data about the exception being thrown.</param> + /// <param name="context">The StreamingContext that contains contextual information about the source or destination.</param> + protected ShareScanInProgressException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +}
\ No newline at end of file diff --git a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs index ddac832a..43029aa5 100644 --- a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs +++ b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs @@ -22,7 +22,6 @@ namespace slskd.Management.API using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; - using Soulseek; /// <summary> /// Application. @@ -34,16 +33,27 @@ namespace slskd.Management.API [Consumes("application/json")] public class ApplicationController : ControllerBase { - public ApplicationController(ISoulseekClient client, IHostApplicationLifetime lifetime) + public ApplicationController(IManagementService managementService, IHostApplicationLifetime lifetime) { - Client = client; + Management = managementService; Lifetime = lifetime; } - private ISoulseekClient Client { get; } + private IManagementService Management { get; } private IHostApplicationLifetime Lifetime { get; } /// <summary> + /// Gets the current state of the application. + /// </summary> + /// <returns></returns> + [HttpGet] + [Authorize] + public IActionResult State() + { + return Ok(Management.GetServiceState()); + } + + /// <summary> /// Stops the application. /// </summary> /// <returns></returns> diff --git a/src/slskd/Application/Management/API/Controllers/ConfigurationController.cs b/src/slskd/Application/Management/API/Controllers/ConfigurationController.cs new file mode 100644 index 00000000..26ce6c52 --- /dev/null +++ b/src/slskd/Application/Management/API/Controllers/ConfigurationController.cs @@ -0,0 +1,76 @@ +// <copyright file="ConfigurationController.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +using Microsoft.Extensions.Options; + +namespace slskd.Application.Management.API +{ + using System.Threading.Tasks; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Mvc; + using slskd.Management; + using slskd.Shares; + + /// <summary> + /// Configuration. + /// </summary> + [Route("api/v{version:apiVersion}/[controller]")] + [ApiVersion("0")] + [ApiController] + [Produces("application/json")] + [Consumes("application/json")] + public class ConfigurationController : ControllerBase + { + public ConfigurationController( + IOptionsSnapshot<Options> optionsShapshot, + IStateMonitor<SharedFileCacheState> sharedFileCacheMonitor, + IManagementService managementService) + { + OptionsShapshot = optionsShapshot; + SharedFileCacheMonitor = sharedFileCacheMonitor; + ManagementService = managementService; + } + + private IManagementService ManagementService { get; } + private IOptionsSnapshot<Options> OptionsShapshot { get; } + private IStateMonitor<SharedFileCacheState> SharedFileCacheMonitor { get; } + + [HttpGet] + [Authorize] + public IActionResult GetOptions() + { + // todo: sanitize this to remove passwords + return Ok(OptionsShapshot.Value); + } + + [HttpPut] + [Authorize] + public async Task<IActionResult> RescanSharesAsync() + { + try + { + await ManagementService.RescanSharesAsync(); + } + catch (ShareScanInProgressException ex) + { + return Conflict(ex.Message); + } + + return Ok(); + } + } +} diff --git a/src/slskd/Application/Management/Domain/ServerState.cs b/src/slskd/Application/Management/API/DTO/ServerState.cs index 80c1e856..80c1e856 100644 --- a/src/slskd/Application/Management/Domain/ServerState.cs +++ b/src/slskd/Application/Management/API/DTO/ServerState.cs diff --git a/src/slskd/Application/Management/IManagementService.cs b/src/slskd/Application/Management/IManagementService.cs index b3e13477..6c5f3784 100644 --- a/src/slskd/Application/Management/IManagementService.cs +++ b/src/slskd/Application/Management/IManagementService.cs @@ -43,5 +43,17 @@ namespace slskd.Management /// </summary> /// <returns>The current server state.</returns> ServerState GetServerState(); + + /// <summary> + /// Gets the current state of the slskd service. + /// </summary> + /// <returns></returns> + ServiceState GetServiceState(); + + /// <summary> + /// Re-scans shared directories. + /// </summary> + /// <returns>The operation context.</returns> + Task RescanSharesAsync(); } }
\ No newline at end of file diff --git a/src/slskd/Application/Management/ManagementService.cs b/src/slskd/Application/Management/ManagementService.cs index 66ed2b97..2f79f496 100644 --- a/src/slskd/Application/Management/ManagementService.cs +++ b/src/slskd/Application/Management/ManagementService.cs @@ -1,18 +1,13 @@ // <copyright file="ManagementService.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. +// This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public +// License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. // -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. +// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty +// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. // -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. +// You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/. // </copyright> using Microsoft.Extensions.Options; @@ -21,6 +16,7 @@ namespace slskd.Management { using System; using System.Threading.Tasks; + using slskd.Shares; using Soulseek; /// <summary> @@ -32,18 +28,26 @@ namespace slskd.Management /// Initializes a new instance of the <see cref="ManagementService"/> class. /// </summary> /// <param name="optionsMonitor">The options monitor used to derive application options.</param> + /// <param name="serviceStateMonitor">The state monitor for application service state.</param> /// <param name="soulseekClient">The Soulseek client.</param> + /// <param name="sharedFileCache">The shared file cache.</param> public ManagementService( IOptionsMonitor<Options> optionsMonitor, - ISoulseekClient soulseekClient) + IStateMonitor<ServiceState> serviceStateMonitor, + ISoulseekClient soulseekClient, + ISharedFileCache sharedFileCache) { OptionsMonitor = optionsMonitor; + ServiceStateMonitor = serviceStateMonitor; Client = soulseekClient; + SharedFileCache = sharedFileCache; } private ISoulseekClient Client { get; } private Options Options => OptionsMonitor.CurrentValue; private IOptionsMonitor<Options> OptionsMonitor { get; } + private IStateMonitor<ServiceState> ServiceStateMonitor { get; } + private ISharedFileCache SharedFileCache { get; } /// <summary> /// Connects the Soulseek client to the server using the configured username and password. @@ -72,5 +76,17 @@ namespace slskd.Management State = Client.State, Username = Client.Username, }; + + /// <summary> + /// Gets the current state of the slskd service. + /// </summary> + /// <returns></returns> + public ServiceState GetServiceState() => ServiceStateMonitor.CurrentValue; + + /// <summary> + /// Re-scans shared directories. + /// </summary> + /// <returns>The operation context.</returns> + public Task RescanSharesAsync() => SharedFileCache.FillAsync(); } }
\ No newline at end of file diff --git a/src/slskd/Application/Shares/SharedFileCache.cs b/src/slskd/Application/Shares/SharedFileCache.cs index a06e86f6..439e5eb8 100644 --- a/src/slskd/Application/Shares/SharedFileCache.cs +++ b/src/slskd/Application/Shares/SharedFileCache.cs @@ -109,21 +109,21 @@ namespace slskd.Shares /// <returns>The operation context.</returns> public async Task FillAsync() { - if (!StateMonitor.CurrentValue.Ready) + if (SyncRoot.TryEnterWriteLock(0)) { - Log.Warning("Cache fill rejected; cache is already filling."); - return; + Log.Warning("Shared file scan rejected; scan is already in progress."); + throw new ShareScanInProgressException("Shared files are already being scanned."); } await Task.Yield(); - var sw = new Stopwatch(); - sw.Start(); - SyncRoot.EnterWriteLock(); try { + var sw = new Stopwatch(); + sw.Start(); + StateMonitor.SetValue(state => state with { Ready = false, FillProgress = 0 }); CreateTable(); @@ -150,7 +150,7 @@ namespace slskd.Shares { if (files.ContainsKey(file.Key)) { - Console.WriteLine($"[WARNING] File {file.Key} shared in directory {directory} has already been cached. This is probably a misconfiguration of the shared directories."); + Log.Warning($"File {file.Key} shared in directory {directory} has already been cached. This is probably a misconfiguration of the shared directories (is a subdirectory being re-shared?)."); } files[file.Key] = file.Value; @@ -170,13 +170,13 @@ namespace slskd.Shares Files = files; StateMonitor.SetValue(state => state with { Ready = true, FillProgress = 1, Directories = directories.Count, Files = files.Count }); + + Log.Information($"Shared file cache recreated in {sw.ElapsedMilliseconds}ms. Directories: {directories.Count}, Files: {files.Count}"); } finally { SyncRoot.ExitWriteLock(); } - - sw.Stop(); } /// <summary> |
