Skip to content

Instantly share code, notes, and snippets.

@ayende
Created May 6, 2020 12:25
Show Gist options
  • Save ayende/d13d584fd8213914375f939a40228827 to your computer and use it in GitHub Desktop.
Save ayende/d13d584fd8213914375f939a40228827 to your computer and use it in GitHub Desktop.
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