protected override async Task ExecuteAsync(CancellationToken stoppingToken) { using var scope = _factory.CreateScope(); _logger.LogInformation($"Worker {_options.Value.Name} is starting"); #region Initialize Hostname and Box var process = new Process { StartInfo = new ProcessStartInfo { FileName = "/bin/hostname", Arguments = "-i", RedirectStandardOutput = true } }; process.Start(); await process.WaitForExitAsync(); if (process.ExitCode != 0) { throw new Exception($"E: Cannot obtain hostname, exit code {process.ExitCode}."); } var hostname = process.StandardOutput.ReadToEnd().Trim(); var boxId = hostname.Split('.').ToList().Last(); _options.Value.Name = _options.Value.Name + '-' + boxId; Box.InitBoxAsync(boxId); // Use ip obtained by hostname as the unique ID of worker _logger.LogInformation($"Worker works at {hostname}, renaming to {_options.Value.Name}"); #endregion #region Initialize RabbitMq var factory = new RabbitMqConnectionFactory(scope.ServiceProvider); var requestConsumer = scope.ServiceProvider.GetRequiredService <JobRequestConsumer>(); var completeProducer = scope.ServiceProvider.GetRequiredService <JobCompleteProducer>(); var heartbeatProducer = scope.ServiceProvider.GetRequiredService <WorkerHeartbeatProducer>(); var connection = factory.GetConnection(); if (!stoppingToken.IsCancellationRequested) // Application may be aborted in GetConnection(). { heartbeatProducer.Start(connection); completeProducer.Start(connection); requestConsumer.Start(connection); } #endregion while (!stoppingToken.IsCancellationRequested) { await Task.Delay(1000, stoppingToken); } _logger.LogInformation($"Worker {_options.Value.Name} is stopping"); requestConsumer.Stop(); completeProducer.Stop(); heartbeatProducer.Stop(); factory.CloseConnection(); }
static void ExecuteUnsuccessfulConnectionCreationAndAssertResults(RabbitMqClientOptions connectionOptions) { var connectionFactory = new RabbitMqConnectionFactory(); var exception = Assert.Throws <InitialConnectionException>(() => connectionFactory.CreateRabbitMqConnection(connectionOptions)); Assert.Equal(connectionOptions.InitialConnectionRetries, exception.NumberOfRetries); }
static void ExecuteSuccessfulConnectionCreationAndAssertResults(RabbitMqClientOptions connectionOptions) { var connectionFactory = new RabbitMqConnectionFactory(); using var connection = connectionFactory.CreateRabbitMqConnection(connectionOptions); Assert.True(connection.IsOpen); }
public List <string> TestGet() { var messageList = new List <string>(); var channel = RabbitMqConnectionFactory.GetChannelPerThreadId(Thread.CurrentThread.ManagedThreadId); var queueDeclareResponse = channel.QueueDeclare(TopicName, true, false, false, null); var consumer = new QueueingBasicConsumer(channel); try { channel.BasicConsume(TopicName, true, consumer); Console.WriteLine(" [*] Processing existing messages."); for (int i = 0; i < queueDeclareResponse.MessageCount; i++) { var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue(); var body = ea.Body; var message = Encoding.UTF8.GetString(body); messageList.Add(message); Console.WriteLine(" [x] Received {0}", message); } } catch (Exception) { var response = channel.BasicGet(TopicName, false); channel.BasicNack(response.DeliveryTag, true, true); throw; } return(messageList); }
public PersistentConnection(RabbitMqConnectionFactory connectionFactory, TimeSpan retryDelay, string connectionName) { this.connectionFactory = connectionFactory; this.retryDelay = retryDelay; this.connectionName = connectionName; TryToConnect(null); }
public RabbitMQProvider(IServiceProvider serviceProvider, IRabbitMqQueueNameProvider queueNameProvider, RabbitMqConnectionFactory connectionFactory) { _serviceProvider = serviceProvider; _queueNameProvider = queueNameProvider; _rabbitMqConnectionFactory = connectionFactory; }
public static void RegisterEvents(this ContainerBuilder builder, Settings settings) { var connection = new RabbitMqConnectionFactory(settings.RabbitHost, settings.RabbitPort, settings.RabbitUserName, settings.RabbitPassword).CreateConnection(); var dispatcher = new RabbitMqDispatcherFactory(connection, settings.RabbitExchangeName, CustomLogger.Logger).CreateDispatcher(); builder.RegisterInstance <IConnection>(connection); builder.RegisterInstance <IDispatchCommits>(dispatcher); }
static RabbitMqConnectionManager SetupRabbitMqConnectionManager(string connectionString) { var config = new ConnectionStringParser(new SettingsHolder()).Parse(connectionString); // config.OverrideClientProperties(); var connectionFactory = new RabbitMqConnectionFactory(config); var newConnectionManager = new RabbitMqConnectionManager(connectionFactory, config); return(newConnectionManager); }
public void SendMessage(string message) { var channel = RabbitMqConnectionFactory.GetChannelPerThreadId(Thread.CurrentThread.ManagedThreadId); channel.QueueDeclare(TopicName, true, false, false, null); IBasicProperties basicProperties = channel.CreateBasicProperties(); basicProperties.DeliveryMode = 2; byte[] messageBytes = Encoding.UTF8.GetBytes(message); channel.BasicPublish("", TopicName, basicProperties, messageBytes); }
public void SetUp() { routingTopology = new ConventionalRoutingTopology(true); receivedMessages = new BlockingCollection <TransportMessage>(); var config = new ConnectionConfiguration(); config.ParseHosts("localhost:5672"); var connectionFactory = new RabbitMqConnectionFactory(config); connectionManager = new RabbitMqConnectionManager(connectionFactory, config); publishChannel = connectionManager.GetPublishConnection().CreateModel(); var channelProvider = new FakeChannelProvider(publishChannel); sender = new RabbitMqMessageSender(routingTopology, channelProvider, new IncomingContext(null, null)); dequeueStrategy = new RabbitMqDequeueStrategy(connectionManager, new RepeatedFailuresOverTimeCircuitBreaker("UnitTest", TimeSpan.FromMinutes(2), e => {}), new ReceiveOptions(s => SecondaryReceiveSettings.Enabled(CallbackQueue, 1), new MessageConverter(), 1, 1000, false, "Unit test")); MakeSureQueueAndExchangeExists(ReceiverQueue); MessagePublisher = new RabbitMqMessagePublisher { ChannelProvider = channelProvider, RoutingTopology = routingTopology }; subscriptionManager = new RabbitMqSubscriptionManager { ConnectionManager = connectionManager, EndpointQueueName = ReceiverQueue, RoutingTopology = routingTopology }; dequeueStrategy.Init(Address.Parse(ReceiverQueue), new TransactionSettings(true, TimeSpan.FromSeconds(30), IsolationLevel.ReadCommitted, 5, false, false), m => { receivedMessages.Add(m); return(true); }, (s, exception) => { }); dequeueStrategy.Start(MaximumConcurrency); }
public static WorkflowOptions UseRabbitMQ(this WorkflowOptions options, RabbitMqConnectionFactory rabbitMqConnectionFactory) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (rabbitMqConnectionFactory == null) { throw new ArgumentNullException(nameof(rabbitMqConnectionFactory)); } options.Services.AddSingleton(rabbitMqConnectionFactory); options.Services.TryAddSingleton <IRabbitMqQueueNameProvider, DefaultRabbitMqQueueNameProvider>(); options.UseQueueProvider(RabbitMqQueueProviderFactory); return(options); }
public void GetMessage() { var channel = RabbitMqConnectionFactory.GetChannelPerThreadId(Thread.CurrentThread.ManagedThreadId); channel.BasicQos(0, 1, false); var consumer = new QueueingBasicConsumer(channel); channel.BasicConsume(TopicName, false, consumer); while (true) { //Get next message var deliveryArgs = (BasicDeliverEventArgs)consumer.Queue.Dequeue(); //Serialize message var message = Encoding.Default.GetString(deliveryArgs.Body); Console.WriteLine("Message Received from RabbitMQ - {0}", message); channel.BasicAck(deliveryArgs.DeliveryTag, false); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider provider, IHostApplicationLifetime lifetime) { ConfigureDatabase(provider).Wait(); app.Use(async(ctx, next) => { ctx.SetIdentityServerOrigin(Configuration["Application:Host"]); await next(); }); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseMigrationsEndPoint(); // https://github.com/aspnet/Announcements/issues/432 } else { app.UseExceptionHandler("/Error"); // app.UseHsts(); } // app.UseHttpsRedirection(); app.UseStaticFiles(); // wwwroot if (!env.IsDevelopment()) { app.UseSpaStaticFiles(); } app.UseRouting(); app.UseCors("default"); app.UseAuthentication() .UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax }); app.UseIdentityServer(); app.UseAuthorization(); // Handle plagiarism report static files. // This should come after UseAuthorization() for auth. var options = provider.GetRequiredService <IOptions <ApplicationConfig> >(); var plagiarismsFolder = Path.Combine(options.Value.DataPath, "plagiarisms"); if (!Directory.Exists(plagiarismsFolder)) { Directory.CreateDirectory(plagiarismsFolder); } app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(plagiarismsFolder), RequestPath = "/plagiarisms", OnPrepareResponse = async(ctx) => { var authorized = ctx.Context.User.IsAuthenticated() && (ctx.Context.User.IsInRole(ApplicationRoles.Administrator) || ctx.Context.User.IsInRole(ApplicationRoles.ContestManager) || ctx.Context.User.IsInRole(ApplicationRoles.SubmissionManager)); if (!authorized) { const string unauthorizedBody = "Unauthorized"; ctx.Context.Response.StatusCode = StatusCodes.Status401Unauthorized; ctx.Context.Response.Headers["Cache-Control"] = "no-store"; ctx.Context.Response.Headers["Content-Length"] = unauthorizedBody.Length.ToString(); ctx.Context.Response.Headers["Content-Type"] = "text/html"; await ctx.Context.Response.WriteAsync(unauthorizedBody); } } }); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller}/{action=Index}/{id?}"); endpoints.MapRazorPages(); }); app.UseSpa(spa => { if (env.IsDevelopment()) { // Check https://github.com/dotnet/aspnetcore/issues/17277 // spa.UseProxyToSpaDevelopmentServer("http://localhost:4200"); spa.Options.SourcePath = "../Client"; spa.UseAngularCliServer(npmScript: "start:dotnet"); } }); lifetime.ApplicationStarted.Register(() => { var factory = new RabbitMqConnectionFactory(app.ApplicationServices); var connection = factory.GetConnection(); app.ApplicationServices.GetRequiredService <JobRequestProducer>().Start(connection); app.ApplicationServices.GetRequiredService <JobCompleteConsumer>().Start(connection); app.ApplicationServices.GetRequiredService <WorkerHeartbeatConsumer>().Start(connection); }); lifetime.ApplicationStopping.Register(() => { var factory = new RabbitMqConnectionFactory(app.ApplicationServices); app.ApplicationServices.GetRequiredService <WorkerHeartbeatConsumer>().Stop(); app.ApplicationServices.GetRequiredService <JobCompleteConsumer>().Stop(); app.ApplicationServices.GetRequiredService <JobRequestProducer>().Stop(); factory.CloseConnection(); }); }
public RabbitMqConnectionManager(RabbitMqConnectionFactory connectionFactory, ConnectionConfiguration connectionConfiguration) { this.connectionFactory = connectionFactory; this.connectionConfiguration = connectionConfiguration; }
public RabbitManager() { var conn = RabbitMqConnectionFactory.CreateConnection(Thread.CurrentThread.ManagedThreadId); RabbitMqConnectionFactory.CreateChannel(Thread.CurrentThread.ManagedThreadId, conn); }