| | 1 | | using Dapper; |
| | 2 | | using Npgsql; |
| | 3 | | using Hangfire; |
| | 4 | |
|
| | 5 | | namespace Syki.Daemon.Tasks; |
| | 6 | |
|
| 1 | 7 | | public class PostgresListener(IConfiguration configuration) : BackgroundService |
| | 8 | | { |
| | 9 | | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| | 10 | | { |
| 1 | 11 | | await using var connection = new NpgsqlConnection(configuration.Database().ConnectionString); |
| | 12 | |
|
| 1 | 13 | | await connection.OpenAsync(stoppingToken); |
| | 14 | |
|
| 1 | 15 | | await CreateTrigger(connection); |
| | 16 | |
|
| 1 | 17 | | connection.Notification += (o, e) => |
| 1 | 18 | | { |
| 796 | 19 | | BackgroundJob.Enqueue<SykiTasksProcessor>(x => x.Run()); |
| 797 | 20 | | }; |
| | 21 | |
|
| 1 | 22 | | await using (var cmd = new NpgsqlCommand("LISTEN new_task;", connection)) |
| | 23 | | { |
| 1 | 24 | | await cmd.ExecuteNonQueryAsync(stoppingToken); |
| | 25 | | } |
| | 26 | |
|
| 797 | 27 | | while (!stoppingToken.IsCancellationRequested) |
| | 28 | | { |
| 797 | 29 | | await connection.WaitAsync(stoppingToken); |
| | 30 | | } |
| 0 | 31 | | } |
| | 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 | |
|
| 1 | 51 | | await connection.ExecuteAsync(sql); |
| 1 | 52 | | } |
| | 53 | | } |