protected override async Task Work(CancellationToken cancellationToken) { logger.LogInformation($"Connecting to Azure Table Storage: {storageAccount.TableEndpoint}"); CloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration()); var table = tableClient.GetTableReference(clouldTableName); /* This load initial vehicle journey assignments off clould storage. */ try { LoadFromPersistentStorage(table); } catch { logger.LogWarning("Initial load of vehicle journey assignments failed. Continuing using only real-time data."); } logger.LogInformation($"Connecting to RabbitMQ: {rabbitConnectionFactory.Uri}"); var connection = rabbitConnectionFactory.CreateConnection(nameof(PersistentVehicleJourneyAssignmentWorker)); var channel = connection.CreateModel(); // The incomming exchange of ROI events. var roiExchange = config.GetValue <string>("RabbitMQ:RoiExchange", "roi-json"); // The persistent queue is shared between workers, and coordinates persistent storage of vehicle assignments (in case of a single worker restarts). var persistentQueue = config.GetValue("RabbitMQ:RoiPersistentQueue", "rpsudp2json-roi-json-persistent"); var arguments = new Dictionary <string, object> { { "x-message-ttl", 3 * 60 * 60 * 1000 } // 3 hours }; channel.QueueDeclare(persistentQueue, durable: true, exclusive: false, autoDelete: false, arguments); channel.QueueBind(persistentQueue, roiExchange, "vehicleJourneyAssignment.#"); var persistentVehicleJourneyAssignmentQueue = new EventConsumer <VehicleJourneyAssignmentEvent>( action: e => { table.Execute( TableOperation.InsertOrReplace(e.VehicleJourneyAssignment.ToTableEntity()) ); ResetWatchdog(); }, eventType: "vehicleJourneyAssignment", logger: logger); logger.LogInformation($"Beginning to consume {persistentQueue} ..."); var persistentConsumerTag = channel.BasicConsume( queue: persistentQueue, autoAck: true, consumer: persistentVehicleJourneyAssignmentQueue); await cancellationToken.WaitHandle.WaitOneAsync(CancellationToken.None); }
protected override async Task Work(CancellationToken cancellationToken) { logger.LogInformation($"Connecting to RabbitMQ: {rabbitConnectionFactory.Uri}"); rabbitConnection = rabbitConnectionFactory.CreateConnection(nameof(VehicleJourneyAssignmentLoaderWorker)); rabbitChannel = rabbitConnection.CreateModel(); // The incomming exchange of ROI events. var roiExchange = config.GetValue <string>("RabbitMQ:RoiExchange", "roi-json"); // The work queue is a per worker queue to distribute vehicle assignments. var workQueue = config.GetValue("RabbitMQ:RoiWorkQueue", "rpsudp2json-roi-json-{machineName}").Replace("{machineName}", Environment.MachineName.ToLower()); var arguments = new Dictionary <string, object> { { "x-message-ttl", 3 * 60 * 60 * 1000 } // 3 hours }; rabbitChannel.QueueDeclare(workQueue, durable: false, exclusive: true, autoDelete: true, arguments); rabbitChannel.QueueBind(workQueue, roiExchange, "vehicleJourneyAssignment.#"); var workerVehicleJourneyAssignmentConsumer = new EventConsumer <VehicleJourneyAssignmentEvent>( action: e => { vehicleJourneyAssignmentCache.Put(e.VehicleJourneyAssignment); ResetWatchdog(); }, eventType: "vehicleJourneyAssignment", logger: logger); // TODO: Wait for initial load to complete in PersistentVehicleJourneyAssignmentWorker logger.LogInformation($"Beginning to consume {workQueue}..."); rabbitChannel.BasicConsume( queue: workQueue, autoAck: true, consumer: workerVehicleJourneyAssignmentConsumer); await cancellationToken.WaitHandle.WaitOneAsync(CancellationToken.None); }