< Summary

Information
Class: Syki.Daemon.Tasks.SykiTasksProcessor
Assembly: Daemon
File(s): /home/runner/work/syki/syki/Daemon/Tasks/SykiTasksProcessor.cs
Tag: 22_11348620282
Line coverage
88%
Covered lines: 23
Uncovered lines: 3
Coverable lines: 26
Total lines: 70
Line coverage: 88.4%
Branch coverage
86%
Covered branches: 19
Total branches: 22
Branch coverage: 86.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%
Run()86.36%24.242283.33%
GetData(...)100%11100%
GetHandler(...)100%11100%

File(s)

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

#LineLine coverage
 1using Dapper;
 2using Npgsql;
 3using Newtonsoft.Json;
 4
 5namespace Syki.Daemon.Tasks;
 6
 7977public class SykiTasksProcessor(IConfiguration configuration, IServiceScopeFactory serviceScopeFactory)
 8{
 9    public async Task Run()
 10    {
 79711        using var scope = serviceScopeFactory.CreateScope();
 79712        await using var connection = new NpgsqlConnection(configuration.Database().ConnectionString);
 13
 14        const string pickRowsSql = @"
 15            UPDATE syki.tasks
 16            SET processor_id = @ProcessorId
 17            WHERE processor_id IS NULL
 18        ";
 19
 79720        var processorId = Guid.NewGuid();
 79721        var rows = await connection.ExecuteAsync(pickRowsSql, new { processorId });
 79722        if (rows == 0) return;
 23
 24        const string sql = @"
 25            SELECT * FROM syki.tasks
 26            WHERE processor_id = @ProcessorId AND processed_at IS NULL
 27        ";
 28
 75029        var tasks = await connection.QueryAsync<SykiTask>(sql, new { processorId });
 30
 344631        foreach (var task in tasks)
 32        {
 97333            dynamic data = GetData(task);
 97334            dynamic handler = GetHandler(scope, task);
 97335            string? error = null;
 36
 37            try
 38            {
 97339                await handler.Handle(data);
 97340            }
 041            catch (Exception ex)
 42            {
 043                error = ex.Message + ex.InnerException?.Message;
 044            }
 45
 46            const string update = @"
 47                UPDATE syki.tasks
 48                SET processed_at = now(), error = @Error
 49                WHERE id = @Id
 50            ";
 51
 97352            await connection.ExecuteAsync(update, new { task.Id, error });
 97353        }
 79754    }
 55
 56    private static dynamic GetData(SykiTask task)
 57    {
 97358        var type = typeof(SykiTask).Assembly.GetType(task.Type)!;
 97359        dynamic data = JsonConvert.DeserializeObject(task.Data, type)!;
 97360        return data;
 61    }
 62
 63    private static dynamic GetHandler(IServiceScope scope, SykiTask task)
 64    {
 97365        var type = task.Type.Split(".").Last();
 97366        var handlerType = typeof(SykiTasksProcessor).Assembly.GetType($"Syki.Daemon.Tasks.{type}Handler")!;
 97367        dynamic handler = scope.ServiceProvider.GetRequiredService(handlerType);
 97368        return handler;
 69    }
 70}