From a2054cadc19d10dd49a860a1f4eded612c50b624 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 5 Aug 2022 12:41:31 -0500 Subject: add UpdateAsync() --- src/slskd/Transfers/Downloads/DownloadService.cs | 25 +++++++----- src/slskd/Transfers/Downloads/IDownloadService.cs | 7 ++++ src/slskd/Transfers/Uploads/IUploadService.cs | 15 ++++++-- src/slskd/Transfers/Uploads/UploadService.cs | 46 ++++++++++++----------- 4 files changed, 58 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 27d7dfb5..80104593 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -122,13 +122,6 @@ namespace slskd.Transfers.Downloads var waitUntilEnqueue = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - async Task UpdateAndSaveChangesAsync(Transfer transfer) - { - using var context = await ContextFactory.CreateDbContextAsync(); - context.Update(transfer); - await context.SaveChangesAsync(); - } - // this task does the actual work of downloading the file. if the transfer is successfully queued // remotely, the waitUntilEnqueue completion source is set, which yields execution to below so we can // return and let the caller know. @@ -154,7 +147,7 @@ namespace slskd.Transfers.Downloads transfer = transfer.WithSoulseekTransfer(args.Transfer); // todo: broadcast - _ = UpdateAndSaveChangesAsync(transfer); + _ = UpdateAsync(transfer); if (args.Transfer.State.HasFlag(TransferStates.Queued) || args.Transfer.State == TransferStates.Initializing) { @@ -165,12 +158,12 @@ namespace slskd.Transfers.Downloads { transfer = transfer.WithSoulseekTransfer(args.Transfer); // todo: broadcast - _ = UpdateAndSaveChangesAsync(transfer); + _ = UpdateAsync(transfer); }))); transfer = transfer.WithSoulseekTransfer(completedTransfer); // todo: broadcast - await UpdateAndSaveChangesAsync(transfer); + await UpdateAsync(transfer); // this would be the ideal place to hook in a generic post-download task processor for now, we'll // just carry out hard coded behavior @@ -372,6 +365,18 @@ namespace slskd.Transfers.Downloads return false; } + /// + /// Updates the specified . + /// + /// The transfer to update. + /// The operation context. + public async Task UpdateAsync(Transfer transfer) + { + using var context = await ContextFactory.CreateDbContextAsync(); + context.Update(transfer); + await context.SaveChangesAsync(); + } + private static FileStream GetLocalFileStream(string remoteFilename, string saveDirectory) { var localFilename = remoteFilename.ToLocalFilename(baseDirectory: saveDirectory); diff --git a/src/slskd/Transfers/Downloads/IDownloadService.cs b/src/slskd/Transfers/Downloads/IDownloadService.cs index f290acb5..df17391d 100644 --- a/src/slskd/Transfers/Downloads/IDownloadService.cs +++ b/src/slskd/Transfers/Downloads/IDownloadService.cs @@ -78,5 +78,12 @@ namespace slskd.Transfers.Downloads /// The unique identifier for the download. /// A value indicating whether the download was successfully cancelled. bool TryCancel(Guid id); + + /// + /// Updates the specified . + /// + /// The transfer to update. + /// The operation context. + Task UpdateAsync(Transfer transfer); } } \ No newline at end of file diff --git a/src/slskd/Transfers/Uploads/IUploadService.cs b/src/slskd/Transfers/Uploads/IUploadService.cs index cfa980e8..0d829227 100644 --- a/src/slskd/Transfers/Uploads/IUploadService.cs +++ b/src/slskd/Transfers/Uploads/IUploadService.cs @@ -45,6 +45,13 @@ namespace slskd.Transfers.Uploads /// The operation context. Task EnqueueAsync(string username, string filename); + /// + /// Returns a value indicating whether an upload matching the specified exists. + /// + /// The expression used to match uploads. + /// A value indicating whether an upload matching the specified expression exists. + Task ExistsAsync(Expression> expression); + /// /// Finds a single upload matching the specified . /// @@ -76,10 +83,10 @@ namespace slskd.Transfers.Uploads bool TryCancel(Guid id); /// - /// Returns a value indicating whether an upload matching the specified exists. + /// Updates the specified . /// - /// The expression used to match uploads. - /// A value indicating whether an upload matching the specified expression exists. - Task ExistsAsync(Expression> expression); + /// The transfer to update. + /// The operation context. + Task UpdateAsync(Transfer transfer); } } \ No newline at end of file diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 319b4306..8b530bc0 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -65,11 +65,11 @@ namespace slskd.Transfers.Uploads public IUploadQueue Queue { get; init; } private ConcurrentDictionary CancellationTokens { get; } = new ConcurrentDictionary(); + private ISoulseekClient Client { get; set; } private IDbContextFactory ContextFactory { get; } private ILogger Log { get; } = Serilog.Log.ForContext(); private IShareService Shares { get; set; } private IUserService Users { get; set; } - private ISoulseekClient Client { get; set; } /// /// Enqueues the requested file. @@ -79,8 +79,8 @@ namespace slskd.Transfers.Uploads /// The operation context. public async Task EnqueueAsync(string username, string filename) { - // remote clients might sometimes re-request downloads to check the status. don't try to add the download again - // if it is already tracked. + // remote clients might sometimes re-request downloads to check the status. don't try to add the download again if it + // is already tracked. if (await ExistsAsync(t => t.Username == username && t.Filename == filename)) { return; @@ -131,13 +131,6 @@ namespace slskd.Transfers.Uploads var cts = new CancellationTokenSource(); CancellationTokens.TryAdd(id, cts); - async Task UpdateAndSaveChangesAsync(Transfer transfer) - { - using var context = await ContextFactory.CreateDbContextAsync(); - context.Update(transfer); - await context.SaveChangesAsync(); - } - // accept all download requests, and begin the upload immediately. normally there would be an internal queue, and // uploads would be handled separately. _ = Task.Run(async () => @@ -151,7 +144,7 @@ namespace slskd.Transfers.Uploads transfer = transfer.WithSoulseekTransfer(args.Transfer); // todo: broadcast - _ = UpdateAndSaveChangesAsync(transfer); + _ = UpdateAsync(transfer); if (args.Transfer.State.HasFlag(TransferStates.Queued)) { @@ -162,16 +155,15 @@ namespace slskd.Transfers.Uploads { transfer = transfer.WithSoulseekTransfer(args.Transfer); // todo: broadcast - _ = UpdateAndSaveChangesAsync(transfer); + _ = UpdateAsync(transfer); }), governor: (tx, req, ct) => Governor.GetBytesAsync(tx.Username, req, ct), reporter: (tx, att, grant, act) => Governor.ReturnBytes(tx.Username, att, grant, act), slotAwaiter: (tx, ct) => Queue.AwaitStartAsync(tx.Username, tx.Filename), slotReleased: (tx) => Queue.Complete(tx.Username, tx.Filename)); - // users with uploads must be watched so that we can keep informed of their - // online status, privileges, and statistics. this is so that we can accurately - // determine their effective group. + // users with uploads must be watched so that we can keep informed of their online status, privileges, and + // statistics. this is so that we can accurately determine their effective group. if (!Users.IsWatched(username)) { await Users.WatchAsync(username); @@ -182,13 +174,23 @@ namespace slskd.Transfers.Uploads transfer = transfer.WithSoulseekTransfer(completedTransfer); //todo: broadcast - await UpdateAndSaveChangesAsync(transfer); + await UpdateAsync(transfer); }).ContinueWith(t => { Log.Information("[{Context}] {Filename} for {Username}: {Message}", "UPLOAD FAILED", username, filename, t.Exception?.Message); }, TaskContinuationOptions.NotOnRanToCompletion); // fire and forget } + /// + /// Returns a value indicating whether an upload matching the specified exists. + /// + /// The expression used to match uploads. + /// A value indicating whether an upload matching the specified expression exists. + public async Task ExistsAsync(Expression> expression) + { + return (await FindAsync(expression)) != default; + } + /// /// Finds a single upload matching the specified . /// @@ -292,13 +294,15 @@ namespace slskd.Transfers.Uploads } /// - /// Returns a value indicating whether an upload matching the specified exists. + /// Updates the specified . /// - /// The expression used to match uploads. - /// A value indicating whether an upload matching the specified expression exists. - public async Task ExistsAsync(Expression> expression) + /// The transfer to update. + /// The operation context. + public async Task UpdateAsync(Transfer transfer) { - return (await FindAsync(expression)) != default; + using var context = await ContextFactory.CreateDbContextAsync(); + context.Update(transfer); + await context.SaveChangesAsync(); } } } \ No newline at end of file -- cgit v1.2.3