async Task <ModelContext> CreateModel(IAsyncPipeContextAgent <ModelContext> asyncContext, CancellationToken cancellationToken) { IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext => { if (_log.IsDebugEnabled) { _log.DebugFormat("Creating model: {0}", connectionContext.Description); } try { var model = await connectionContext.CreateModel().ConfigureAwait(false); var modelContext = new RabbitMqModelContext(connectionContext, model, _host, cancellationToken); await asyncContext.Created(modelContext).ConfigureAwait(false); await asyncContext.Completed.ConfigureAwait(false); } catch (OperationCanceledException) { await asyncContext.CreateCanceled().ConfigureAwait(false); } catch (Exception exception) { await asyncContext.CreateFaulted(exception).ConfigureAwait(false); } }); _connectionCache.Send(connectionPipe, cancellationToken).ConfigureAwait(false); return(await asyncContext.Context.ConfigureAwait(false)); }
async Task SendUsingNewModel(IPipe <ModelContext> modelPipe, ModelScope scope, CancellationToken cancellationToken) { IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext => { IModel model = await connectionContext.CreateModel().ConfigureAwait(false); EventHandler <ShutdownEventArgs> modelShutdown = null; modelShutdown = (obj, reason) => { model.ModelShutdown -= modelShutdown; Interlocked.CompareExchange(ref _scope, null, scope); scope.Close(); }; model.ModelShutdown += modelShutdown; var modelContext = new RabbitMqModelContext(connectionContext, model, connectionContext.CancellationToken); scope.Connected(modelContext); try { using (SharedModelContext context = await scope.Attach(cancellationToken).ConfigureAwait(false)) { await modelPipe.Send(context).ConfigureAwait(false); } } catch (Exception ex) { if (_log.IsDebugEnabled) { _log.Debug("The existing model usage threw an exception", ex); } Interlocked.CompareExchange(ref _scope, null, scope); scope.Close(); throw; } }); try { await _connectionCache.Send(connectionPipe, new CancellationToken()).ConfigureAwait(false); } catch (Exception exception) { if (_log.IsDebugEnabled) { _log.Debug("The connection threw an exception", exception); } Interlocked.CompareExchange(ref _scope, null, scope); throw; } }
async Task SendUsingNewModel(IPipe <ModelContext> modelPipe, ModelScope scope, CancellationToken cancellationToken) { IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext => { try { if (_log.IsDebugEnabled) { _log.DebugFormat("Creating model: {0}", connectionContext.HostSettings.ToDebugString()); } var model = await connectionContext.CreateModel().ConfigureAwait(false); EventHandler <ShutdownEventArgs> modelShutdown = null; modelShutdown = (obj, reason) => { model.ModelShutdown -= modelShutdown; Interlocked.CompareExchange(ref _scope, null, scope); scope.Shutdown(reason.ReplyText); }; model.ModelShutdown += modelShutdown; var modelContext = new RabbitMqModelContext(connectionContext, model, _cacheTaskScope, _host); scope.Created(modelContext); } catch (Exception ex) { Interlocked.CompareExchange(ref _scope, null, scope); scope.CreateFaulted(ex); throw; } await SendUsingExistingModel(modelPipe, scope, cancellationToken).ConfigureAwait(false); }); try { await _host.ConnectionCache.Send(connectionPipe, _cacheTaskScope.StoppedToken).ConfigureAwait(false); } catch (Exception exception) { if (_log.IsDebugEnabled) { _log.Debug("The connection threw an exception", exception); } Interlocked.CompareExchange(ref _scope, null, scope); scope.CreateFaulted(exception); throw; } }
async Task SendUsingNewModel(IPipe<ModelContext> modelPipe, ModelScope scope, CancellationToken cancellationToken) { IPipe<ConnectionContext> connectionPipe = Pipe.ExecuteAsync<ConnectionContext>(async connectionContext => { IModel model = await connectionContext.CreateModel().ConfigureAwait(false); EventHandler<ShutdownEventArgs> modelShutdown = null; modelShutdown = (obj, reason) => { model.ModelShutdown -= modelShutdown; Interlocked.CompareExchange(ref _scope, null, scope); scope.Close(); }; model.ModelShutdown += modelShutdown; var modelContext = new RabbitMqModelContext(connectionContext, model, connectionContext.CancellationToken); scope.Connected(modelContext); try { using (SharedModelContext context = await scope.Attach(cancellationToken).ConfigureAwait(false)) { await modelPipe.Send(context).ConfigureAwait(false); } } catch (Exception ex) { if (_log.IsDebugEnabled) _log.Debug("The existing model usage threw an exception", ex); Interlocked.CompareExchange(ref _scope, null, scope); scope.Close(); throw; } }); try { await _connectionCache.Send(connectionPipe, new CancellationToken()).ConfigureAwait(false); } catch (Exception exception) { if (_log.IsDebugEnabled) _log.Debug("The connection threw an exception", exception); Interlocked.CompareExchange(ref _scope, null, scope); throw; } }
async Task IFilter <ConnectionContext> .Send(ConnectionContext context, IPipe <ConnectionContext> next) { IModel model = await context.CreateModel(); using (var modelContext = new RabbitMqModelContext(context, model, context.CancellationToken)) { await _pipe.Send(modelContext); } await next.Send(context); }
async Task IFilter <ConnectionContext> .Send(ConnectionContext context, IPipe <ConnectionContext> next) { using (var scope = _supervisor.CreateScope($"{TypeMetadataCache<ReceiveModelFilter>.ShortName}")) { var model = await context.CreateModel().ConfigureAwait(false); using (var modelContext = new RabbitMqModelContext(context, model, scope, _host)) { await _pipe.Send(modelContext).ConfigureAwait(false); } await scope.Completed.ConfigureAwait(false); await next.Send(context).ConfigureAwait(false); } }
async Task IFilter <ConnectionContext> .Send(ConnectionContext context, IPipe <ConnectionContext> next) { var model = await context.CreateModel().ConfigureAwait(false); var modelContext = new RabbitMqModelContext(context, model, _host, context.CancellationToken); try { await _pipe.Send(modelContext).ConfigureAwait(false); if (_log.IsDebugEnabled) { _log.DebugFormat("Consumer model pipe completed."); } } finally { await modelContext.DisposeAsync(CancellationToken.None).ConfigureAwait(false); } await next.Send(context).ConfigureAwait(false); }
public void Connected(RabbitMqModelContext connectionContext) { _modelContext.TrySetResult(connectionContext); }
public void Created(RabbitMqModelContext connectionContext) { _modelContext.TrySetResult(connectionContext); _taskScope.SetReady(); }
async Task SendUsingNewModel(IPipe<ModelContext> modelPipe, ModelScope scope, CancellationToken cancellationToken) { IPipe<ConnectionContext> connectionPipe = Pipe.ExecuteAsync<ConnectionContext>(async connectionContext => { try { if (_log.IsDebugEnabled) _log.DebugFormat("Creating model: {0}", connectionContext.HostSettings.ToDebugString()); var model = await connectionContext.CreateModel().ConfigureAwait(false); EventHandler<ShutdownEventArgs> modelShutdown = null; modelShutdown = (obj, reason) => { model.ModelShutdown -= modelShutdown; Interlocked.CompareExchange(ref _scope, null, scope); scope.Shutdown(reason.ReplyText); }; model.ModelShutdown += modelShutdown; var modelContext = new RabbitMqModelContext(connectionContext, model, _cacheTaskScope, _modelSettings); scope.Created(modelContext); } catch (Exception ex) { Interlocked.CompareExchange(ref _scope, null, scope); scope.CreateFaulted(ex); throw; } await SendUsingExistingModel(modelPipe, scope, cancellationToken).ConfigureAwait(false); }); try { await _connectionCache.Send(connectionPipe, _cacheTaskScope.StoppedToken).ConfigureAwait(false); } catch (Exception exception) { if (_log.IsDebugEnabled) _log.Debug("The connection threw an exception", exception); Interlocked.CompareExchange(ref _scope, null, scope); scope.CreateFaulted(exception); throw; } }