< Summary

Information
Class: Elsa.Alterations.Activities.GenerateAlterationJobs
Assembly: Elsa.Alterations
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations/Activities/GenerateAlterationJobs.cs
Line coverage
8%
Covered lines: 4
Uncovered lines: 45
Coverable lines: 49
Total lines: 104
Line coverage: 8.1%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
.ctor(...)100%11100%
get_PlanId()100%11100%
ExecuteAsync()0%620%
GetPlanAsync()0%620%
UpdatePlanStatusAsync()100%210%
FindMatchingWorkflowInstanceIdsAsync()100%210%
GenerateJobsAsync()100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations/Activities/GenerateAlterationJobs.cs

#LineLine coverage
 1using System.ComponentModel;
 2using System.Runtime.CompilerServices;
 3using Elsa.Alterations.Core.Contracts;
 4using Elsa.Alterations.Core.Entities;
 5using Elsa.Alterations.Core.Enums;
 6using Elsa.Alterations.Core.Filters;
 7using Elsa.Alterations.Core.Models;
 8using Elsa.Common;
 9using Elsa.Extensions;
 10using Elsa.Workflows;
 11using Elsa.Workflows.Attributes;
 12using Elsa.Workflows.Exceptions;
 13using Elsa.Workflows.Memory;
 14using Elsa.Workflows.Models;
 15
 16namespace Elsa.Alterations.Activities;
 17
 18/// <summary>
 19/// Submits an alteration plan for execution.
 20/// </summary>
 21[Browsable(false)]
 22[Activity("Elsa", "Alterations", "Generates jobs for the specified Alteration Plan", Kind = ActivityKind.Task)]
 23public class GenerateAlterationJobs : CodeActivity<int>
 24{
 25    /// <inheritdoc />
 026    public GenerateAlterationJobs([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(so
 27    {
 028    }
 29
 30    /// <inheritdoc />
 1931    public GenerateAlterationJobs(Variable<string> planId, [CallerFilePath] string? source = null, [CallerLineNumber] in
 32    {
 1933        PlanId = new Input<string>(planId);
 1934    }
 35
 36    /// <summary>
 37    /// The ID of the submitted alteration plan.
 38    /// </summary>
 6739    public Input<string> PlanId { get; set; } = null!;
 40
 41    /// <inheritdoc />
 42    protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
 43    {
 044        var plan = await GetPlanAsync(context);
 045        await UpdatePlanStatusAsync(context, plan);
 046        var workflowInstanceIds = (await FindMatchingWorkflowInstanceIdsAsync(context, plan.WorkflowInstanceFilter)).ToL
 47
 048        if (workflowInstanceIds.Any())
 049            await GenerateJobsAsync(context, plan, workflowInstanceIds);
 50
 051        context.SetResult(workflowInstanceIds.Count);
 052    }
 53
 54    private async Task<AlterationPlan> GetPlanAsync(ActivityExecutionContext context)
 55    {
 056        var cancellationToken = context.CancellationToken;
 057        var planId = context.Get(PlanId)!;
 058        var alterationPlanStore = context.GetRequiredService<IAlterationPlanStore>();
 059        var planFilter = new AlterationPlanFilter
 060        {
 061            Id = planId
 062        };
 063        var plan = await alterationPlanStore.FindAsync(planFilter, cancellationToken);
 64
 065        if (plan == null)
 066            throw new FaultException(AlterationFaultCodes.PlanNotFound, AlterationFaultCategories.Alteration, DefaultFau
 67
 068        return plan;
 069    }
 70
 71    private async Task UpdatePlanStatusAsync(ActivityExecutionContext context, AlterationPlan plan)
 72    {
 073        var cancellationToken = context.CancellationToken;
 074        var alterationPlanStore = context.GetRequiredService<IAlterationPlanStore>();
 075        plan.Status = AlterationPlanStatus.Generating;
 076        await alterationPlanStore.SaveAsync(plan, cancellationToken);
 077    }
 78
 79    private async Task<IEnumerable<string>> FindMatchingWorkflowInstanceIdsAsync(ActivityExecutionContext context, Alter
 80    {
 081        var cancellationToken = context.CancellationToken;
 082        var workflowInstanceFinder = context.GetRequiredService<IWorkflowInstanceFinder>();
 083        return await workflowInstanceFinder.FindAsync(filter, cancellationToken);
 084    }
 85
 86    private async Task GenerateJobsAsync(ActivityExecutionContext context, AlterationPlan plan, IEnumerable<string> work
 87    {
 088        var cancellationToken = context.CancellationToken;
 089        var identityGenerator = context.GetRequiredService<IIdentityGenerator>();
 090        var systemClock = context.GetRequiredService<ISystemClock>();
 091        var jobs = workflowInstanceIds.Select(workflowInstanceId => new AlterationJob
 092            {
 093                Id = identityGenerator.GenerateId(),
 094                PlanId = plan.Id,
 095                Status = AlterationJobStatus.Pending,
 096                WorkflowInstanceId = workflowInstanceId,
 097                CreatedAt = systemClock.UtcNow
 098            })
 099            .ToList();
 100
 0101        var alterationJobStore = context.GetRequiredService<IAlterationJobStore>();
 0102        await alterationJobStore.SaveManyAsync(jobs, cancellationToken);
 0103    }
 104}