Example #1
0
        protected override void OnStop()
        {
            if (null != ServiceStopCommand)
            {
                ServiceStopCommand.Set();
            }

            if (windowsServiceThread != null)
            {
                windowsServiceThread.Join();
            }
        }
Example #2
0
        protected override void OnStart(string[] args)
        {
            //Debugger.Launch();
            if (null != ServiceStopCommand)
            {
                ServiceStopCommand.Reset();
            }

            windowsServiceThread = new Thread(new ThreadStart(LogicalEntryPoint))
            {
                Name = "Windows service thread"
            };
            windowsServiceThread.Start();
        }
Example #3
0
        public void LogicalEntryPoint()
        {
            //Tracer.Trace("WorkerManager BeforeInit");
            Init();
            //Tracer.Trace("WorkerManager AfterInit");

            var stopWatch = new Stopwatch();

            while (true)
            {
                stopWatch.Restart();

                bool directorServiceFailure = false;
                try
                {
                    if (directorCoreServicesClient == null)
                    {
                        directorCoreServicesClient = new DirectorCoreServicesClient("NetTcpBinding_IDirectorCoreServices");
                    }

                    //Tracer.Info("Worker Manager polls Director for jobs");
                    OpenJobs openJobs = directorCoreServicesClient.GetOpenJobs(Environment.MachineName);

                    declaredStateOfEmergency = false;

                    if (null != openJobs)
                    {
                        //Tracer.Trace("openJobs bin size is {0}", Utils.BinSizeOf(openJobs));
                        //Tracer.Trace("WorkerManager received {0} open jobs from Director", openJobs.Jobs.Count);
                        ProcessInfoFromDirector(openJobs);
                    }
                }
                catch (TimeoutException)
                {
                    directorServiceFailure = true;
                }
                catch (CommunicationException) // EndpointNotFoundException also comes here
                {
                    directorServiceFailure = true;
                }
                catch (Exception ex)
                {
                    Tracer.Error("WorkerManager: Unhandled exception: " + ex.ToDebugString());

                    // If we want the service to stop we need to throw here and let exception be unhandled

                    Tracer.Info("WorkerManager: trying to continue after unhandled exception");
                    continue;
                }

                if (directorServiceFailure)
                {
                    Debug.Assert(directorCoreServicesClient != null, "_directorCoreServicesClient != null");
                    directorCoreServicesClient.Abort();
                    ((IDisposable)directorCoreServicesClient).Dispose();
                    directorCoreServicesClient = null;

                    //EmergencyFreeze();
                    EmergencyEvacuation();
                }

                stopWatch.Stop();
                TimeSpan getSomeRest = TimeSpan.Zero;
                if (stopWatch.Elapsed < directorPollingInterval)
                {
                    getSomeRest = directorPollingInterval - stopWatch.Elapsed;
                }

                if (ServiceStopCommand == null)
                {
                    Thread.Sleep(getSomeRest);
                }
                else
                {
                    if (ServiceStopCommand.WaitOne(getSomeRest))
                    {
                        break;
                    }
                }
            }
        }