コード例 #1
0
        public async Task Should_update_repositories_with_jobs_from_service()
        {
            var @event = Envelope.Create <IEvent>(new ContentCreated {
                AppId = appId
            });

            var rule1 = CreateRule();
            var rule2 = CreateRule();

            var job1 = new RuleJob {
                Created = now
            };

            A.CallTo(() => appProvider.GetRulesAsync(appId.Id))
            .Returns(new List <IRuleEntity> {
                rule1, rule2
            });

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

            A.CallTo(() => ruleService.CreateJobsAsync(rule2.RuleDef, rule2.Id, @event, true))
            .Returns(new List <(RuleJob, Exception?)>());

            await sut.On(@event);

            A.CallTo(() => ruleEventRepository.EnqueueAsync(job1, now, default))
            .MustHaveHappened();
        }
コード例 #2
0
ファイル: RuleRunnerGrain.cs プロジェクト: wensincai/squidex
        private async Task ProcessAsync(State currentState, CancellationToken ct)
        {
            try
            {
                currentReminder = await RegisterOrUpdateReminder("KeepAlive", TimeSpan.Zero, TimeSpan.FromMinutes(2));

                var rules = await appProvider.GetRulesAsync(DomainId.Create(Key));

                var rule = rules.Find(x => x.Id == currentState.RuleId);

                if (rule == null)
                {
                    throw new InvalidOperationException("Cannot find rule.");
                }

                using (localCache.StartContext())
                {
                    if (currentState.FromSnapshots && ruleService.CanCreateSnapshotEvents(rule.RuleDef))
                    {
                        await EnqueueFromSnapshotsAsync(rule);
                    }
                    else
                    {
                        await EnqueueFromEventsAsync(currentState, rule, ct);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                return;
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "runRule")
                             .WriteProperty("status", "failed")
                             .WriteProperty("ruleId", currentState.RuleId?.ToString()));
            }
            finally
            {
                if (!isStopping)
                {
                    currentState.RuleId   = null;
                    currentState.Position = null;

                    await state.WriteAsync();

                    if (currentReminder != null)
                    {
                        await UnregisterReminder(currentReminder);

                        currentReminder = null;
                    }

                    currentJobToken?.Dispose();
                    currentJobToken = null;
                }
            }
        }
コード例 #3
0
        public async Task <IActionResult> GetRules(string app)
        {
            var rules = await appProvider.GetRulesAsync(AppId);

            var response = rules.Select(r => r.ToModel());

            return(Ok(response));
        }
コード例 #4
0
        public async Task <IActionResult> GetRules(string app)
        {
            var entities = await appProvider.GetRulesAsync(AppId);

            var response = entities.Select(RuleDto.FromRule);

            return(Ok(response));
        }
コード例 #5
0
        private Task <List <IRuleEntity> > GetRulesAsync(Guid appId)
        {
            return(cache.GetOrCreateAsync(appId, entry =>
            {
                entry.AbsoluteExpirationRelativeToNow = CacheDuration;

                return appProvider.GetRulesAsync(appId);
            }));
        }
コード例 #6
0
        public async Task <IActionResult> GetRules(string app)
        {
            var entities = await appProvider.GetRulesAsync(AppId);

            var response = entities.Select(RuleDto.FromRule).ToArray();

            Response.Headers[HeaderNames.ETag] = response.ToManyEtag(0);

            return(Ok(response));
        }
コード例 #7
0
        public async Task <IActionResult> GetRules(string app)
        {
            var rules = await appProvider.GetRulesAsync(AppId);

            var response = RulesDto.FromRules(rules, this, app);

            Response.Headers[HeaderNames.ETag] = response.GenerateEtag();

            return(Ok(response));
        }
コード例 #8
0
ファイル: RuleEnqueuer.cs プロジェクト: wensincai/squidex
        private Task <List <IRuleEntity> > GetRulesAsync(DomainId appId)
        {
            var cacheKey = $"{typeof(RuleEnqueuer)}_Rules_{appId}";

            return(cache.GetOrCreateAsync(cacheKey, entry =>
            {
                entry.AbsoluteExpirationRelativeToNow = CacheDuration;

                return appProvider.GetRulesAsync(appId);
            }));
        }
コード例 #9
0
        public async Task Should_update_repositories_on_with_jobs_from_sender()
        {
            var @event = Envelope.Create(new ContentCreated {
                AppId = appId
            });

            var rule1 = new Rule(new ContentChangedTrigger(), new TestAction {
                Url = new Uri("https://squidex.io")
            });
            var rule2 = new Rule(new ContentChangedTrigger(), new TestAction {
                Url = new Uri("https://squidex.io")
            });
            var rule3 = new Rule(new ContentChangedTrigger(), new TestAction {
                Url = new Uri("https://squidex.io")
            });

            var job1 = new RuleJob {
                Created = now
            };
            var job2 = new RuleJob {
                Created = now
            };

            var ruleEntity1 = A.Fake <IRuleEntity>();
            var ruleEntity2 = A.Fake <IRuleEntity>();
            var ruleEntity3 = A.Fake <IRuleEntity>();

            A.CallTo(() => ruleEntity1.RuleDef).Returns(rule1);
            A.CallTo(() => ruleEntity2.RuleDef).Returns(rule2);
            A.CallTo(() => ruleEntity3.RuleDef).Returns(rule3);

            A.CallTo(() => appProvider.GetRulesAsync(appId.Id))
            .Returns(new List <IRuleEntity> {
                ruleEntity1, ruleEntity2, ruleEntity3
            });

            A.CallTo(() => ruleService.CreateJobAsync(rule1, @event))
            .Returns(job1);

            A.CallTo(() => ruleService.CreateJobAsync(rule2, @event))
            .Returns(job2);

            A.CallTo(() => ruleService.CreateJobAsync(rule3, @event))
            .Returns(Task.FromResult <RuleJob>(null));

            await sut.On(@event);

            A.CallTo(() => ruleEventRepository.EnqueueAsync(job1, now))
            .MustHaveHappened();

            A.CallTo(() => ruleEventRepository.EnqueueAsync(job2, now))
            .MustHaveHappened();
        }
コード例 #10
0
ファイル: RulesController.cs プロジェクト: yjpark/squidex
        public async Task <IActionResult> GetRules(string app)
        {
            var rules = await appProvider.GetRulesAsync(AppId);

            var response = Deferred.Response(() =>
            {
                return(RulesDto.FromRules(rules, this, app));
            });

            Response.Headers[HeaderNames.ETag] = rules.ToEtag();

            return(Ok(response));
        }
コード例 #11
0
        public async Task On(Envelope <IEvent> @event)
        {
            if (@event.Payload is AppEvent appEvent)
            {
                var rules = await appProvider.GetRulesAsync(appEvent.AppId.Id);

                foreach (var ruleEntity in rules)
                {
                    var job = ruleService.CreateJob(ruleEntity.RuleDef, @event);

                    if (job != null)
                    {
                        await ruleEventRepository.EnqueueAsync(job, job.Created);
                    }
                }
            }
        }
コード例 #12
0
        private void SetupRules(Envelope <IEvent> @event, RuleJob job1)
        {
            var rule1 = CreateRule();
            var rule2 = CreateRule();

            A.CallTo(() => appProvider.GetRulesAsync(appId.Id))
            .Returns(new List <IRuleEntity> {
                rule1, rule2
            });

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

            A.CallTo(() => ruleService.CreateJobsAsync(rule2.RuleDef, rule2.Id, @event, true))
            .Returns(new List <(RuleJob, Exception?)>());
        }
コード例 #13
0
        private async Task ProcessAsync(State job, CancellationToken ct)
        {
            try
            {
                currentReminder = await RegisterOrUpdateReminder("KeepAlive", TimeSpan.Zero, TimeSpan.FromMinutes(2));

                var rules = await appProvider.GetRulesAsync(Key);

                var rule = rules.Find(x => x.Id == job.RuleId);

                if (rule == null)
                {
                    throw new InvalidOperationException("Cannot find rule.");
                }

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

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

                    foreach (var job in jobs)
                    {
                        await ruleEventRepository.EnqueueAsync(job, job.Created, ct);
                    }

                    job.Position = storedEvent.EventPosition;

                    await state.WriteAsync();
                }, SquidexHeaders.AppId, Key.ToString(), job.Position, ct);
            }
            catch (OperationCanceledException)
            {
                return;
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "runeRule")
                             .WriteProperty("status", "failed")
                             .WriteProperty("ruleId", job.RuleId?.ToString()));
            }
            finally
            {
                if (!isStopping)
                {
                    job.RuleId   = null;
                    job.Position = null;

                    await state.WriteAsync();

                    if (currentReminder != null)
                    {
                        await UnregisterReminder(currentReminder);

                        currentReminder = null;
                    }

                    currentTaskToken = null;
                }
            }
        }