MailSharp / MailSharp.MailClient / Services / Logging / BackgroundIndexingStatus.cs
Code · 37 lines · 1495 bytes
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
37namespace MailSharp.MailClient.Services.Logging;

public interface IBackgroundIndexingStatus
{
	int MaxConcurrentIndexing { get; }
	int CurrentlyAvailableSlots { get; }
	IReadOnlyCollection<string> InProgressKeys { get; }

	// Only populated while a key's index build is doing a full reindex (see
	// ImapService.EnsureFolderIndexedAsync) - returns false for keys still in InProgressKeys but
	// not yet past the initial connect/search step, or doing a cheap incremental sync instead.
	bool TryGetProgress(string key, out int processed, out int total);
}

// Thin read-only view over ImapService's process-wide static indexing state - that state is
// already shared across every request (see ImapService.IndexingInProgress/
// BackgroundIndexingThrottle), so this just exposes it for the Maintenance page instead of
// routing background indexing through a new service.
public class BackgroundIndexingStatus : IBackgroundIndexingStatus
{
	public int MaxConcurrentIndexing => ImapService.MaxConcurrentBackgroundIndexing;
	public int CurrentlyAvailableSlots => ImapService.BackgroundIndexingThrottle.CurrentCount;
	public IReadOnlyCollection<string> InProgressKeys => [.. ImapService.IndexingInProgress.Keys];

	public bool TryGetProgress(string key, out int processed, out int total)
	{
		if (ImapService.IndexingProgress.TryGetValue(key, out var progress))
		{
			processed = progress.Processed;
			total = progress.Total;
			return true;
		}
		processed = 0;
		total = 0;
		return false;
	}
}