示例#1
0
        public async Task Should_not_create_job_if_too_old()
        {
            var @event = new ContentCreated {
                SchemaId = NamedId.Of(Guid.NewGuid(), "my-schema"), AppId = NamedId.Of(Guid.NewGuid(), "my-event")
            };

            var now = SystemClock.Instance.GetCurrentInstant();

            var ruleConfig   = ValidRule();
            var ruleEnvelope = Envelope.Create(@event);

            ruleEnvelope.SetTimestamp(now.Minus(Duration.FromDays(3)));

            A.CallTo(() => clock.GetCurrentInstant())
            .Returns(now);

            A.CallTo(() => ruleTriggerHandler.Triggers(A <Envelope <AppEvent> > .Ignored, ruleConfig.Trigger))
            .Returns(true);

            A.CallTo(() => ruleActionHandler.CreateJobAsync(A <EnrichedEvent> .Ignored, ruleConfig.Action))
            .Returns((actionDescription, actionData));

            var job = await sut.CreateJobAsync(ruleConfig, ruleEnvelope);

            Assert.Null(job);
        }
示例#2
0
        public async Task Should_not_create_job_if_too_old()
        {
            var e = new ContentCreated {
                SchemaId = new NamedId <Guid>(Guid.NewGuid(), "my-schema"), AppId = new NamedId <Guid>(Guid.NewGuid(), "my-event")
            };

            var now = SystemClock.Instance.GetCurrentInstant();

            var ruleConfig   = new Rule(new ContentChangedTrigger(), new WebhookAction());
            var ruleEnvelope = Envelope.Create(e);

            ruleEnvelope.SetTimestamp(now.Minus(Duration.FromDays(3)));

            var actionData        = new JObject();
            var actionDescription = "MyDescription";

            var eventName = "MySchemaCreatedEvent";

            A.CallTo(() => clock.GetCurrentInstant())
            .Returns(now);

            A.CallTo(() => ruleTriggerHandler.Triggers(A <Envelope <AppEvent> > .Ignored, ruleConfig.Trigger))
            .Returns(true);

            A.CallTo(() => ruleActionHandler.CreateJobAsync(A <Envelope <AppEvent> > .Ignored, eventName, ruleConfig.Action))
            .Returns((actionDescription, actionData));

            var job = await sut.CreateJobAsync(ruleConfig, ruleEnvelope);

            Assert.Null(job);
        }
示例#3
0
        public async Task Should_create_job_if_triggered()
        {
            var now = clock.GetCurrentInstant();

            var rule = ValidRule();

            var enrichedEvent = new EnrichedContentEvent {
                AppId = appId
            };

            var @event = Envelope.Create(new ContentCreated()).SetTimestamp(now);

            A.CallTo(() => ruleTriggerHandler.Trigger(@event.Payload, rule.Trigger, ruleId))
            .Returns(true);

            A.CallTo(() => ruleTriggerHandler.Trigger(enrichedEvent, rule.Trigger))
            .Returns(true);

            A.CallTo(() => ruleTriggerHandler.CreateEnrichedEventAsync(A <Envelope <AppEvent> > .That.Matches(x => x.Payload == @event.Payload)))
            .Returns(enrichedEvent);

            A.CallTo(() => ruleActionHandler.CreateJobAsync(A <EnrichedEvent> .Ignored, rule.Action))
            .Returns((actionDescription, new ValidData {
                Value = 10
            }));

            var job = await sut.CreateJobAsync(rule, ruleId, @event);

            Assert.Equal(actionData, job.ActionData);
            Assert.Equal(actionName, job.ActionName);
            Assert.Equal(actionDescription, job.Description);

            Assert.Equal(now, job.Created);
            Assert.Equal(now.Plus(Duration.FromDays(2)), job.Expires);

            Assert.Equal(enrichedEvent.AppId.Id, job.AppId);

            Assert.NotEqual(Guid.Empty, job.JobId);

            A.CallTo(() => eventEnricher.EnrichAsync(enrichedEvent, A <Envelope <AppEvent> > .That.Matches(x => x.Payload == @event.Payload)))
            .MustHaveHappened();
        }
示例#4
0
        private async Task <JobResult> CreateJobAsync(IRuleActionHandler actionHandler, EnrichedEvent enrichedEvent, RuleContext context, Instant now)
        {
            var actionName = typeNameRegistry.GetName(context.Rule.Action.GetType());

            var expires = now.Plus(Constants.ExpirationTime);

            var job = new RuleJob
            {
                Id                 = DomainId.NewGuid(),
                ActionData         = string.Empty,
                ActionName         = actionName,
                AppId              = enrichedEvent.AppId.Id,
                Created            = now,
                EventName          = enrichedEvent.Name,
                ExecutionPartition = enrichedEvent.Partition,
                Expires            = expires,
                RuleId             = context.RuleId
            };

            try
            {
                var(description, data) = await actionHandler.CreateJobAsync(enrichedEvent, context.Rule.Action);

                var json = jsonSerializer.Serialize(data);

                job.ActionData  = json;
                job.ActionName  = actionName;
                job.Description = description;

                return(new JobResult {
                    Job = job, EnrichedEvent = enrichedEvent
                });
            }
            catch (Exception ex)
            {
                job.Description = "Failed to create job";

                return(JobResult.Failed(ex, enrichedEvent, job));
            }
        }
示例#5
0
        private async Task <(RuleJob, Exception?)> CreateJobAsync(Rule rule, DomainId ruleId, IRuleActionHandler actionHandler, Instant now, EnrichedEvent enrichedEvent)
        {
            var actionName = typeNameRegistry.GetName(rule.Action.GetType());

            var expires = now.Plus(Constants.ExpirationTime);

            var job = new RuleJob
            {
                Id                 = DomainId.NewGuid(),
                ActionData         = string.Empty,
                ActionName         = actionName,
                AppId              = enrichedEvent.AppId.Id,
                Created            = now,
                EventName          = enrichedEvent.Name,
                ExecutionPartition = enrichedEvent.Partition,
                Expires            = expires,
                RuleId             = ruleId
            };

            try
            {
                var(description, data) = await actionHandler.CreateJobAsync(enrichedEvent, rule.Action);

                var json = jsonSerializer.Serialize(data);

                job.ActionData  = json;
                job.ActionName  = actionName;
                job.Description = description;

                return(job, null);
            }
            catch (Exception ex)
            {
                job.Description = "Failed to create job";

                return(job, ex);
            }
        }