Edition 0 Updated to asp. Net core 0



Yüklə 11,82 Mb.
Pdf görüntüsü
səhifə147/288
tarix12.07.2023
ölçüsü11,82 Mb.
#136458
1   ...   143   144   145   146   147   148   149   150   ...   288
public
class
GracePeriodManagerService : BackgroundService 

private
readonly
ILogger _logger; 
private
readonly
OrderingBackgroundSettings _settings; 
private
readonly
IEventBus _eventBus; 
public
GracePeriodManagerService
(IOptions settings, 
IEventBus eventBus, 
ILogger logger) 

// Constructor's parameters validations...

protected
override
async Task 
ExecuteAsync
(CancellationToken stoppingToken) 

_logger.
LogDebug
($
"GracePeriodManagerService is starting."
); 
stoppingToken.
Register
(() => 
_logger.
LogDebug
($
" GracePeriod background task is stopping."
)); 
while
(!stoppingToken.
IsCancellationRequested


_logger.
LogDebug
($
"GracePeriod task doing background work."
); 
// This eShopOnContainers method is querying a database table
// and publishing events into the Event Bus (RabbitMQ / ServiceBus)
CheckConfirmedGracePeriodOrders
(); 
try

await Task.
Delay
(_settings.
CheckUpdateTime
, stoppingToken); 

catch
(TaskCanceledException exception) { 
_logger.
LogCritical
(exception, 
"TaskCanceledException Error"



162 
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications 
exception.
Message
); 


_logger.
LogDebug
($
"GracePeriod background task is stopping."
); 

.../... 

In this specific case for eShopOnContainers, it’s executing an application method that’s querying a 
database table looking for orders with a specific state and when applying changes, it is publishing 
integration events through the event bus (underneath it can be using RabbitMQ or Azure Service Bus). 
Of course, you could run any other business background task, instead. 
By default, the cancellation token is set with a 5 seconds timeout, although you can change that value 
when building your 
WebHost
using the 
UseShutdownTimeout
extension of the 
IWebHostBuilder
. This 
means that our service is expected to cancel within 5 seconds otherwise it will be more abruptly killed. 
The following code would be changing that time to 10 seconds. 
WebHost.
CreateDefaultBuilder
(args) 
.
UseShutdownTimeout
(TimeSpan.
FromSeconds
(
10
)) 
... 

Yüklə 11,82 Mb.

Dostları ilə paylaş:
1   ...   143   144   145   146   147   148   149   150   ...   288




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin