コード例 #1
0
        private void DoMonitor()
        {
            while (!_MonitorToken.IsCancellationRequested)
            {
                Thread.Sleep(10 * 1000);

                if (!_MonitorToken.IsCancellationRequested)
                {
                    ServiceSlot[] slots = ServiceContext.Current.GetServiceSlots();

                    if (0 != slots.Length)
                    {
                        foreach (ServiceSlot s in slots)
                        {
                            if (_MonitorToken.IsCancellationRequested)
                            {
                                break;
                            }

                            if (s.WorkProcess.HasExited)
                            {
                                if (0 != s.WorkProcess.ExitCode)
                                {
                                    "服务进程 {0} 已经退出 {1}".Formate(s.Name, s.WorkProcess.ExitCode.ToString()).Error();
                                    ServiceContext.Current.RemoveSlot(s.WorkProcess.Id);

                                    if ("Y" == s.Config.RestartOnError.ToUpper())
                                    {
                                        "服务进程 {0} 配置为在错误退出后重新启动".Formate(s.Name).Info();

                                        string msg = "";

                                        if (BasicServiceStarter.RunServiceProcess(ServiceContext.Current.Configuration.ServiceInfo.Name,
                                                                                  s.Config, out msg))
                                        {
                                            "服务进程 {0} 完成重启".Formate(s.Name).Info();
                                        }
                                        else
                                        {
                                            msg.Error();
                                            "服务进程 {0} 重启失败".Formate(s.Name).Error();
                                        }
                                    }
                                }
                                else
                                {
                                    "服务进程 {0} 已经退出".Formate(s.Name).Info();
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
        protected override void OnStart(string[] args)
        {
            "启动监听服务".Info();

            CStarterDControlServiceDaemon.Current.Start(_Config);
            CStarterDNotifierServiceDaemon.Current.Start(_Config);

            "监听服务已经启动".Info();

            BasicServiceStarter.Run(_Config);
        }
コード例 #3
0
        public ActionResult StartService(string name)
        {
            ActionResult retValue = new ActionResult()
            {
                Result = -1
            };

            if (1 == _StartLockCount)
            {
                retValue.Message = "正在启动一个服务,请稍后再试";
                return(retValue);
            }

            Interlocked.Increment(ref _StartLockCount);

            "尝试启动服务:{0}".Formate(name).Info();

            ServiceSlot slot = ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == name);

            ServiceStarterElement ele = ServiceContext.Current.Configuration.Services.Cast <ServiceStarterElement>().FirstOrDefault(s => s.Name == name);

            if (null != slot)
            {
                Interlocked.Decrement(ref _StartLockCount);
                retValue.Message = "服务:{0} 已经启动".Formate(name);
                retValue.Message.Info();
            }
            else
            {
                string msg = "";
                if (null != ele)
                {
                    if (!BasicServiceStarter.RunServiceProcess(ServiceContext.Current.Configuration.ServiceInfo.Name, ele, out msg))
                    {
                        retValue.Message = msg;
                        msg.Error();
                    }
                    else
                    {
                        retValue.Result = 0;
                    }
                }
                else
                {
                    retValue.Message = "找不到服务:{0} 的配置".Formate(name);;
                }

                Interlocked.Decrement(ref _StartLockCount);
            }

            return(retValue);
        }
コード例 #4
0
        static bool StartServiceProccess(ServiceStarterSection srvConfig)
        {
            bool retValue = false;

            try
            {
                "启动注册的服务".Info();
                BasicServiceStarter.Run(srvConfig);
                "服务都已经启动".Info();

                retValue = true;
            }
            catch (Exception eX)
            {
                retValue = false;
            }

            return(retValue);
        }
コード例 #5
0
        static void Main(string[] args)
        {
            ServiceStarterParams param = new ServiceStarterParams();

            List <string> extra = param.Parse(args);

            extra = param.Parse(args);

            Configuration config;

            if (!string.IsNullOrEmpty(param.ConfigurationFileName))
            {
                string fileFullName = param.ConfigurationFileName;

                if (!Path.IsPathRooted(fileFullName))
                {
                    fileFullName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, param.ConfigurationFileName);
                }

                ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                map.ExeConfigFilename = fileFullName;
                config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

                log4net.Config.XmlConfigurator.Configure(new FileInfo(fileFullName));
            }
            else
            {
                config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                log4net.Config.XmlConfigurator.Configure();
            }

            var srvConfig = (ServiceStarterSection)config.GetSection("serviceStarters");

            ServiceContext.Current.Configuration = srvConfig;

            PrepareLogger(srvConfig.ServiceInfo.Name);

            if (Environment.UserInteractive)
            {
                try
                {
                    if (param.IsShowHelp)
                    {
                        ShowHelp(param.Options);
                        (string.Join(",", extra.ToArray())).Info();
                        return;
                    }
                    else if (param.IsShowVersion)
                    {
                        Version();
                    }
                    else
                    {
                        "启动监听服务".Info();

                        CStarterDControlServiceDaemon.Current.Start(srvConfig);
                        CStarterDNotifierServiceDaemon.Current.Start(srvConfig);

                        "监听服务已经启动".Info();

                        BasicServiceStarter.Run(srvConfig);

                        "按任意键关闭程序".Info();
                        Console.ReadLine();

                        "程序正在退出,请不要关闭窗口".Info();
                        string.Format("需要停止 {0} 个服务", ServiceContext.Current.ServiceSlots.Count).Info();

                        if (0 != ServiceContext.Current.ServiceSlots.Count)
                        {
                            ServiceSlot[] slots = new ServiceSlot[ServiceContext.Current.ServiceSlots.Count];

                            ServiceContext.Current.ServiceSlots.CopyTo(slots);

                            foreach (ServiceSlot slot in slots)
                            {
                                string.Format("正在停止服务:{0}", slot.Name).Info();

                                EventWaitHandle waitExitSignal;
                                bool            created = EventWaitHandleHelper.Create("exit_" + slot.Signal, EventResetMode.ManualReset, out waitExitSignal);

                                (new CStarterClient()).Stop(srvConfig.ServiceInfo.Name, slot.Name, slot.Signal);

                                if (waitExitSignal.WaitOne(10 * 1000))
                                {
                                    if (!slot.WorkProcess.WaitForExit(10 * 1000))
                                    {
                                        slot.WorkProcess.Kill();

                                        ServiceSlot tSlot = ServiceContext.Current.ServiceSlots.FirstOrDefault(s => s.Name == slot.Name);

                                        if (null != tSlot)
                                        {
                                            ServiceContext.Current.ServiceSlots.Remove(tSlot);
                                        }
                                    }
                                }
                            }
                        }

                        "停止监听服务".Info();
                        CStarterDControlServiceDaemon.Current.Stop();
                        CStarterDNotifierServiceDaemon.Current.Stop();
                    }
                }
                catch (Exception eX)
                {
                    "servicestarter:".Error();
                    eX.Message.Error();
                    eX.Exception();
                    "使用命令cstarterd --help获取更多命令帮助".Info();
                }
            }
            else
            {
                var srv = new WindowsService()
                {
                    ServiceName = srvConfig.ServiceInfo.DisplayName
                };

                srv.Initialize(srvConfig);

                ServiceBase.Run(srv);
            }
        }