Windows服务间歇性停止

时间:2014-10-03 15:21:18

标签: c# parallel-processing windows-services parallel.foreach

我有一个Windows服务,用于监控服务器和服务以及这些服务。

为此,我使用两个parallel.foreach循环服务器的外部循环和特定服务器上的服务的内部循环......这是一种正确的方法吗?我将在每台服务器上拥有大约400台服务器和10项服务,因此每次迭代完全可以进行4k次计算。但问题是Windows服务间歇性关闭并且不记录数据......我不确定这是否是因为服务器上的负载。还有,请你建议我这个Windows服务运行的服务器的最低硬件配置应该是什么。代码如下所示。

 public void CheckForWIServices()
        {

            List<ServiceMonitor> serviceMonitorList = new List<ServiceMonitor>();
            ConcurrentQueue<Exception> exceptions = new ConcurrentQueue<Exception>();
            serverList = businessLayerObj.GetServerList();
            Parallel.ForEach(
                serverList,
                () => new Dictionary<string, List<ServiceMonitor>>(),
                (server, loop, localState) =>
                {
                    try
                    {
                        localState.Add(server.ServerName, serverServicesCheck(server));
                    }
                    catch (Win32Exception eEx)
                    {
                        exceptions.Enqueue(eEx);
                    }
                    catch (InvalidOperationException Iex)
                    {
                        exceptions.Enqueue(Iex);
                    }
                    catch (Exception ex)
                    {
                        exceptions.Enqueue(ex);
                    }
                    return localState;
                },
                    localState =>
                    {
                        lock (serviceMonitorList)
                        {
                            foreach (KeyValuePair<string, List<ServiceMonitor>> item in localState)
                            {
                                var list = (List<ServiceMonitor>)item.Value;
                                foreach (ServiceMonitor serviceMonitor in list)
                                {
                                    serviceMonitorList.Add(serviceMonitor);
                                }
                            }
                        }
                    });
            businessLayerObj.SaveWIServices(serviceMonitorList);
            businessLayerObj.SetEndTimeWindowsService();
            //businessLayerObj.AddErrorToDbObject();
            businessLayerObj.SaveIndexTableChanges();
            //if (exceptions.Count > 0) throw new AggregateException(exceptions);
            //return serviceMonitorList;
        }


public List<ServiceMonitor> serverServicesCheck(Server server)
        {
            List<Service> listOfServicesToBeMonitored = new List<Service>();
            List<ServiceMonitor> serviceMonitorList = new List<ServiceMonitor>();
            lock (lockObject)
            {
                listOfServicesToBeMonitored = businessLayerObj.GetListOfServicesMonitored(server);
            }
            if (listOfServicesToBeMonitored.Count > 0)
            {
                Parallel.ForEach(
                    listOfServicesToBeMonitored,
                    () => new List<ServiceMonitor>(),
                    (service, loop, localState) =>
                    {
                        if (service.ServiceType.ToLower() == "windows service")
                        {

                            localState.Add(new ServiceMonitor
                            {
                                CreatedDateTime = DateTime.Now,
                                ServerID = server.ServerID,
                                ServiceID = service.ServiceID,
                                ServiceStatus = CheckForWindowsService(server, service)
                            });
                        }
                        else if (service.ServiceType.ToLower().Contains("iis"))
                        {
                            localState.Add(new ServiceMonitor
                            {
                                CreatedDateTime = DateTime.Now,
                                ServerID = server.ServerID,
                                ServiceID = service.ServiceID,
                                ServiceStatus = CheckForIISServices(server, service)
                            });
                        }
                        return localState;
                    },
                        localState =>
                        {
                            lock (serviceMonitorList)
                            {
                                foreach (ServiceMonitor serviceMonitor in localState)
                                {
                                    serviceMonitorList.Add(serviceMonitor);
                                }
                            }
                        });
            }
            return serviceMonitorList;
        }



public bool CheckForWindowsService(Server server, Service service)
        {
            bool windowsService = false;

            try
            {
                windowsService = (new ServiceController(service.ServiceName, server.ServerName).Status == ServiceControllerStatus.Running);
            }
            catch (Exception ex)
            {
                //businessLayerObj.AddErrorObjectToStaticList(ex, server, service);
            }

            return windowsService;
        }

public bool CheckForIISServices(Server server, Service service)
        {
            bool windowsService = false;

            try
            {
                windowsService = ((ServerManager.OpenRemote(server.ServerName)).ApplicationPools[service.ServiceName].State == ObjectState.Started);
            }
            catch (Exception ex)
            {
                //businessLayerObj.AddErrorObjectToStaticList(ex, server, service);
            }

            return windowsService;

        }

0 个答案:

没有答案