< Summary

Information
Class: Syki.Daemon.Tasks.PostgresListener
Assembly: Daemon
File(s): /home/runner/work/syki/syki/Daemon/Tasks/PostgresListener.cs
Tag: 22_11348620282
Line coverage
93%
Covered lines: 14
Uncovered lines: 1
Coverable lines: 15
Total lines: 53
Line coverage: 93.3%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ExecuteAsync()83.33%6.04690%
CreateTrigger()100%11100%

File(s)

/home/runner/work/syki/syki/Daemon/Tasks/PostgresListener.cs

#LineLine coverage
 1using Dapper;
 2using Npgsql;
 3using Hangfire;
 4
 5namespace Syki.Daemon.Tasks;
 6
 17public class PostgresListener(IConfiguration configuration) : BackgroundService
 8{
 9    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
 10    {
 111        await using var connection = new NpgsqlConnection(configuration.Database().ConnectionString);
 12
 113        await connection.OpenAsync(stoppingToken);
 14
 115        await CreateTrigger(connection);
 16
 117        connection.Notification += (o, e) =>
 118        {
 79619            BackgroundJob.Enqueue<SykiTasksProcessor>(x => x.Run());
 79720        };
 21
 122        await using (var cmd = new NpgsqlCommand("LISTEN new_task;", connection))
 23        {
 124            await cmd.ExecuteNonQueryAsync(stoppingToken);
 25        }
 26
 79727        while (!stoppingToken.IsCancellationRequested)
 28        {
 79729            await connection.WaitAsync(stoppingToken);
 30        }
 031    }
 32
 33    private async Task CreateTrigger(NpgsqlConnection connection)
 34    {
 35        const string sql = @"
 36            CREATE OR REPLACE FUNCTION notify_new_task_trigger()
 37            RETURNS trigger
 38            LANGUAGE 'plpgsql'
 39            AS $BODY$
 40            BEGIN
 41                PERFORM pg_notify('new_task', '');
 42                RETURN NEW;
 43            END
 44            $BODY$;
 45
 46            CREATE OR REPLACE TRIGGER notify_new_task_trigger
 47            AFTER INSERT ON syki.tasks
 48            FOR EACH ROW EXECUTE PROCEDURE notify_new_task_trigger();
 49        ";
 50
 151        await connection.ExecuteAsync(sql);
 152    }
 53}