public async Task SerializeAsync()
        {
            await _log.WriteInfoAsync(nameof(SerializeAsync), "", "Gettings state...");

            var state = _stateHolder.GetState();

            await _log.WriteInfoAsync(nameof(SerializeAsync), _stateHolder.DescribeState(state), "Saving state...");

            await _repository.SaveAsync(state);

            await _log.WriteInfoAsync(nameof(SerializeAsync), "", "State saved");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Commits events to event store and event bus
        /// </summary>
        /// <param name="id">aggregate ID</param>
        /// <param name="aggregateRoot">aggregate</param>
        /// <param name="metadata">metadata</param>
        /// <returns>async task boid result</returns>
        public async Task CommitEventsAsync(string id, IAggregateRoot aggregateRoot, ICommandMetadata metadata)
        {
            var events  = ExtractEvents(aggregateRoot, metadata).ToList();
            var version = aggregateRoot.Version;

            events.ForEach(e => e.Version = version);
            await _eventSource.AppendEventsAsync(id, version, events).ConfigureAwait(false);

            _eventBus?.Publish(events);

            if (aggregateRoot.Version % SnapshotsInterval == 0)
            {
                await _snapshots.SaveAsync(new Snapshot(id, version, _serializer.Serialize(aggregateRoot.State))).ConfigureAwait(false);
            }
        }
Ejemplo n.º 3
0
        public async Task CreateSnapshotAsync(
            DateTime from,
            DateTime to)
        {
            _log.Info($"Creating balance snapshot for {from:s} - {to:s}...");

            var snapshotBalances = (await _balanceUpdateRepository
                                    .GetAsync(from, to))
                                   .OrderBy(x => x.EventTimestamp)
                                   .GroupBy(x => x.WalletId)
                                   .ToDictionary(x => x.Key, x => x.Select(y => y.NewBalance).Last());

            _log.Info($"{snapshotBalances.Count} balance updates found");

            var previousSnapshot = await _snapshotRepository.TryGetAsync(from);

            if (previousSnapshot != null)
            {
                _log.Info($"Previous snapshot found with {previousSnapshot.Balances.Count} balances");

                foreach (var balance in previousSnapshot.Balances)
                {
                    if (!snapshotBalances.ContainsKey(balance.WalletId))
                    {
                        snapshotBalances[balance.WalletId] = balance.Value;
                    }
                }
            }

            var newSnapshot = SnapshotAggregate.CreateOrRestore
                              (
                balances: snapshotBalances.Select(x => SnapshotAggregate.Balance.CreateOrRestore
                                                  (
                                                      balance: x.Value,
                                                      walletId: x.Key
                                                  )),
                timestamp: to
                              );

            await _snapshotRepository.SaveAsync(newSnapshot);
        }
        private static async Task SerializeAsync <TState>(ILog log, IHaveState <TState> stateHolder, ISnapshotRepository <TState> repository)
        {
            log.Info(nameof(SerializeAsync), "Gettings state...");

            TState state;

            try
            {
                state = stateHolder.GetState();
            }
            catch (NotSupportedException)
            {
                log.Warning(nameof(SerializeAsync), "Not supported, skipping");
                return;
            }

            log.Info(nameof(SerializeAsync), "Saving state...", stateHolder.DescribeState(state));

            await repository.SaveAsync(state);

            log.Info(nameof(SerializeAsync), "State saved");
        }