Created
May 6, 2020 12:25
-
-
Save ayende/d13d584fd8213914375f939a40228827 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
{ | |
// Don't try to start right off the bat | |
await Task.Delay(_settings.FirstPollingTime, stoppingToken); | |
_connection = new NpgsqlConnection(_settings.ConnectionString); | |
await _connection.OpenAsync(stoppingToken); | |
while (!stoppingToken.IsCancellationRequested) | |
{ | |
var state = await _process.State(); | |
if (state != ProcessState.Active) | |
{ | |
// If you can take the global lock, start | |
// the process | |
if (await _connection.TryGetGlobalLock(_settings.LockId, cancellation: stoppingToken)) | |
{ | |
await _process.Start(_connection); | |
} | |
} | |
await Task.Delay(_settings.OwnershipPollingTime, stoppingToken); | |
} | |
if (_connection.State != ConnectionState.Closed) | |
{ | |
await _connection.DisposeAsync(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment