forked from LykosAI/StabilityMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrackedDownloadService.cs
More file actions
483 lines (415 loc) · 16 KB
/
TrackedDownloadService.cs
File metadata and controls
483 lines (415 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
using System.Collections.Concurrent;
using System.Text;
using System.Text.Json;
using AsyncAwaitBestPractices;
using Microsoft.Extensions.Logging;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Models;
using StabilityMatrix.Core.Models.FileInterfaces;
using StabilityMatrix.Core.Models.Progress;
namespace StabilityMatrix.Core.Services;
public class TrackedDownloadService : ITrackedDownloadService, IDisposable
{
private readonly ILogger<TrackedDownloadService> logger;
private readonly IDownloadService downloadService;
private readonly ISettingsManager settingsManager;
private readonly IModelIndexService modelIndexService;
private readonly ConcurrentDictionary<Guid, (TrackedDownload Download, FileStream Stream)> downloads =
new();
private readonly ConcurrentQueue<TrackedDownload> pendingDownloads = new();
private readonly SemaphoreSlim downloadSemaphore;
public IEnumerable<TrackedDownload> Downloads => downloads.Values.Select(x => x.Download);
public IEnumerable<TrackedDownload> PendingDownloads => pendingDownloads;
/// <inheritdoc />
public event EventHandler<TrackedDownload>? DownloadAdded;
public event EventHandler<TrackedDownload>? DownloadStarted;
private int MaxConcurrentDownloads { get; set; }
private bool IsQueueEnabled => MaxConcurrentDownloads > 0;
public int ActiveDownloads =>
downloads.Count(kvp => kvp.Value.Download.ProgressState == ProgressState.Working);
public TrackedDownloadService(
ILogger<TrackedDownloadService> logger,
IDownloadService downloadService,
IModelIndexService modelIndexService,
ISettingsManager settingsManager
)
{
this.logger = logger;
this.downloadService = downloadService;
this.settingsManager = settingsManager;
this.modelIndexService = modelIndexService;
// Index for in-progress downloads when library dir loaded
settingsManager.RegisterOnLibraryDirSet(path =>
{
var downloadsDir = new DirectoryPath(settingsManager.DownloadsDirectory);
// Ignore if not exist
if (!downloadsDir.Exists)
return;
LoadInProgressDownloads(downloadsDir);
});
MaxConcurrentDownloads = settingsManager.Settings.MaxConcurrentDownloads;
downloadSemaphore = new SemaphoreSlim(MaxConcurrentDownloads);
}
private void OnDownloadAdded(TrackedDownload download)
{
logger.LogInformation("Download added: ({Download}, {State})", download.Id, download.ProgressState);
DownloadAdded?.Invoke(this, download);
}
private void OnDownloadStarted(TrackedDownload download)
{
logger.LogInformation("Download started: ({Download}, {State})", download.Id, download.ProgressState);
DownloadStarted?.Invoke(this, download);
}
/// <summary>
/// Creates a new tracked download with backed json file and adds it to the dictionary.
/// </summary>
/// <param name="download"></param>
private void AddDownload(TrackedDownload download)
{
// Set download service
download.SetDownloadService(downloadService);
// Create json file
var downloadsDir = new DirectoryPath(settingsManager.DownloadsDirectory);
downloadsDir.Create();
var jsonFile = downloadsDir.JoinFile($"{download.Id}.json");
var jsonFileStream = jsonFile.Info.Open(FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read);
// Serialize to json
var json = JsonSerializer.Serialize(download);
jsonFileStream.Write(Encoding.UTF8.GetBytes(json));
jsonFileStream.Flush();
// Add to dictionary
downloads.TryAdd(download.Id, (download, jsonFileStream));
// Connect to state changed event to update json file
AttachHandlers(download);
OnDownloadAdded(download);
}
public async Task TryStartDownload(TrackedDownload download)
{
if (IsQueueEnabled && ActiveDownloads >= MaxConcurrentDownloads)
{
logger.LogDebug("Download {Download} is pending", download.FileName);
pendingDownloads.Enqueue(download);
download.SetPending();
UpdateJsonForDownload(download);
return;
}
if (!IsQueueEnabled || await downloadSemaphore.WaitAsync(0).ConfigureAwait(false))
{
logger.LogDebug("Starting download {Download}", download.FileName);
download.Start();
OnDownloadStarted(download);
}
else
{
logger.LogDebug("Download {Download} is pending", download.FileName);
pendingDownloads.Enqueue(download);
download.SetPending();
UpdateJsonForDownload(download);
}
}
public async Task TryRestartDownload(TrackedDownload download)
{
// Re-create the backing JSON file and re-add to the dictionary.
// Downloads are removed on failure, so this restores the tracking entry
// so that subsequent state-change events can persist normally.
var downloadsDir = new DirectoryPath(settingsManager.DownloadsDirectory);
downloadsDir.Create();
var jsonFile = downloadsDir.JoinFile($"{download.Id}.json");
var jsonFileStream = new FileStream(
jsonFile.Info.FullName,
FileMode.Create,
FileAccess.ReadWrite,
FileShare.Read,
bufferSize: 4096,
useAsync: true
);
var jsonBytes = JsonSerializer.SerializeToUtf8Bytes(download);
try
{
await jsonFileStream.WriteAsync(jsonBytes).ConfigureAwait(false);
await jsonFileStream.FlushAsync().ConfigureAwait(false);
// Handlers are already attached from the original AddDownload call.
if (!downloads.TryAdd(download.Id, (download, jsonFileStream)))
{
// Already tracked; discard the newly opened stream.
await jsonFileStream.DisposeAsync().ConfigureAwait(false);
}
}
catch
{
await jsonFileStream.DisposeAsync().ConfigureAwait(false);
throw;
}
await TryResumeDownload(download).ConfigureAwait(false);
}
public async Task TryResumeDownload(TrackedDownload download)
{
if (IsQueueEnabled && ActiveDownloads >= MaxConcurrentDownloads)
{
logger.LogDebug("Download {Download} is pending", download.FileName);
pendingDownloads.Enqueue(download);
download.SetPending();
UpdateJsonForDownload(download);
return;
}
if (!IsQueueEnabled || await downloadSemaphore.WaitAsync(0).ConfigureAwait(false))
{
logger.LogDebug("Resuming download {Download}", download.FileName);
download.Resume();
OnDownloadStarted(download);
}
else
{
logger.LogDebug("Download {Download} is pending", download.FileName);
pendingDownloads.Enqueue(download);
download.SetPending();
UpdateJsonForDownload(download);
}
}
public void UpdateMaxConcurrentDownloads(int newMax)
{
if (newMax <= 0)
{
MaxConcurrentDownloads = 0;
return;
}
var oldMax = MaxConcurrentDownloads;
MaxConcurrentDownloads = newMax;
if (oldMax == newMax)
return;
logger.LogInformation("Updating max concurrent downloads from {OldMax} to {NewMax}", oldMax, newMax);
if (newMax > oldMax)
{
downloadSemaphore.Release(newMax - oldMax);
ProcessPendingDownloads().SafeFireAndForget();
}
// When reducing, we don't need to do anything immediately.
// The system will naturally adjust as downloads complete or are paused/resumed.
AdjustSemaphoreCount();
}
private void AdjustSemaphoreCount()
{
var currentCount = downloadSemaphore.CurrentCount;
var targetCount = MaxConcurrentDownloads - ActiveDownloads;
if (currentCount < targetCount)
{
downloadSemaphore.Release(targetCount - currentCount);
}
else if (currentCount > targetCount)
{
for (var i = 0; i < currentCount - targetCount; i++)
{
downloadSemaphore.Wait(0);
}
}
}
/// <summary>
/// Update the json file for the download.
/// </summary>
private void UpdateJsonForDownload(TrackedDownload download)
{
// Serialize to json
var json = JsonSerializer.Serialize(download);
var jsonBytes = Encoding.UTF8.GetBytes(json);
// Write to file
var (_, fs) = downloads[download.Id];
fs.Seek(0, SeekOrigin.Begin);
fs.Write(jsonBytes);
fs.Flush();
}
private void AttachHandlers(TrackedDownload download)
{
download.ProgressStateChanged += TrackedDownload_OnProgressStateChanged;
}
private async Task ProcessPendingDownloads()
{
while (pendingDownloads.TryPeek(out var nextDownload))
{
if (ActiveDownloads >= MaxConcurrentDownloads)
{
break;
}
if (pendingDownloads.TryDequeue(out nextDownload))
{
if (nextDownload.DownloadedBytes > 0)
{
await TryResumeDownload(nextDownload).ConfigureAwait(false);
}
else
{
await TryStartDownload(nextDownload).ConfigureAwait(false);
}
}
else
{
break;
}
}
}
/// <summary>
/// Handler when the download's state changes
/// </summary>
private void TrackedDownload_OnProgressStateChanged(object? sender, ProgressState e)
{
if (sender is not TrackedDownload download)
{
return;
}
// Update json file
UpdateJsonForDownload(download);
// If the download is completed, remove it from the dictionary and delete the json file
if (e is ProgressState.Success or ProgressState.Failed or ProgressState.Cancelled)
{
if (downloads.TryRemove(download.Id, out var downloadInfo))
{
downloadInfo.Item2.Dispose();
// Delete json file
new DirectoryPath(settingsManager.DownloadsDirectory)
.JoinFile($"{download.Id}.json")
.Delete();
logger.LogDebug("Removed download {Download}", download.FileName);
if (IsQueueEnabled)
{
try
{
downloadSemaphore.Release();
}
catch (SemaphoreFullException)
{
// Ignore
}
ProcessPendingDownloads().SafeFireAndForget();
}
}
}
else if (e is ProgressState.Paused && IsQueueEnabled)
{
downloadSemaphore.Release();
ProcessPendingDownloads().SafeFireAndForget();
}
// On successes, run the continuation action
if (e == ProgressState.Success)
{
if (download.ContextAction is not null)
{
logger.LogDebug("Running context action for {Download}", download.FileName);
}
switch (download.ContextAction)
{
case CivitPostDownloadContextAction action:
action.Invoke(settingsManager, modelIndexService);
break;
case ModelPostDownloadContextAction action:
action.Invoke(modelIndexService);
break;
}
}
}
private void LoadInProgressDownloads(DirectoryPath downloadsDir)
{
logger.LogDebug("Indexing in-progress downloads at {DownloadsDir}...", downloadsDir);
var jsonFiles = downloadsDir.Info.EnumerateFiles("*.json", EnumerationOptionConstants.TopLevelOnly);
// Add to dictionary, the file name is the guid
foreach (var file in jsonFiles)
{
// Try to get a shared write handle
try
{
var fileStream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
// Deserialize json and add to dictionary
var download = JsonSerializer.Deserialize<TrackedDownload>(fileStream)!;
// If the download is marked as working, pause it
if (download.ProgressState is ProgressState.Working or ProgressState.Pending)
{
download.ProgressState = ProgressState.Paused;
}
else if (
download.ProgressState != ProgressState.Inactive
&& download.ProgressState != ProgressState.Paused
&& download.ProgressState != ProgressState.Pending
)
{
// If the download is not inactive, skip it
logger.LogWarning(
"Skipping download {Download} with state {State}",
download.FileName,
download.ProgressState
);
fileStream.Dispose();
// Delete json file
logger.LogDebug(
"Deleting json file for {Download} with unsupported state",
download.FileName
);
file.Delete();
continue;
}
download.SetDownloadService(downloadService);
downloads.TryAdd(download.Id, (download, fileStream));
if (download.ProgressState == ProgressState.Pending)
{
pendingDownloads.Enqueue(download);
}
AttachHandlers(download);
OnDownloadAdded(download);
logger.LogDebug("Loaded in-progress download {Download}", download.FileName);
}
catch (Exception e)
{
logger.LogInformation(e, "Could not open file {File} for reading", file.Name);
}
}
}
public TrackedDownload NewDownload(Uri downloadUrl, FilePath downloadPath)
{
var download = new TrackedDownload
{
Id = Guid.NewGuid(),
SourceUrl = downloadUrl,
DownloadDirectory = downloadPath.Directory!,
FileName = downloadPath.Name,
TempFileName = NewTempFileName(downloadPath.Directory!),
};
AddDownload(download);
return download;
}
/// <summary>
/// Generate a new temp file name that is unique in the given directory.
/// In format of "Unconfirmed {id}.smdownload"
/// </summary>
/// <param name="parentDir"></param>
/// <returns></returns>
private static string NewTempFileName(DirectoryPath parentDir)
{
FilePath? tempFile = null;
for (var i = 0; i < 10; i++)
{
if (tempFile is { Exists: false })
{
return tempFile.Name;
}
var id = Random.Shared.Next(1000000, 9999999);
tempFile = parentDir.JoinFile($"Unconfirmed {id}.smdownload");
}
throw new Exception("Failed to generate a unique temp file name.");
}
/// <inheritdoc />
public void Dispose()
{
foreach (var (download, fs) in downloads.Values)
{
if (download.ProgressState == ProgressState.Working)
{
try
{
download.Pause();
}
catch (Exception e)
{
logger.LogWarning(e, "Failed to pause download {Download}", download.FileName);
}
}
}
GC.SuppressFinalize(this);
}
}