Esempio n. 1
0
 public void Start(ServicesConfiguration configuration)
 {
     if (status.TryChangeState(ServiceLauncherState.Stopped, ServiceLauncherState.Starting))
     {
         consoleHandler.AddStopHandler(StopAll);
         output.Info("Launching services...\r\n");
         foreach (var servicesGroup in configuration.Groups.OrderByDescending(x => x.Priority))
         {
             foreach (var service in servicesGroup.Services.OrderByDescending(x => x.Priority))
             {
                 if (!status.HasState(ServiceLauncherState.Starting))
                 {
                     return;
                 }
                 try
                 {
                     output.Info(string.Format("Starting {0}...", service.Name));
                     var process = processLauncher.Launch(service);
                     process.OnExit += (sender, args) =>
                     {
                         processes.Remove(process);
                         if (status.HasState(ServiceLauncherState.Stopping) || status.HasState(ServiceLauncherState.Stopped))
                         {
                             return;
                         }
                         output.Error(string.Format("Service {0} has exited", process.Name));
                     };
                     processes.Add(process);
                     output.Info(string.Format("Service {0} launched", service.Name));
                 }
                 catch (Exception exception)
                 {
                     output.Error(string.Format("Service {0} failed to launch", service.Name), exception);
                 }
             }
             threadSleeper.Sleep(servicesGroup.Timeout);
         }
         status.ChangeState(ServiceLauncherState.Started);
         output.Info("\r\nServices were launched");
     }
 }