< Summary - Estud

Information
Class: Estud.Back.Webhooks.PendingWebhookCallsProcessor
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Webhooks/PendingWebhookCallsProcessor.cs
Tag: 114_29044117136
Line coverage
100%
Covered lines: 35
Uncovered lines: 0
Coverable lines: 35
Total lines: 65
Line coverage: 100%
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%
.cctor()100%11100%
Execute()100%11100%
Process()83.33%66100%

File(s)

/home/runner/work/syki/syki/Back/Webhooks/PendingWebhookCallsProcessor.cs

#LineLine coverage
 1using Quartz;
 2using System.Diagnostics;
 3using Estud.Back.Features.Webhooks.CallWebhooks;
 4
 5namespace Estud.Back.Webhooks;
 6
 187public class PendingWebhookCallsProcessor(IServiceScopeFactory serviceScopeFactory) : IJob
 8{
 29    private static readonly ActivitySource _activitySource = new(OpenTelemetryConfigs.WebhookCallsProcessing);
 10
 11    public async Task Execute(IJobExecutionContext context)
 12    {
 1813        using var scope = serviceScopeFactory.CreateScope();
 1814        var ctx = scope.ServiceProvider.GetRequiredService<EstudDbContext>();
 15
 1816        await Process(scope, ctx);
 1817    }
 18
 19    private static async Task Process(IServiceScope scope, EstudDbContext ctx)
 20    {
 1221        while (true)
 22        {
 3023            using var activity = _activitySource.StartActivity("Enqueue CallWebhookCommands", ActivityKind.Producer);
 24
 3025            await ctx.Database.BeginTransactionAsync();
 26
 3027            var calls = await ctx.WebhookCalls.FromSqlRaw(Sql).AsNoTracking().ToListAsync();
 3028            if (calls.Count == 0)
 29            {
 1830                await ctx.Database.RollbackTransactionAsync();
 1831                break;
 32            }
 33
 1234            activity?.SetTag("webhook_calls.count", calls.Count);
 35
 6036            foreach (var call in calls)
 37            {
 1838                ctx.AddCommand(call.InstitutionId, new CallWebhookCommand(call.Id));
 39            }
 40
 1241            await ctx.SaveChangesAsync();
 1242            await ctx.Database.CommitTransactionAsync();
 43
 1244            var scheduler = await scope.ServiceProvider.GetRequiredService<ISchedulerFactory>().GetScheduler();
 1245            await scheduler.TriggerCommandsProcessorJob();
 1246        }
 1847    }
 48
 49    /// <summary>
 50    /// See <see cref="WebhookCallStatus"/> for status mapping
 51    /// </summary>
 252    private static readonly string Sql = @"
 253        UPDATE estud.webhook_calls
 254        SET status = 1
 255        WHERE ctid IN (
 256            SELECT ctid
 257            FROM estud.webhook_calls
 258            WHERE status = 0
 259            ORDER BY created_at
 260            FOR UPDATE SKIP LOCKED
 261            LIMIT 100
 262        )
 263        RETURNING *;
 264    ";
 65}