aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-08-05 12:41:31 -0500
committerJP Dillingham <jp@dillingham.me>2022-08-05 12:41:31 -0500
commita2054cadc19d10dd49a860a1f4eded612c50b624 (patch)
treeaabf892c66356e58a1a540ffb3524e0e7d1bde08
parentb5283437b0c1cf36ea2625dd411419ec9d28ef00 (diff)
downloadslskd-a2054cadc19d10dd49a860a1f4eded612c50b624.tar.gz
add UpdateAsync()
-rw-r--r--src/slskd/Transfers/Downloads/DownloadService.cs25
-rw-r--r--src/slskd/Transfers/Downloads/IDownloadService.cs7
-rw-r--r--src/slskd/Transfers/Uploads/IUploadService.cs15
-rw-r--r--src/slskd/Transfers/Uploads/UploadService.cs46
4 files changed, 58 insertions, 35 deletions
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<bool>(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;
}
+ /// <summary>
+ /// Updates the specified <paramref name="transfer"/>.
+ /// </summary>
+ /// <param name="transfer">The transfer to update.</param>
+ /// <returns>The operation context.</returns>
+ 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
/// <param name="id">The unique identifier for the download.</param>
/// <returns>A value indicating whether the download was successfully cancelled.</returns>
bool TryCancel(Guid id);
+
+ /// <summary>
+ /// Updates the specified <paramref name="transfer"/>.
+ /// </summary>
+ /// <param name="transfer">The transfer to update.</param>
+ /// <returns>The operation context.</returns>
+ 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
@@ -46,6 +46,13 @@ namespace slskd.Transfers.Uploads
Task EnqueueAsync(string username, string filename);
/// <summary>
+ /// Returns a value indicating whether an upload matching the specified <paramref name="expression"/> exists.
+ /// </summary>
+ /// <param name="expression">The expression used to match uploads.</param>
+ /// <returns>A value indicating whether an upload matching the specified expression exists.</returns>
+ Task<bool> ExistsAsync(Expression<Func<Transfer, bool>> expression);
+
+ /// <summary>
/// Finds a single upload matching the specified <paramref name="expression"/>.
/// </summary>
/// <param name="expression">The expression to use to match uploads.</param>
@@ -76,10 +83,10 @@ namespace slskd.Transfers.Uploads
bool TryCancel(Guid id);
/// <summary>
- /// Returns a value indicating whether an upload matching the specified <paramref name="expression"/> exists.
+ /// Updates the specified <paramref name="transfer"/>.
/// </summary>
- /// <param name="expression">The expression used to match uploads.</param>
- /// <returns>A value indicating whether an upload matching the specified expression exists.</returns>
- Task<bool> ExistsAsync(Expression<Func<Transfer, bool>> expression);
+ /// <param name="transfer">The transfer to update.</param>
+ /// <returns>The operation context.</returns>
+ 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<Guid, CancellationTokenSource> CancellationTokens { get; } = new ConcurrentDictionary<Guid, CancellationTokenSource>();
+ private ISoulseekClient Client { get; set; }
private IDbContextFactory<TransfersDbContext> ContextFactory { get; }
private ILogger Log { get; } = Serilog.Log.ForContext<UploadService>();
private IShareService Shares { get; set; }
private IUserService Users { get; set; }
- private ISoulseekClient Client { get; set; }
/// <summary>
/// Enqueues the requested file.
@@ -79,8 +79,8 @@ namespace slskd.Transfers.Uploads
/// <returns>The operation context.</returns>
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,7 +174,7 @@ 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);
@@ -190,6 +182,16 @@ namespace slskd.Transfers.Uploads
}
/// <summary>
+ /// Returns a value indicating whether an upload matching the specified <paramref name="expression"/> exists.
+ /// </summary>
+ /// <param name="expression">The expression used to match uploads.</param>
+ /// <returns>A value indicating whether an upload matching the specified expression exists.</returns>
+ public async Task<bool> ExistsAsync(Expression<Func<Transfer, bool>> expression)
+ {
+ return (await FindAsync(expression)) != default;
+ }
+
+ /// <summary>
/// Finds a single upload matching the specified <paramref name="expression"/>.
/// </summary>
/// <param name="expression">The expression to use to match uploads.</param>
@@ -292,13 +294,15 @@ namespace slskd.Transfers.Uploads
}
/// <summary>
- /// Returns a value indicating whether an upload matching the specified <paramref name="expression"/> exists.
+ /// Updates the specified <paramref name="transfer"/>.
/// </summary>
- /// <param name="expression">The expression used to match uploads.</param>
- /// <returns>A value indicating whether an upload matching the specified expression exists.</returns>
- public async Task<bool> ExistsAsync(Expression<Func<Transfer, bool>> expression)
+ /// <param name="transfer">The transfer to update.</param>
+ /// <returns>The operation context.</returns>
+ 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