Skip to content

Commit

Permalink
Restore state listener after DB disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Jun 30, 2021
1 parent 30dff26 commit 0116d90
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions Tzkt.Api/Services/Sync/StateListener.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -61,17 +62,34 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)

using var db = new NpgsqlConnection(ConnectionString);
db.Notification += OnStateChanged;
await db.OpenAsync(cancellationToken);
await db.ExecuteAsync("LISTEN state_changed;");

while (!cancellationToken.IsCancellationRequested)
await db.WaitAsync(cancellationToken);
{
try
{
if (db.State != ConnectionState.Open)
{
await db.OpenAsync(cancellationToken);
await db.ExecuteAsync("LISTEN state_changed;");
Logger.LogInformation("State listener connected");
}
await db.WaitAsync(cancellationToken);
}
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested) { break; }
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { break; }
catch (Exception ex)
{
Logger.LogError("State listener disconnected: {0}", ex.Message);
await Task.Delay(1000, cancellationToken);
}
}

db.Notification -= OnStateChanged;
}
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested) { }
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { }
catch (Exception ex)
{
// TODO: reanimate listener without breaking HubProcessors state
Logger.LogCritical($"State listener crashed: {ex.Message}");
}
finally
Expand Down

0 comments on commit 0116d90

Please sign in to comment.