-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkerService.cs
54 lines (49 loc) · 1.94 KB
/
WorkerService.cs
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
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using DomoticzToRouterSmsBot.Loader;
using DomoticzToRouterSmsBot.Proccessor;
internal class WorkerService : IHostedService
{
private readonly ILogger _logger;
private readonly ISmsLoader _loader;
private readonly ISmsRunner _runner;
private readonly IConfiguration _configuration;
private readonly IHostApplicationLifetime _lifeTime;
public WorkerService(ILogger<WorkerService> logger, ISmsLoader loader, ISmsRunner runner, IConfiguration configuration, IHostApplicationLifetime lifeTime){
_logger = logger;
_loader = loader;
_runner = runner;
_configuration = configuration;
_lifeTime = lifeTime;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Starting application");
var sms = await _loader.LoadAsync();
_logger.LogInformation($"SMS to process {sms?.Count()}");
DateTime lastProccessedTime = DateTime.MinValue;
if (!String.IsNullOrEmpty(_configuration["LastProccessedTime"]))
{
lastProccessedTime = Convert.ToDateTime(_configuration["LastProccessedTime"]);
}
_logger.LogInformation($"Last proccessed time {lastProccessedTime}");
foreach (var smsToProccess in sms.Where(s => s.RecievedTime > lastProccessedTime).OrderBy(s => s.Index))
{
if(cancellationToken.IsCancellationRequested)
return;
_logger.LogInformation($"Proccessing sms with id {smsToProccess.Index}");
await _runner.RunAsync(smsToProccess);
}
_logger.LogInformation("All done!");
_lifeTime.StopApplication();
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}