MailSharp / MailSharp.IMAP / Services / ImapService.cs
Code · 69 lines · 2282 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
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
69using MailSharp.Common;
using MailSharp.Common.Services;
using MailSharp.IMAP.Metrics;
using MailSharp.IMAP.Server;
using MailSharp.IMAP.Session;

namespace MailSharp.IMAP.Services;

public class ImapService : BackgroundService
{
	private readonly ImapServer server;
	private readonly IConfiguration configuration;
	private readonly ImapMetrics metrics;
	private readonly ILogger<ImapService> logger;

	public ImapService(
		IConfiguration configuration,
		ILogger<ImapService> logger,
		ILogger<ImapServer> serverLogger,
		ILogger<ImapSession> sessionLogger,
		AuthenticationService authService,
		MailboxService mailboxService,
		ImapMetrics metrics)
	{
		this.configuration = configuration;
		this.metrics = metrics;
		this.logger = logger;
		this.server = new ImapServer(configuration, serverLogger, sessionLogger, authService, mailboxService, metrics);
	}

	protected override async Task ExecuteAsync(CancellationToken stoppingToken)
	{
		try
		{
			metrics.IsRunning = true;
			var eventIdConfig = configuration.GetSection("ImapEventIds:ServiceStarting").Get<EventIdConfig>()
				?? throw new InvalidOperationException("Missing ImapEventIds:ServiceStarting");
			logger.LogInformation(
				new EventId(eventIdConfig.Id, eventIdConfig.Name),
				configuration["ImapLogMessages:ServiceStarting"]);

			await server.StartAsync();
			await Task.Delay(Timeout.Infinite, stoppingToken);
		}
		catch (Exception ex)
		{
			var eventIdConfig = configuration.GetSection("ImapEventIds:ServiceError").Get<EventIdConfig>()
				?? throw new InvalidOperationException("Missing ImapEventIds:ServiceError");
			logger.LogError(
				new EventId(eventIdConfig.Id, eventIdConfig.Name),
				ex,
				configuration["ImapLogMessages:ServiceError"]);
			metrics.IsRunning = false;
			throw;
		}
	}

	public override async Task StopAsync(CancellationToken cancellationToken)
	{
		var eventIdConfig = configuration.GetSection("ImapEventIds:ServiceStopping").Get<EventIdConfig>()
			?? throw new InvalidOperationException("Missing ImapEventIds:ServiceStopping");
		logger.LogInformation(
			new EventId(eventIdConfig.Id, eventIdConfig.Name),
			configuration["ImapLogMessages:ServiceStopping"]);
		metrics.IsRunning = false;
		await server.StopAsync();
		await base.StopAsync(cancellationToken);
	}
}