private void ServiceConnectionHandler_ServiceHandlerInitializationCompleted(object sender, ServiceHandlerConnectionChangedEventArgs e) { if (e.Status == ProcessManagerServiceHandlerStatus.Connected) { Task.Run(() => DistributeSourceFilesThread(e.ServiceHandler.Machine)); } else { ConnectionStore.RemoveConnection(e.ServiceHandler.Machine); } }
private void DistributionConnectionManagementThread() { try { Logger.Add("STARTUP -- Starting distribution connection management thread..."); while (!_shutDownRequested) { try { lock (_pendingDistributionWork) { ConnectionStore.Connections.Keys .Where(machine => !_pendingDistributionWork.Any(work => Comparer.MachinesEqual(work.DestinationMachine, machine))) .ToList() .ForEach(ConnectionStore.RemoveConnection); } List <Machine> machinesToConnect; lock (_pendingDistributionWork) { machinesToConnect = _pendingDistributionWork .Select(work => work.DestinationMachine) .Distinct(new MachineEqualityComparer()) .Where(destinationMachine => !ConnectionStore.Connections.ContainsKey(destinationMachine)) .ToList(); } machinesToConnect.ForEach(destinationMachine => { MachineConnection connection = ConnectionStore.CreateConnection(this, destinationMachine); connection.ServiceHandler.Initialize(); }); } catch (Exception ex) { Logger.Add("An unexpected error occurred in distribution connection management thread", ex); } Thread.Sleep(Settings.Service.Read <int>("DistributionConnectionCleanInterval")); } Logger.Add("SHUTDOWN -- Shutting down distribution connection management thread..."); } catch (Exception ex) { Logger.Add("Fatal exception in distribution connection management thread, dying....", ex); } finally { // indicate that we are no longer running _distributionConnectionManagementThread = null; } }
private void DistributeSourceFilesThread(Machine machine) { List <DistributionWork> processedDistributionWork = new List <DistributionWork>(); while (ConnectionStore.ConnectionCreated(machine)) { DistributionWork work; lock (_pendingDistributionWork) { work = _pendingDistributionWork .Where(pendingWork => Comparer.MachinesEqual(pendingWork.DestinationMachine, machine)) .FirstOrDefault(pendingWork => !processedDistributionWork.Contains(pendingWork)); } if (work == null) { Thread.Sleep(10); continue; } processedDistributionWork.Add(work); Task.Run(() => { try { try { Group destinationGroup = ConnectionStore.Connections[work.DestinationMachine].Configuration.Groups .FirstOrDefault(group => Comparer.GroupsEqual(group, work.Group)); if (destinationGroup == null) { Logger.AddAndThrow <DistributionActionException>(LogType.Warning, $"Could not distribute application {work.Application.Name} in group {work.Group.Name}" + $" to destination machine {work.DestinationMachine.HostName}. Destination machine does not contain a matching group."); } List <string> errorMessages = new List <string>(); foreach (DistributionFile file in work.Files) { file.DestinationGroupID = destinationGroup.ID; file.Content = FileSystemHandler.GetFileContent(Path.Combine(work.Group.Path, file.RelativePath.Trim(Path.DirectorySeparatorChar))); DistributeFileResult result = ConnectionStore.Connections[work.DestinationMachine].ServiceHandler.Service.DistributeFile(new DTODistributionFile(file)).FromDTO(); try { if (result.Success) { Logger.Add(LogType.Verbose, $"Distribution of file to {work.DestinationMachine.HostName} succeeded: {file.RelativePath}, {file.Content.Length} bytes"); } else { Logger.AddAndThrow <DistributionActionException>(LogType.Error, $"Distribution of file to {work.DestinationMachine.HostName} failed: {Path.GetFileName(file.RelativePath)} | {result.ErrorMessage}"); } } catch (DistributionActionException ex) { errorMessages.Add(ex.Message); } file.IsDistributed = true; } if (errorMessages.Any()) { throw new DistributionActionException(string.Join(Environment.NewLine, errorMessages.Select(x => x.Replace(" | ", Environment.NewLine + "\t")))); } RaiseDistributionCompletedEvent(new DistributionResult(work, DistributionResultValue.Success), work.ClientId); } catch (DistributionActionException) { throw; } catch (ThreadAbortException) { } catch (Exception ex) { Logger.AddAndThrow <DistributionActionException>("An unexpected error occurred while distributing source files, aborting..", ex); } finally { lock (_pendingDistributionWork) _pendingDistributionWork.Remove(work); } } catch (DistributionActionException ex) { RaiseDistributionCompletedEvent(new DistributionResult(work, DistributionResultValue.Failure, ex.Message), work.ClientId); } }); } }