diff --git a/DysonNetwork.Drive/Storage/FileUploadController.cs b/DysonNetwork.Drive/Storage/FileUploadController.cs index 2c5c10c..5067c5f 100644 --- a/DysonNetwork.Drive/Storage/FileUploadController.cs +++ b/DysonNetwork.Drive/Storage/FileUploadController.cs @@ -261,7 +261,7 @@ public class FileUploadController( ); // Update task status to "processing" - background processing is now happening - await persistentTaskService.UpdateTaskProgressAsync(taskId, 95, "Processing file in background..."); + await persistentTaskService.UpdateTaskProgressAsync(taskId, 0.95, "Processing file in background..."); // Send upload completion notification (file is uploaded, but processing continues) await persistentTaskService.SendUploadCompletedNotificationAsync(persistentTask, fileId); @@ -312,15 +312,13 @@ public class FileUploadController( { var chunkPath = Path.Combine(taskPath, i + ".chunk"); if (!System.IO.File.Exists(chunkPath)) - { throw new InvalidOperationException("Chunk " + i + " is missing."); - } await using var chunkStream = new FileStream(chunkPath, FileMode.Open); await chunkStream.CopyToAsync(mergedStream); // Update progress after each chunk is merged - var currentProgress = baseProgress + (progressPerChunk * (i + 1)); + var currentProgress = baseProgress + progressPerChunk * (i + 1); await persistentTaskService.UpdateTaskProgressAsync( taskId, currentProgress, diff --git a/DysonNetwork.Drive/Storage/PersistentTaskService.cs b/DysonNetwork.Drive/Storage/PersistentTaskService.cs index 1e762e3..f6a0c5d 100644 --- a/DysonNetwork.Drive/Storage/PersistentTaskService.cs +++ b/DysonNetwork.Drive/Storage/PersistentTaskService.cs @@ -69,34 +69,34 @@ public class PersistentTaskService( if (task is null) return; var previousProgress = task.Progress; + var delta = progress - previousProgress; var clampedProgress = Math.Clamp(progress, 0, 1.0); var now = SystemClock.Instance.GetCurrentInstant(); + // Update the cached task + task.Progress = clampedProgress; + task.LastActivity = now; + task.UpdatedAt = now; + if (statusMessage is not null) + task.Description = statusMessage; + + await SetCacheAsync(task); + + // Send progress update notification + await SendTaskProgressUpdateAsync(task, task.Progress, previousProgress); + + // Only updates when update in period // Use ExecuteUpdateAsync for better performance - update only the fields we need - var updatedRows = await db.Tasks - .Where(t => t.TaskId == taskId) - .ExecuteUpdateAsync(setters => setters - .SetProperty(t => t.Progress, clampedProgress) - .SetProperty(t => t.LastActivity, now) - .SetProperty(t => t.UpdatedAt, now) - .SetProperty(t => t.Description, t => statusMessage ?? t.Description) - ); - - if (updatedRows > 0) + if (Math.Abs(progress - 1) < 0.1 || delta * 100 > 5) { - // Update the cached task - task.Progress = clampedProgress; - task.LastActivity = now; - task.UpdatedAt = now; - if (statusMessage is not null) - { - task.Description = statusMessage; - } - - await SetCacheAsync(task); - - // Send progress update notification - await SendTaskProgressUpdateAsync(task, task.Progress, previousProgress); + await db.Tasks + .Where(t => t.TaskId == taskId) + .ExecuteUpdateAsync(setters => setters + .SetProperty(t => t.Progress, clampedProgress) + .SetProperty(t => t.LastActivity, now) + .SetProperty(t => t.UpdatedAt, now) + .SetProperty(t => t.Description, t => statusMessage ?? t.Description) + ); } } @@ -384,6 +384,7 @@ public class PersistentTaskService( TaskId = task.TaskId, Name = task.Name, Type = task.Type.ToString(), + Parameters = task.Parameters, CreatedAt = task.CreatedAt.ToString() }; @@ -409,10 +410,6 @@ public class PersistentTaskService( { try { - // Only send significant progress updates (every 5% or major milestones) - if (Math.Abs(newProgress - previousProgress) < 5 && newProgress < 100 && newProgress > 0) - return; - var data = new TaskProgressData { TaskId = task.TaskId, @@ -634,7 +631,7 @@ public class PersistentTaskService( var chunkSize = request.ChunkSize ?? 1024 * 1024 * 5; // 5MB default var chunksCount = (int)Math.Ceiling((double)request.FileSize / chunkSize); - // Use default pool if no pool is specified, or find first available pool + // Use the default pool if no pool is specified, or find first available pool var poolId = request.PoolId ?? await GetFirstAvailablePoolIdAsync(); var uploadTask = new PersistentUploadTask @@ -703,7 +700,7 @@ public class PersistentTaskService( { parameters.UploadedChunks.Add(chunkIndex); parameters.ChunksUploaded = parameters.UploadedChunks.Count; - + var now = SystemClock.Instance.GetCurrentInstant(); // Use ExecuteUpdateAsync to update the Parameters dictionary directly @@ -1057,6 +1054,7 @@ public class TaskCreatedData public string Name { get; set; } = null!; public string Type { get; set; } = null!; public string CreatedAt { get; set; } = null!; + public Dictionary? Parameters { get; set; } } public class TaskProgressData @@ -1166,4 +1164,4 @@ public class RecentActivity public double Progress { get; set; } } -#endregion +#endregion \ No newline at end of file