public void Dispose()
 {
     lock(this)
         if(_runningExtension != null)
         {
             if(_runningExtension.Extension is IDisposable)
                 ((IDisposable)_runningExtension.Extension).Dispose();
             _runningExtension = null;
         }
 }
示例#2
0
 public void Dispose()
 {
     lock (this)
         if (_runningExtension != null)
         {
             if (_runningExtension.Extension is IDisposable)
             {
                 ((IDisposable)_runningExtension.Extension).Dispose();
             }
             _runningExtension = null;
         }
 }
        public void RunExtension(Guid guid, bool runDebugMethodOnExtension, string extensionID)
        {
            _logger.Info("Starting extension: {0} -- process monitor ID: {1}", extensionID, guid);

            lock(this)
                _runningExtension = Activate(extensionID);
            var ext = _runningExtension.Extension;

            Thread extThread = new Thread(new ThreadStart(() =>
            {
            //const TaskCreationOptions atp = TaskCreationOptions.AttachedToParent | TaskCreationOptions.LongRunning;
            //var task = Task.Factory.StartNew(() =>
            //{
                try
                {
                    _logger.Debug("Extension activation/monitoring task starting...");
                    lock (this)
                        _runningExtension.Task = Task.Factory.StartNew(() => ext.Run(_cancelSource), TaskCreationOptions.LongRunning);
                        //_runningExtension.Task = Task.Factory.StartNew(() => ext.Run(_cancelSource), _cancelSource.Token, atp, TaskScheduler.Current);
                    _extensionStarted = true;
                    if(runDebugMethodOnExtension)
                    {
                        while(!ext.RunCalled && !_cancelSource.IsCancellationRequested)
                            Thread.Sleep(250); // give the extension a chance to start up
                        _cancelSource.Token.ThrowIfCancellationRequested();
                        ext.Debug();
                    }

                    ServiceManagerClient client = null;
                    if(guid != Guid.Empty)
                    {
                        _logger.Info("Connecting to Service Manager...");
                        client = new ServiceManagerClient("ServiceManagerClient");
                        client.Disconnected += c =>
                        {
                            _logger.Info("Client state changed to: " + c.State);
                            _logger.Info("Disconnected from service manager. Execution on all extensions will be cancelled now to allow the process to shut down.");
                            _cancelSource.Cancel(); // process manager will take care of restarting this process
                        };

                        // Don't notify the server until Run has been called, otherwise the extension's Logger won't be available
                        while(!ext.RunCalled)
                            Thread.Sleep(250);
                        _logger.Info("Sending extension connection address: " + _runningExtension.Address);
                        client.NotifyExtensionServiceReady(guid, _runningExtension.Address);
                        _logger.Info("Connected.");

                    }

                    _logger.Info("Extension is now running.");
                    const string exitMsg = " Execution on all extensions will be cancelled now to allow the process to restart.";
                    while(!_cancelSource.IsCancellationRequested)
                    {
                        if(!ext.IsRunning)
                            _logger.Info("Extension {" + ext.Name + "} IsRunning == false." + exitMsg);
                        if(_runningExtension.Task.IsCompleted)
                            _logger.Info("Extension {" + ext.Name + "} Task.IsCompleted == true." + exitMsg);
                        if(_runningExtension.Task.IsFaulted)
                        {
                            _logger.Info("Extension {" + ext.Name + "} Task.IsFaulted == true." + exitMsg);
                            _logger.Info("The exception thrown by the task was: " + _runningExtension.Task.Exception);
                        }
                        if(!ext.IsRunning || _runningExtension.Task.IsCompleted || _runningExtension.Task.IsFaulted)
                            _cancelSource.Cancel();
                        if(client != null)
                        {
                            client.KeepExtensionProcessAlive(guid);
                        }
                        Thread.Sleep(3000);
                    }
                }
                catch(ThreadAbortException)
                {
                    _logger.Warn("Extension state monitoring terminating.");
                }
                catch(Exception ex)
                {
                    _logger.FatalException("EXTENSION MONITORING TASK EXCEPTION:", ex);
                }
            })); //, _cancelSource.Token, atp, TaskScheduler.Current);

            extThread.Start();

            _logger.Info("Waiting on task threads to finish...");
            try
            {
                extThread.Join();
                //task.Wait(_cancelSource.Token);
            }
            catch (ThreadAbortException) { }
            catch (ThreadInterruptedException) { }
            catch (ThreadStateException) { }
            catch (OperationCanceledException)
            {
            }
            _logger.Info("Task threads have all ended.");
        }
示例#4
0
        public void RunExtension(Guid guid, bool runDebugMethodOnExtension, string extensionID)
        {
            _logger.Info("Starting extension: {0} -- process monitor ID: {1}", extensionID, guid);

            lock (this)
                _runningExtension = Activate(extensionID);
            var ext = _runningExtension.Extension;

            Thread extThread = new Thread(new ThreadStart(() =>
            {
                //const TaskCreationOptions atp = TaskCreationOptions.AttachedToParent | TaskCreationOptions.LongRunning;
                //var task = Task.Factory.StartNew(() =>
                //{
                try
                {
                    _logger.Debug("Extension activation/monitoring task starting...");
                    lock (this)
                        _runningExtension.Task = Task.Factory.StartNew(() => ext.Run(_cancelSource), TaskCreationOptions.LongRunning);
                    //_runningExtension.Task = Task.Factory.StartNew(() => ext.Run(_cancelSource), _cancelSource.Token, atp, TaskScheduler.Current);
                    _extensionStarted = true;
                    if (runDebugMethodOnExtension)
                    {
                        while (!ext.RunCalled && !_cancelSource.IsCancellationRequested)
                        {
                            Thread.Sleep(250);                             // give the extension a chance to start up
                        }
                        _cancelSource.Token.ThrowIfCancellationRequested();
                        ext.Debug();
                    }

                    ServiceManagerClient client = null;
                    if (guid != Guid.Empty)
                    {
                        _logger.Info("Connecting to Service Manager...");
                        client = new ServiceManagerClient("ServiceManagerClient");
                        client.Disconnected += c =>
                        {
                            _logger.Info("Client state changed to: " + c.State);
                            _logger.Info("Disconnected from service manager. Execution on all extensions will be cancelled now to allow the process to shut down.");
                            _cancelSource.Cancel();                             // process manager will take care of restarting this process
                        };

                        // Don't notify the server until Run has been called, otherwise the extension's Logger won't be available
                        while (!ext.RunCalled)
                        {
                            Thread.Sleep(250);
                        }
                        _logger.Info("Sending extension connection address: " + _runningExtension.Address);
                        client.NotifyExtensionServiceReady(guid, _runningExtension.Address);
                        _logger.Info("Connected.");
                    }

                    _logger.Info("Extension is now running.");
                    const string exitMsg = " Execution on all extensions will be cancelled now to allow the process to restart.";
                    while (!_cancelSource.IsCancellationRequested)
                    {
                        if (!ext.IsRunning)
                        {
                            _logger.Info("Extension {" + ext.Name + "} IsRunning == false." + exitMsg);
                        }
                        if (_runningExtension.Task.IsCompleted)
                        {
                            _logger.Info("Extension {" + ext.Name + "} Task.IsCompleted == true." + exitMsg);
                        }
                        if (_runningExtension.Task.IsFaulted)
                        {
                            _logger.Info("Extension {" + ext.Name + "} Task.IsFaulted == true." + exitMsg);
                            _logger.Info("The exception thrown by the task was: " + _runningExtension.Task.Exception);
                        }
                        if (!ext.IsRunning || _runningExtension.Task.IsCompleted || _runningExtension.Task.IsFaulted)
                        {
                            _cancelSource.Cancel();
                        }
                        if (client != null)
                        {
                            client.KeepExtensionProcessAlive(guid);
                        }
                        Thread.Sleep(3000);
                    }
                }
                catch (ThreadAbortException)
                {
                    _logger.Warn("Extension state monitoring terminating.");
                }
                catch (Exception ex)
                {
                    _logger.FatalException("EXTENSION MONITORING TASK EXCEPTION:", ex);
                }
            }));             //, _cancelSource.Token, atp, TaskScheduler.Current);

            extThread.Start();

            _logger.Info("Waiting on task threads to finish...");
            try
            {
                extThread.Join();
                //task.Wait(_cancelSource.Token);
            }
            catch (ThreadAbortException) { }
            catch (ThreadInterruptedException) { }
            catch (ThreadStateException) { }
            catch (OperationCanceledException)
            {
            }
            _logger.Info("Task threads have all ended.");
        }