-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVeevaBackgroundService.cs
More file actions
184 lines (162 loc) · 5.83 KB
/
VeevaBackgroundService.cs
File metadata and controls
184 lines (162 loc) · 5.83 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
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ReachOutVeevaPromoMats.Configurations;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
namespace ReachOutVeevaPromoMats
{
/// <summary>
/// Background service for processing Veeva promotional materials
/// </summary>
public class VeevaBackgroundService : BackgroundService
{
private readonly ILogger<VeevaBackgroundService> _logger;
private readonly AppConfiguration _configuration;
private System.Timers.Timer? _dropFileTimer;
private System.Timers.Timer? _apiTimer;
private bool _apiSweepAll = true;
public VeevaBackgroundService(ILogger<VeevaBackgroundService> logger, AppConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
_logger.LogInformation("Veeva Background Service starting...");
StartTimers();
_logger.LogInformation("Veeva Background Service started.");
// Keep the service running until cancellation is requested
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(1000, stoppingToken);
}
}
catch (OperationCanceledException)
{
// Expected when cancellation is requested
_logger.LogInformation("Veeva Background Service stopping due to cancellation.");
}
catch (Exception ex)
{
_logger.LogError(ex, "Fatal error in Veeva Background Service: {Message}", ex.Message);
throw;
}
finally
{
StopTimers();
_logger.LogInformation("Veeva Background Service stopped.");
}
}
/// <summary>
/// Starts the timers for drop file and API processing.
/// </summary>
private void StartTimers()
{
if (_configuration.DropFileEnabled)
{
_logger.LogInformation("Starting drop file timer.");
_dropFileTimer = new System.Timers.Timer();
_dropFileTimer.Elapsed += OnDropFileTimerEvent;
_dropFileTimer.Interval = _configuration.RunAtStartup ? 100 : _configuration.DropFilePollFrequency * 60000;
_dropFileTimer.Enabled = true;
}
if (_configuration.VeevaAPIEnabled)
{
_logger.LogInformation("Starting API timer.");
_apiTimer = new System.Timers.Timer();
_apiTimer.Elapsed += OnAPITimerEvent;
_apiTimer.Interval = _configuration.RunAtStartup ? 100 : GetNextSweepInterval();
_apiTimer.Enabled = true;
}
}
/// <summary>
/// Stops the timers.
/// </summary>
private void StopTimers()
{
_dropFileTimer?.Stop();
_dropFileTimer?.Dispose();
_dropFileTimer = null;
_apiTimer?.Stop();
_apiTimer?.Dispose();
_apiTimer = null;
}
/// <summary>
/// Gets the next sweep interval.
/// </summary>
/// <returns>Interval in milliseconds</returns>
private double GetNextSweepInterval()
{
double interval = _configuration.GetNextSweepInterval();
_logger.LogInformation("API sweep scheduled for {ScheduledTime}", DateTime.Now.AddMilliseconds(interval));
return interval;
}
/// <summary>
/// Handles the drop file timer event.
/// </summary>
private void OnDropFileTimerEvent(object? sender, ElapsedEventArgs e)
{
try
{
if (_dropFileTimer != null)
{
_dropFileTimer.Enabled = false;
var businessLogic = new BusinessLogic(_configuration);
businessLogic.ProcessDroppedFiles();
_dropFileTimer.Interval = _configuration.DropFilePollFrequency * 60000;
_dropFileTimer.Enabled = true;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in drop file timer event: {Message}", ex.Message);
}
}
/// <summary>
/// Handles the API timer event.
/// </summary>
private void OnAPITimerEvent(object? sender, ElapsedEventArgs e)
{
try
{
if (_apiTimer != null)
{
_apiTimer.Enabled = false;
var businessLogic = new BusinessLogic(_configuration);
businessLogic.SweepAPIForProducts();
_apiTimer.Interval = GetNextSweepInterval();
_apiSweepAll = false;
_apiTimer.Enabled = true;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in API timer event: {Message}", ex.Message);
}
}
/// <summary>
/// Executes work for interactive mode.
/// </summary>
public void DoWork()
{
var businessLogic = new BusinessLogic(_configuration);
if (_configuration.DropFileEnabled)
{
businessLogic.ProcessDroppedFiles();
}
if (_configuration.VeevaAPIEnabled)
{
businessLogic.SweepAPIForProducts();
}
}
public override void Dispose()
{
StopTimers();
base.Dispose();
}
}
}