예제 #1
0
        public async Task Should_not_write_to_state_for_reservation()
        {
            await sut.ReserveAsync(id1.Id, id1.Name);

            A.CallTo(() => grainState.WriteAsync())
            .MustNotHaveHappened();
        }
예제 #2
0
        public async Task CheckUsagesAsync()
        {
            var today = DateTime.Today;

            foreach (var(key, target) in state.Value.Targets)
            {
                var from = GetFromDate(today, target.NumDays);

                if (target.Triggered == null || target.Triggered < from)
                {
                    var costs = await usageTracker.GetMonthCallsAsync(target.AppId.Id.ToString(), today, null);

                    var limit = target.Limits;

                    if (costs > limit)
                    {
                        target.Triggered = today;

                        var @event = new AppUsageExceeded
                        {
                            AppId        = target.AppId,
                            CallsCurrent = costs,
                            CallsLimit   = limit,
                            RuleId       = key
                        };

                        await state.WriteEventAsync(Envelope.Create <IEvent>(@event));
                    }
                }
            }

            await state.WriteAsync();
        }
예제 #3
0
        public Task RebuildAsync(TagsExport export)
        {
            state.Value.Tags  = export.Tags;
            state.Value.Alias = export.Alias;

            return(state.WriteAsync());
        }
예제 #4
0
        public async Task CheckUsagesAsync()
        {
            var today = DateTime.Today;

            foreach (var kvp in state.Value.Targets)
            {
                var target = kvp.Value;

                var from = GetFromDate(today, target.NumDays);

                if (!target.Triggered.HasValue || target.Triggered < from)
                {
                    var usage = await usageTracker.GetMonthlyCallsAsync(target.AppId.Id.ToString(), today);

                    var limit = kvp.Value.Limits;

                    if (usage > limit)
                    {
                        kvp.Value.Triggered = today;

                        var @event = new AppUsageExceeded
                        {
                            AppId        = target.AppId,
                            CallsCurrent = usage,
                            CallsLimit   = limit,
                            RuleId       = kvp.Key
                        };

                        await state.WriteEventAsync(Envelope.Create <IEvent>(@event));
                    }
                }
            }

            await state.WriteAsync();
        }
예제 #5
0
        public Task RebuildAsync(HashSet <T> ids)
        {
            state.Value = new TState {
                Ids = ids
            };

            return(state.WriteAsync());
        }
예제 #6
0
        public Task RebuildAsync(HashSet <Guid> apps)
        {
            state.Value = new GrainState {
                Apps = apps
            };

            return(state.WriteAsync());
        }
예제 #7
0
        public Task RebuildAsync(Dictionary <string, Guid> schemas)
        {
            state.Value = new GrainState {
                Schemas = schemas
            };

            return(state.WriteAsync());
        }
예제 #8
0
        public Task RebuildAsync(Dictionary <string, Guid> apps)
        {
            state.Value = new GrainState {
                Apps = apps
            };

            return(state.WriteAsync());
        }
예제 #9
0
        public Task RebuildAsync(HashSet <Guid> rules)
        {
            state.Value = new GrainState {
                Rules = rules
            };

            return(state.WriteAsync());
        }
예제 #10
0
        public async Task Should_increment_counters()
        {
            Assert.Equal(1, await sut.IncrementAsync("Counter1"));
            Assert.Equal(2, await sut.IncrementAsync("Counter1"));

            Assert.Equal(1, await sut.IncrementAsync("Counter2"));
            Assert.Equal(2, await sut.IncrementAsync("Counter2"));

            A.CallTo(() => grainState.WriteAsync())
            .MustHaveHappened(4, Times.Exactly);
        }
예제 #11
0
        private async Task RecoverAfterRestartAsync()
        {
            if (CurrentJob?.Status == JobStatus.Started)
            {
                Log("Failed due application restart");

                CurrentJob.Status = JobStatus.Failed;

                await state.WriteAsync();
            }
        }
예제 #12
0
        public async Task Should_add_app_id_to_index()
        {
            await sut.AddAppAsync(appId1.Id, appId1.Name);

            var result = await sut.GetAppIdAsync(appId1.Name);

            Assert.Equal(appId1.Id, result);

            A.CallTo(() => grainState.WriteAsync())
            .MustHaveHappened();
        }
예제 #13
0
        public async Task Should_add_schema_id_to_index()
        {
            await sut.AddSchemaAsync(schemaId1.Id, schemaId1.Name);

            var result = await sut.GetSchemaIdAsync(schemaId1.Name);

            Assert.Equal(schemaId1.Id, result);

            A.CallTo(() => grainState.WriteAsync())
            .MustHaveHappened();
        }
예제 #14
0
        private async Task RecoverAfterRestartAsync()
        {
            if (CurrentJob?.Status == JobStatus.Started)
            {
                var handlers = CreateHandlers();

                Log("Failed due application restart");

                CurrentJob.Status = JobStatus.Failed;

                await CleanupAsync(handlers);

                await state.WriteAsync();
            }
        }
예제 #15
0
        public async Task Should_add_app_id_to_index()
        {
            await sut.AddAppAsync(appId1);

            await sut.AddAppAsync(appId2);

            var result = await sut.GetAppIdsAsync();

            Assert.Equal(new List <Guid> {
                appId1, appId2
            }, result);

            A.CallTo(() => grainState.WriteAsync())
            .MustHaveHappenedTwiceExactly();
        }
예제 #16
0
        public async Task Should_add_id_to_index()
        {
            await sut.AddAsync(id1);

            await sut.AddAsync(id2);

            var result = await sut.GetIdsAsync();

            Assert.Equal(new List <DomainId> {
                id1, id2
            }, result);

            A.CallTo(() => grainState.WriteAsync())
            .MustHaveHappenedTwiceExactly();
        }
예제 #17
0
        public async Task RunAsync(DomainId ruleId)
        {
            if (currentJobToken != null)
            {
                throw new DomainException(T.Get("rules.ruleAlreadyRunning"));
            }

            state.Value = new State
            {
                RuleId = ruleId
            };

            EnsureIsRunning();

            await state.WriteAsync();
        }
예제 #18
0
        public async Task RunAsync(Guid ruleId)
        {
            if (currentTaskToken != null)
            {
                throw new DomainException("Another rule is already running.");
            }

            state.Value = new State
            {
                RuleId = ruleId
            };

            EnsureIsRunning();

            await state.WriteAsync();
        }
예제 #19
0
        private async Task DoAndUpdateStateAsync(Func <Task> action, [CallerMemberName] string caller = null)
        {
            try
            {
                await action();
            }
            catch (Exception ex)
            {
                try
                {
                    Unsubscribe();
                }
                catch (Exception unsubscribeException)
                {
                    ex = new AggregateException(ex, unsubscribeException);
                }

                log.LogFatal(ex, w => w
                             .WriteProperty("action", caller)
                             .WriteProperty("status", "Failed")
                             .WriteProperty("eventConsumer", eventConsumer.Name));

                state.Value = state.Value.Failed(ex);
            }

            await state.WriteAsync();
        }
예제 #20
0
        public async Task <long> ResetAsync(string name, long value)
        {
            state.Value.Counters[name] = value;

            await state.WriteAsync();

            return(value);
        }
예제 #21
0
        public async Task RunAsync(DomainId ruleId, bool fromSnapshots)
        {
            if (currentJobToken != null)
            {
                throw new DomainException(T.Get("rules.ruleAlreadyRunning"));
            }

            state.Value = new State
            {
                RuleId           = ruleId,
                RunFromSnapshots = fromSnapshots
            };

            await EnsureIsRunningAsync(false);

            await state.WriteAsync();
        }
예제 #22
0
        private async Task RecoverAfterRestartAsync()
        {
            foreach (var job in state.Value.Jobs)
            {
                if (!job.Stopped.HasValue)
                {
                    var jobId = job.Id.ToString();

                    job.Stopped = clock.GetCurrentInstant();

                    await Safe.DeleteAsync(backupArchiveLocation, jobId, log);

                    await Safe.DeleteAsync(assetStore, jobId, log);

                    job.Status = JobStatus.Failed;

                    await state.WriteAsync();
                }
            }
        }
        public async Task Should_set_setting()
        {
            await sut.SetAsync(JsonValue.Object().Add("key", 15).AsJ());

            var actual = await sut.GetAsync();

            var expected =
                JsonValue.Object().Add("key", 15);

            Assert.Equal(expected.ToString(), actual.Value.ToString());

            A.CallTo(() => grainState.WriteAsync())
            .MustHaveHappened();
        }
        public async Task Should_stop_subscription_when_stopped()
        {
            await sut.ActivateAsync(consumerName);

            await sut.ActivateAsync();

            await sut.StopAsync();

            await sut.StopAsync();

            grainState.Value.Should().BeEquivalentTo(new EventConsumerState {
                IsStopped = true, Position = initialPosition, Error = null
            });

            A.CallTo(() => grainState.WriteAsync())
            .MustHaveHappened(1, Times.Exactly);

            A.CallTo(() => eventSubscription.StopAsync())
            .MustHaveHappened(1, Times.Exactly);
        }
예제 #25
0
        private async Task RecoverAfterRestartAsync()
        {
            state.Value.Jobs.RemoveAll(x => x.Stopped == null);

            await state.WriteAsync();
        }
예제 #26
0
        private Task TrackNotifiedAsync(Guid appId, DateTime now)
        {
            state.Value.NotificationsSent[appId] = now;

            return(state.WriteAsync());
        }
예제 #27
0
        public async Task Should_stop_subscription_if_stopped()
        {
            await sut.ActivateAsync(consumerName);

            await sut.ActivateAsync();

            await sut.StopAsync();

            await sut.StopAsync();

            await sut.CompleteAsync();

            AssertGrainState(isStopped: true, position: initialPosition);

            A.CallTo(() => grainState.WriteAsync())
            .MustHaveHappenedOnceExactly();

            A.CallTo(() => eventSubscription.Unsubscribe())
            .MustHaveHappenedOnceExactly();
        }
예제 #28
0
        public Task SetAsync(J <JsonObject> settings)
        {
            state.Value.Settings = settings;

            return(state.WriteAsync());
        }
예제 #29
0
        public Task RebuildAsync(TagsExport tags)
        {
            state.Value.Tags = tags;

            return(state.WriteAsync());
        }
예제 #30
0
        public async Task Should_stop_subscription_when_stopped()
        {
            await sut.ActivateAsync(consumerName);

            await sut.ActivateAsync();

            await sut.StopAsync();

            await sut.StopAsync();

            await sut.CompleteAsync();

            AssetGrainState(new EventConsumerState {
                IsStopped = true, Position = initialPosition, Error = null
            });

            A.CallTo(() => grainState.WriteAsync())
            .MustHaveHappenedOnceExactly();

            A.CallTo(() => eventSubscription.Unsubscribe())
            .MustHaveHappenedOnceExactly();
        }