Esempio n. 1
0
        public void Run(CancellationToken token)
        {
            var connectFailure = 0;

            while (!token.IsCancellationRequested)
            {
                try {
                    var localStoreIsEmpty = _store.GetCurrentVersion() == 0;
                    if (localStoreIsEmpty)
                    {
                        token.WaitHandle.WaitOne(TimeSpan.FromSeconds(30));
                        continue;
                    }

                    _log.Information("Starting ES replication to {stream}", _streamName);

                    using (var conn = new CloudClient(_config.Host, _config.Login, _config.Password)) {
                        connectFailure = 0;

                        var lastReplicatedEvent = _checkpoint.GetOrInitPosition();

                        while (!token.IsCancellationRequested)
                        {
                            if (lastReplicatedEvent == _store.GetCurrentVersion())
                            {
                                // no work to do, so sleep and continue
                                token.WaitHandle.WaitOne(500);
                                continue;
                            }

                            var keys         = _store.ReadRecords(lastReplicatedEvent, 1000).ToList();
                            var remoteEvents = keys.Select(MessageToWrite).ToList();
                            conn.PostMessagesAsync(_streamName, remoteEvents).Wait(token);

                            lastReplicatedEvent = keys.Last().StoreVersion;
                            _checkpoint.Update(lastReplicatedEvent);
                        }
                    }
                }
                catch (Exception ex) {
                    if (connectFailure == 0)
                    {
                        _log.Error(ex, "Write connection failure");
                    }
                    connectFailure += 1;
                    token.WaitHandle.WaitOne(TimeSpan.FromMinutes(1));
                }
            }
        }
 public long GetVersion()
 {
     return(_appendOnlyStore.GetCurrentVersion());
 }