Esempio n. 1
0
        async Task <RuntimeServiceState> close(RuntimeServiceState from, RuntimeServiceState to, CancellationToken cts, object prms = null)
        {
            /*List<Task<RuntimeServiceState>> waits = new List<Task<RuntimeServiceState>>(_services.Count());
             * foreach (IRuntimeService srv in _services.Where(s => !s.IsInFinalState).OrderByDescending(s => s.StartingOrder))
             *      waits.Add(srv.Transit(RuntimeServiceState.Closed, cts));
             *
             * if (waits.Count == 0)
             * {
             *      _logger.Warn("Host '{0}': no services for closing.\r\n{1}", WinServiceName, GetStates());
             *      return to;
             * }
             * else
             *      return await Task.WhenAll(waits).WithAllExceptionsArray().ConfigureAwait(false);*/
            int count = 0;

            foreach (IRuntimeService srv in _services.Where(s => !s.IsInFinalState).OrderByDescending(s => s.StartingOrder))
            {
                ++count;
                await srv.Transit(RuntimeServiceState.Closed, cts).WithAllExceptions().ConfigureAwait(false);
            }
            if (count == 0)
            {
                _logger.Warn("Host '{0}': no services for closing", WinServiceName);
            }
            return(RuntimeServiceState.Closed);
        }
Esempio n. 2
0
        async Task <RuntimeServiceState> start(RuntimeServiceState from, RuntimeServiceState to, CancellationToken cts, object prms = null)
        {
            requestStartStopTime();

            /*List<Task<RuntimeServiceState>> waits = new List<Task<RuntimeServiceState>>(_services.Count());
             * foreach (IRuntimeService srv in _services.OrderBy(s => s.StartingOrder))
             *      waits.Add(srv.Transit(RuntimeServiceState.Started, cts));
             *
             * if (waits.Count == 0)
             * {
             *      _logger.Warn("Host '{0}': no services for starting", WinServiceName);
             *      return to;
             * }
             * else
             *      return await Task.WhenAll(waits).WithAllExceptionsArray().ConfigureAwait(false);*/

            int count = 0;

            foreach (IRuntimeService srv in _services.OrderBy(s => s.StartingOrder))
            {
                ++count;
                await srv.Transit(RuntimeServiceState.Started, cts).WithAllExceptions().ConfigureAwait(false);
            }
            if (count == 0)
            {
                _logger.Warn("Host '{0}': no services for starting", WinServiceName);
            }
            return(RuntimeServiceState.Started);
        }
 internal void StopInternal()
 {
     WithLockedState(() =>
     {
         if (_state == RuntimeServiceState.Stopping || _state == RuntimeServiceState.Stopped)
         {
         }
         else
         {
             if (_state != RuntimeServiceState.Started)
             {
                 throw new InvalidOperationException("The service must be started. It is currently " + _state);
             }
             try
             {
                 _state = RuntimeServiceState.Stopping;
                 OnStop();
                 _state = RuntimeServiceState.Stopped;
             }
             finally
             {
                 if (_state != RuntimeServiceState.Stopped)
                 {
                     _state = RuntimeServiceState.Error;
                 }
                 Runtime = null;
             }
         }
     });
 }
Esempio n. 4
0
        void stateChangingHndl(object sender, RuntimeServiceState state)
        {
            ServiceDebugForm form = _dbgForm;

            if (form != null)
            {
                form.ShowState(state);
            }
        }
 protected void WithLockedState(RuntimeServiceState state, Action action)
 {
     lock (_stateTransitionLock)
     {
         if (_state != state)
         {
             throw new InvalidOperationException("The service must be " + state);
         }
         action();
     }
 }
Esempio n. 6
0
        public void ShowStateFailure(RuntimeServiceState from, RuntimeServiceState to, Exception ex)
        {
            _currState = from;

            Action act = () => { adjustButton(); string.Format("Error at transition {0} --> {1}:\r\n", from, to, ex.ToString()); textBox1.Select(0, 0); Application.DoEvents(); };

            if (InvokeRequired)
            {
                _ctx.Post((st) => act(), null);
            }
            else
            {
                act();
            }
        }
Esempio n. 7
0
        public void ShowState(RuntimeServiceState state)
        {
            _currState = state;

            Action act = () => { adjustButton(); textBox1.Text = string.Format("Service is in {0} state", state); textBox1.Select(0, 0); Application.DoEvents(); };

            if (InvokeRequired)
            {
                //Invoke( (MethodInvoker)delegate  {act();} );
                _ctx.Post((st) => act(), null);
            }
            else
            {
                act();
            }
        }
Esempio n. 8
0
        void stateChangedHandler(object sender, RuntimeServiceState oldState, RuntimeServiceState newState, Exception ex)
        {
            ServiceDebugForm form = _dbgForm;

            if (form != null)
            {
                if (ex == null)
                {
                    form.ShowState(newState);
                }
                else
                {
                    form.ShowStateFailure(oldState, newState, ex);
                }
            }
        }
        internal void StartInternal()
        {
            WithLockedState(RuntimeServiceState.Validated, () =>
            {
                _state = RuntimeServiceState.Starting;

                try
                {
                    if (!Started)
                    {
                        OnStart();
                        _state = RuntimeServiceState.Started;
                    }
                }
                finally
                {
                    if (!Started)
                    {
                        Runtime = null;
                        _state  = RuntimeServiceState.Error;
                    }
                }
            });
        }
Esempio n. 10
0
 protected void WithLockedState(RuntimeServiceState state, Action action)
 {
     lock (_stateTransitionLock)
     {
         if (_state != state)
         {
             throw new InvalidOperationException("The service must be " + state);
         }
         action();
     }
 }
Esempio n. 11
0
 internal void Validate()
 {
     WithLockedState(RuntimeServiceState.Stopped, () =>
     {
         _state = RuntimeServiceState.Validating;
         try
         {
             OnValidate();
         }
         catch(Exception ex)
         {
             _state = RuntimeServiceState.Stopped;
             throw new ValidationException("The service could not be validated", ex);
         }
         _state = RuntimeServiceState.Validated;
     });
 }
Esempio n. 12
0
 internal void StopInternal()
 {
     WithLockedState(() =>
     {
         if (_state == RuntimeServiceState.Stopping || _state == RuntimeServiceState.Stopped)
         {
         }
         else
         {
             if (_state != RuntimeServiceState.Started)
             {
                 throw new InvalidOperationException("The service must be started. It is currently "+ _state );
             }
             try
             {
                 _state = RuntimeServiceState.Stopping;
                 OnStop();
                 _state = RuntimeServiceState.Stopped;
             }
             finally
             {
                 if (_state != RuntimeServiceState.Stopped) _state = RuntimeServiceState.Error;
                 Runtime = null;
             }
         }
     });
 }
Esempio n. 13
0
        internal void StartInternal()
        {
            WithLockedState(RuntimeServiceState.Validated, () =>
            {
                _state = RuntimeServiceState.Starting;

                try
                {
                    if (!Started)
                    {
                        OnStart();
                        _state = RuntimeServiceState.Started;
                    }
                }
                finally
                {
                    if (!Started)
                    {
                        Runtime = null;
                        _state = RuntimeServiceState.Error;
                    }
                }
            });
        }
Esempio n. 14
0
 Task <RuntimeServiceState> IStateMachine <RuntimeServiceState> .Transit(RuntimeServiceState newState, CancellationToken cts, object args)
 {
     return(_stateMachine.Transit(newState, cts, args));
 }
Esempio n. 15
0
 public virtual Task <RuntimeServiceState> Closing(RuntimeServiceState from, RuntimeServiceState to, CancellationToken cts, object prms = null)
 {
     return(Task <RuntimeServiceState> .FromResult(RuntimeServiceState.Closed));
 }