Beispiel #1
0
        public async Task EnqueueAsync(Rule rule, DomainId ruleId, Envelope <IEvent> @event)
        {
            Guard.NotNull(rule, nameof(rule));
            Guard.NotNull(@event, nameof(@event));

            var jobs = await ruleService.CreateJobsAsync(rule, ruleId, @event);

            foreach (var(job, ex) in jobs)
            {
                await ruleEventRepository.EnqueueAsync(job, ex);
            }
        }
Beispiel #2
0
        public async Task Should_update_repository_when_enqueing()
        {
            var @event = Envelope.Create <IEvent>(new ContentCreated {
                AppId = appId
            });

            var rule = CreateRule();

            var job = new RuleJob {
                Created = now
            };

            A.CallTo(() => ruleService.CreateJobsAsync(rule.RuleDef, rule.Id, @event, true))
            .Returns(new List <(RuleJob, Exception?)> {
                (job, null)
            });

            await sut.EnqueueAsync(rule.RuleDef, rule.Id, @event);

            A.CallTo(() => ruleEventRepository.EnqueueAsync(job, (Exception?)null))
            .MustHaveHappened();
        }
        public async Task <List <SimulatedRuleEvent> > SimulateAsync(NamedId <DomainId> appId, DomainId ruleId, Rule rule,
                                                                     CancellationToken ct = default)
        {
            Guard.NotNull(rule);

            var context = new RuleContext
            {
                AppId          = appId,
                Rule           = rule,
                RuleId         = ruleId,
                IncludeSkipped = true,
                IncludeStale   = true
            };

            var simulatedEvents = new List <SimulatedRuleEvent>(MaxSimulatedEvents);

            var fromNow = SystemClock.Instance.GetCurrentInstant().Minus(Duration.FromDays(7));

            await foreach (var storedEvent in eventStore.QueryAllReverseAsync($"^([a-zA-Z0-9]+)\\-{appId.Id}", fromNow, MaxSimulatedEvents, ct))
            {
                var @event = eventDataFormatter.ParseIfKnown(storedEvent);

                if (@event?.Payload is AppEvent appEvent)
                {
                    // Also create jobs for rules with failing conditions because we want to show them in th table.
                    await foreach (var result in ruleService.CreateJobsAsync(@event, context, ct))
                    {
                        var eventName = result.Job?.EventName;

                        if (string.IsNullOrWhiteSpace(eventName))
                        {
                            eventName = ruleService.GetName(appEvent);
                        }

                        simulatedEvents.Add(new SimulatedRuleEvent
                        {
                            ActionData    = result.Job?.ActionData,
                            ActionName    = result.Job?.ActionName,
                            EnrichedEvent = result.EnrichedEvent,
                            Error         = result.EnrichmentError?.Message,
                            Event         = @event.Payload,
                            EventId       = @event.Headers.EventId(),
                            EventName     = eventName,
                            SkipReason    = result.SkipReason
                        });
                    }
                }
            }

            return(simulatedEvents);
        }
Beispiel #4
0
        private async Task EnqueueFromEventsAsync(State currentState, RuleContext context,
                                                  CancellationToken ct)
        {
            var errors = 0;

            var filter = $"^([a-z]+)\\-{Key}";

            await foreach (var storedEvent in eventStore.QueryAllAsync(filter, currentState.Position, ct: ct))
            {
                try
                {
                    var @event = eventDataFormatter.ParseIfKnown(storedEvent);

                    if (@event != null)
                    {
                        var jobs = ruleService.CreateJobsAsync(@event, context, ct);

                        await foreach (var job in jobs.WithCancellation(ct))
                        {
                            if (job.Job != null && job.SkipReason == SkipReason.None)
                            {
                                await ruleEventRepository.EnqueueAsync(job.Job, job.EnrichmentError, ct);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    errors++;

                    if (errors >= MaxErrors)
                    {
                        throw;
                    }

                    log.LogWarning(ex, w => w
                                   .WriteProperty("action", "runRule")
                                   .WriteProperty("status", "failedPartially"));
                }
                finally
                {
                    currentState.Position = storedEvent.EventPosition;
                }

                await state.WriteAsync();
            }
        }
Beispiel #5
0
        private async Task EnqueueFromEventsAsync(State currentState, IRuleEntity rule, CancellationToken ct)
        {
            var errors = 0;

            await eventStore.QueryAsync(async storedEvent =>
            {
                try
                {
                    var @event = eventDataFormatter.ParseIfKnown(storedEvent);

                    if (@event != null)
                    {
                        var jobs = await ruleService.CreateJobsAsync(rule.RuleDef, rule.Id, @event, false);

                        foreach (var(job, ex) in jobs)
                        {
                            await ruleEventRepository.EnqueueAsync(job, ex);
                        }
                    }
                }
                catch (Exception ex)
                {
                    errors++;

                    if (errors >= MaxErrors)
                    {
                        throw;
                    }

                    log.LogWarning(ex, w => w
                                   .WriteProperty("action", "runRule")
                                   .WriteProperty("status", "failedPartially"));
                }
                finally
                {
                    currentState.Position = storedEvent.EventPosition;
                }

                await state.WriteAsync();
            }, $"^([a-z]+)\\-{Key}", currentState.Position, ct);
        }
        public async Task <List <SimulatedRuleEvent> > SimulateAsync(IRuleEntity rule, CancellationToken ct)
        {
            Guard.NotNull(rule, nameof(rule));

            var context = GetContext(rule);

            var result = new List <SimulatedRuleEvent>(MaxSimulatedEvents);

            var fromNow = SystemClock.Instance.GetCurrentInstant().Minus(Duration.FromDays(7));

            await foreach (var storedEvent in eventStore.QueryAllReverseAsync($"^([a-z]+)\\-{rule.AppId.Id}", fromNow, MaxSimulatedEvents, ct))
            {
                var @event = eventDataFormatter.ParseIfKnown(storedEvent);

                if (@event?.Payload is AppEvent appEvent)
                {
                    await foreach (var(job, exception, skip) in ruleService.CreateJobsAsync(@event, context, ct))
                    {
                        var name = job?.EventName;

                        if (string.IsNullOrWhiteSpace(name))
                        {
                            name = ruleService.GetName(appEvent);
                        }

                        var simulationResult = new SimulatedRuleEvent(
                            name,
                            job?.ActionName,
                            job?.ActionData,
                            exception?.Message,
                            skip);

                        result.Add(simulationResult);
                    }
                }
            }

            return(result);
        }