private async void Run(CancellationToken token, Func <IEvent, Task> dispatcher, Action <Exception> onError, Action onCompleted) { var current = _checkpointWriter.GetOrInitPosition(); var reader = await _client.GetMessageReaderAsync(_stream); Task.Factory.StartNew(() => FetchMessages(token, dispatcher, onError, onCompleted, reader, current) , TaskCreationOptions.LongRunning); }
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 void Init() { _pages.Init(); _position = _positionWriter.GetOrInitPosition(); //_log.Verbose("Stream {stream} at {offset}", _streamName, _position); var tail = Tail(_position); if (tail != 0) { // preload tail var offset = Floor(_position); //_log.Verbose("Load tail at {offset}", offset); var page = _pages.ReadPage(offset); _stream.Write(page, 0, tail); } }
public async void Run(CancellationToken ct) { var current = _checkpoint.GetOrInitPosition(); var reader = await _cloudClient.GetMessageReaderAsync("test"); while (!ct.IsCancellationRequested) { var result = await reader.GetMessagesAsync(ct, current, 100); if (result.HasMessages()) { foreach (var message in result.Messages) { Console.WriteLine("Got message! " + message.Id); } current = result.NextOffset; _checkpoint.Update(current); } } }
public void Init() { _targetWriter.Init(); TargetPos.GetOrInitPosition(); }