private void ProcessFile(string filePath, Guid agentId, CancellationToken token) { if (FileHelper.IsFileValid(filePath)) { lock (_sequances) { var agentQueueName = GetAgentQueueName(agentId); if (!_sequances.ContainsKey(agentId)) { _status = ProcessingStatus.FileProcessing; var newSequance = new Sequance(agentId, _sequanceTime, token); newSequance.OnSequanceCompleted += SequanceProcess; _sequances.Add(agentId, newSequance); if (!System.Messaging.MessageQueue.Exists(agentQueueName)) { System.Messaging.MessageQueue.Create(agentQueueName); using (var queuesQueue = new System.Messaging.MessageQueue(_queuesQueueName, QueueAccessMode.Send)) { var agentQueue = new AgentQueue { AgentId = agentId, QueueName = agentQueueName }; queuesQueue.Send(new Message(agentQueue)); } } } _sequances[agentId].UpdateSequanceState(); using (var fileQueue = new System.Messaging.MessageQueue(agentQueueName, QueueAccessMode.Send)) { var message = new Message(filePath); fileQueue.Send(message); _processedCount++; } } } else { var fileName = Path.GetFileName(filePath); var agentName = agentId.ToString().Substring(0, 4); var trashFilePath = Path.Combine(_trashDirectory, agentName, fileName); FileHelper.MoveWithRenaming(filePath, trashFilePath); HostLogger.Get <ProcessingService>().Error($"Recieved invalid file {filePath}"); } _status = ProcessingStatus.Idle; }
private void RestorePreviousSession() { if (!System.Messaging.MessageQueue.Exists(_queuesQueueName)) { System.Messaging.MessageQueue.Create(_queuesQueueName); } using (var queuesQueue = new System.Messaging.MessageQueue(_queuesQueueName, QueueAccessMode.Receive)) { queuesQueue.Formatter = new XmlMessageFormatter(new[] { typeof(AgentQueue) }); var agentQueues = queuesQueue.GetAllMessages().Select(x => x.Body as AgentQueue); foreach (var agentQqueue in agentQueues) { if (agentQqueue == null) { continue; } lock (_sequances) { if (!_sequances.ContainsKey(agentQqueue.AgentId)) { var newSequance = new Sequance(agentQqueue.AgentId, _sequanceTime, _cancelationSource.Token); newSequance.OnSequanceCompleted += SequanceProcess; _sequances.Add(agentQqueue.AgentId, newSequance); if (!System.Messaging.MessageQueue.Exists(agentQqueue.QueueName)) { System.Messaging.MessageQueue.Create(agentQqueue.QueueName); } } _sequances[agentQqueue.AgentId].UpdateSequanceState(); } } } }