Пример #1
0
        private void StopServices(ITaskContext context, bool reset)
        {
            if (this.State == ServiceManagerState.Started || this.State == ServiceManagerState.InitOrStartFailed)
            {
                context.ProgressSetup(0, m_services.Count, null);
                List <IService> reversed = new List <IService>(m_services);
                reversed.Reverse();
                long i     = 0;
                bool fails = false;
                foreach (var s in reversed)
                {
                    if (s.State == ServiceState.Started)
                    {
                        try
                        {
                            context.UpdateStatus("Stopping service " + s.Name, i);
                            s.Stop(this, context);
                        }
                        catch (Exception)
                        {
                            fails = true;
                        }
                    }
                    i++;
                }
                if (fails)
                {
                    context.UpdateStatus("Failed stopping all services", m_services.Count);
                    if (this.State != ServiceManagerState.InitOrStartFailed)      // If start failed, keep that state
                    {
                        this.State = ServiceManagerState.StopFailed;
                    }
                    throw new Exception("Failed starting services. Some dependencies are missing.");
                }
                else
                {
                    context.UpdateStatus("Stopped all services", m_services.Count);
                    if (this.State != ServiceManagerState.InitOrStartFailed)      // If start failed, keep that state
                    {
                        this.State = ServiceManagerState.Stopped;
                    }
                }
            }
            else
            {
                context.UpdateStatus($"Failed to stop, because manager is in '{this.State}' state.", 0);
            }

            if (reset)
            {
                m_services = new List <IService>();
                g_manager  = null;
                this.State = ServiceManagerState.Initial;
            }
        }
Пример #2
0
        private void StartServices(ITaskContext context)
        {
            if (this.State == ServiceManagerState.Initial)
            {
                this.State = ServiceManagerState.Starting;
                if (this.ListUnregisteredDependencies().Any())
                {
                    string failed  = String.Join(", ", this.ListUnregisteredDependencies().Select(t => t.Name));
                    string message = $"Failed starting services. Some dependencies are missing ({failed}).";
                    context.UpdateStatus(message, 0);
                    this.State = ServiceManagerState.InitOrStartFailed;
                    throw new Exception(message);
                }

                context.ProgressSetup(0, m_services.Count * 2, null);
                long i = 0;
                foreach (var s in m_services)
                {
                    try
                    {
                        context.UpdateStatus("Initializing service " + s.Name, i);
                        s.Initialize(this, context);
                    }
                    catch (Exception ex)
                    {
                        context.UpdateStatus("Failed initializing service " + s.Name, i);
                        this.State = ServiceManagerState.InitOrStartFailed;
                        throw new Exception("Failed initializing service " + s.Name, ex);
                    }
                    i++;
                }
                foreach (var s in m_services)
                {
                    try
                    {
                        context.UpdateStatus("Starting service " + s.Name, i);
                        s.Start(this, context);
                    }
                    catch (Exception ex)
                    {
                        context.UpdateStatus("Failed starting service " + s.Name, i);
                        this.State = ServiceManagerState.InitOrStartFailed;
                        throw new Exception("Failed starting service " + s.Name, ex);
                    }
                    i++;
                }
                context.UpdateStatus("Started all services", m_services.Count * 2);
                this.State = ServiceManagerState.Started;
            }
            else
            {
                context.UpdateStatus($"Failed to start, because manager is in state '{this.State}'.", 0);
                throw new Exception("Failed starting services. Some dependencies are missing.");
            }
        }