コード例 #1
0
        public void Execute(IJobExecutionContext context)
        {
            var configurationByserviceKey = ServiceHelper.ConfigurationByserviceKey;

            foreach (var serviceKey in configurationByserviceKey.Keys.ToArray())
            {
                try
                {
                    var serviceStateInfo = ServiceHelper.ServiceStateInfoByServuceKey[serviceKey];
                    if (serviceStateInfo.ServiceState != ServiceStateEnum.Normal && serviceStateInfo.ServiceState != ServiceStateEnum.Failed)
                    {
                        return;
                    }

                    string msg;
                    var    executeState = ServiceHelper.ServiceJobBaseByServiceKey[serviceKey].ActiveTest(out msg);
                    if (executeState == ExecuteStateEnum.Normal)
                    {
                        _log.InfoFormat("服务 [{0}] 心跳测试成功", configurationByserviceKey[serviceKey].ServiceName);
                        ServiceInit.ChangeServiceStateInfo(new ServiceStateInfo()
                        {
                            ServiceState = ServiceStateEnum.Normal
                        });
                    }

                    if (executeState == ExecuteStateEnum.Failed)
                    {
                        _log.InfoFormat("服务 [{0}] 心跳测试失败", configurationByserviceKey[serviceKey].ServiceName);
                        ServiceInit.ChangeServiceStateInfo(new ServiceStateInfo()
                        {
                            ServiceState = ServiceStateEnum.Failed
                        });
                    }

                    if (executeState == ExecuteStateEnum.Exception)
                    {
                        throw new Exception(msg);
                    }
                }
                catch (Exception ex)
                {
                    _log.ErrorFormat("服务 [{0}] 心跳测试异常", ex, configurationByserviceKey[serviceKey].ServiceName);
                    ServiceInit.ChangeServiceStateInfo(new ServiceStateInfo()
                    {
                        ExcuteDescription = ex.Message,
                        ServiceState      = ServiceStateEnum.Exception
                    });
                    ServiceInit.UninstallServiceByServiceKey(serviceKey);
                }
                finally
                {
                    if (ServiceHelper.ServiceStateInfoByServuceKey[serviceKey].ServiceState ==
                        ServiceStateEnum.Exception)
                    {
                        ServiceInit.ResetStartServiceByServiceKey(serviceKey);
                    }
                }
            }
        }
コード例 #2
0
        public void Execute(IJobExecutionContext context)
        {
            var serviceKey = context.JobDetail.JobDataMap["ServiceKey"] as string;

            try
            {
                var serviceStateInfo = ServiceHelper.ServiceStateInfoByServuceKey[serviceKey];
                if (serviceStateInfo.ServiceState != ServiceStateEnum.Normal && serviceStateInfo.ServiceState != ServiceStateEnum.Failed)
                {
                    return;
                }

                string message;
                var    executeState = ServiceHelper.ServiceJobBaseByServiceKey[serviceKey].Execute(out message);

                if (executeState == ExecuteStateEnum.Normal)
                {
                    _log.InfoFormat("服务 [{0}] 业务执行成功", serviceStateInfo.ServiceName);
                }

                if (executeState == ExecuteStateEnum.Failed)
                {
                    _log.InfoFormat("服务 [{0}] 业务执行失败", serviceStateInfo.ServiceName);
                    ServiceInit.ChangeServiceStateInfo(new ServiceStateInfo()
                    {
                        ServiceState      = ServiceStateEnum.Failed,
                        ExcuteDescription = message
                    });
                }

                if (executeState == ExecuteStateEnum.Exception)
                {
                    throw new Exception(message);
                }
            }
            catch (Exception ex)
            {
                _log.ErrorFormat("服务 [{0}] 执行异常", ex, ServiceHelper.ConfigurationByserviceKey[serviceKey].ServiceName);

                ServiceInit.ChangeServiceStateInfo(new ServiceStateInfo()
                {
                    ServiceState      = ServiceStateEnum.Exception,
                    ExcuteDescription = ex.Message,
                    ServiceStopTime   = DateTime.Now
                });
                ServiceInit.ResetStartServiceByServiceKey(serviceKey);
            }
        }
コード例 #3
0
        public SendCommandResult SendCommand(SendCommandParam sendCommandParam)
        {
            if (sendCommandParam == null)
            {
                _log.Info("[SendCommand]:服务调用失败 [sendCommandParam参数不能为null]");
                return(new SendCommandResult()
                {
                    Success = false, Message = "sendCommandParam参数不能为null"
                });
            }
            if (sendCommandParam.Command == ServiceCommandEnum.Load)
            {
                ServiceInit.LoadService();
                _log.InfoFormat("[SendCommand]:服务加载成功");
                return(new SendCommandResult()
                {
                    Success = true, Message = "指令执行成功"
                });
            }

            if (!ServiceHelper.ServiceStateInfoByServuceKey.ContainsKey(sendCommandParam.ServiceKey))
            {
                _log.InfoFormat("[SendCommand] 调用失败,找不到该指定的服务 [{0}]", sendCommandParam);
                return(new SendCommandResult()
                {
                    Success = false,
                    Message = $"找不到指定ID[{sendCommandParam.ServiceKey}]的服务"
                });
            }

            var serviceStateInfo = ServiceHelper.ServiceStateInfoByServuceKey[sendCommandParam.ServiceKey];
            SendCommandResult sendCommandResult;

            switch (sendCommandParam.Command)
            {
            case ServiceCommandEnum.Start:
            {
                if (serviceStateInfo.ServiceState == ServiceStateEnum.Uninstall)
                {
                    _log.InfoFormat("[SendCommand] 调用失败,该指定的服务已卸载 [{0}]", sendCommandParam);
                    return(new SendCommandResult()
                        {
                            Success = false,
                            Message = $"指定ID[{sendCommandParam.ServiceKey}]该指定的服务已卸载"
                        });
                }

                if (serviceStateInfo.ServiceState == ServiceStateEnum.Normal ||
                    serviceStateInfo.ServiceState == ServiceStateEnum.Failed)
                {
                    _log.InfoFormat("[SendCommand] 调用失败,该指定的服务已在运行中 [{0}]", sendCommandParam);
                    return(new SendCommandResult()
                        {
                            Success = false,
                            Message = $"指定ID[{sendCommandParam.ServiceKey}]的服务已在运行中"
                        });
                }


                sendCommandResult = ServiceInit.StartServiceByServiceKey(sendCommandParam.ServiceKey);
                if (!sendCommandResult.Success)
                {
                    _log.InfoFormat("[SendCommand] 启动失败 [{0}]", sendCommandParam);
                    return(new SendCommandResult()
                        {
                            Success = false,
                            Message = $"指定ID[{sendCommandParam.ServiceKey}]的服务启动失败  失败信息:[{sendCommandResult.Message}]"
                        });
                }
            }
            break;

            case ServiceCommandEnum.Stop:
            {
                if (serviceStateInfo.ServiceState == ServiceStateEnum.Uninstall)
                {
                    _log.InfoFormat("[SendCommand] 调用失败,该指定的服务已卸载 [{0}]", sendCommandParam);
                    return(new SendCommandResult()
                        {
                            Success = false,
                            Message = $"指定ID[{sendCommandParam.ServiceKey}]该指定的服务已卸载"
                        });
                }

                if (
                    serviceStateInfo.ServiceState == ServiceStateEnum.Stopped)
                {
                    _log.InfoFormat("[SendCommand] 调用失败,该指定的服务未启动 [{0}]", sendCommandParam);
                    return(new SendCommandResult()
                        {
                            Success = false,
                            Message = $"指定ID[{sendCommandParam.ServiceKey}]的服务未启动"
                        });
                }

                sendCommandResult = ServiceInit.StopServiceByServiceKey(sendCommandParam.ServiceKey);
                if (!sendCommandResult.Success)
                {
                    _log.InfoFormat("[SendCommand] 停止失败 [{0}]", sendCommandParam);
                    return(new SendCommandResult()
                        {
                            Success = false,
                            Message = $"指定ID[{sendCommandParam.ServiceKey}]的服务停止失败 失败信息:[{sendCommandResult.Message}]"
                        });
                }
            }
            break;

            case ServiceCommandEnum.ResetStart:
            {
                if (serviceStateInfo.ServiceState == ServiceStateEnum.Uninstall)
                {
                    _log.InfoFormat("[SendCommand] 调用失败,该指定的服务已卸载 [{0}]", sendCommandParam);
                    return(new SendCommandResult()
                        {
                            Success = false,
                            Message = $"指定ID[{sendCommandParam.ServiceKey}]该指定的服务已卸载"
                        });
                }

                sendCommandResult = ServiceInit.ResetStartServiceByServiceKey(sendCommandParam.ServiceKey);
                if (!sendCommandResult.Success)
                {
                    _log.InfoFormat("[SendCommand] 重新启动失败 [{0}]", sendCommandParam);
                    return(new SendCommandResult()
                        {
                            Success = false,
                            Message = $"指定ID[{sendCommandParam.ServiceKey}]的服务重新启动失败 失败信息:[{sendCommandResult.Message}]"
                        });
                }
            }
            break;

            case ServiceCommandEnum.Install:
            {
                if (serviceStateInfo.ServiceState != ServiceStateEnum.Uninstall)
                {
                    _log.InfoFormat("[SendCommand] 调用失败,该指定的服务已安装 [{0}]", sendCommandParam);
                    return(new SendCommandResult()
                        {
                            Success = false,
                            Message = $"指定ID[{sendCommandParam.ServiceKey}]的服务已被安装"
                        });
                }

                sendCommandResult = ServiceInit.InstallServiceByServiceKey(sendCommandParam.ServiceKey);
                if (!sendCommandResult.Success)
                {
                    _log.InfoFormat("[SendCommand] 加载失败 [{0}]", sendCommandParam);
                    return(new SendCommandResult()
                        {
                            Success = false,
                            Message = $"指定ID[{sendCommandParam.ServiceKey}]的服务安装失败 失败信息:[{sendCommandResult.Message}]"
                        });
                }
            }
            break;

            case ServiceCommandEnum.Uninstall:
            {
                if (serviceStateInfo.ServiceState == ServiceStateEnum.Uninstall)
                {
                    _log.InfoFormat("[SendCommand] 调用失败,该指定的服务未被安装 [{0}]", sendCommandParam);
                    return(new SendCommandResult()
                        {
                            Success = false,
                            Message = $"指定ID[{sendCommandParam.ServiceKey}]的服务未被安装"
                        });
                }

                sendCommandResult = ServiceInit.UninstallServiceByServiceKey(sendCommandParam.ServiceKey);
                if (!sendCommandResult.Success)
                {
                    _log.InfoFormat("[SendCommand] 卸载失败 [{0}]", sendCommandParam);
                    return(new SendCommandResult()
                        {
                            Success = false,
                            Message = $"指定ID[{sendCommandParam.ServiceKey}]的服务卸载失败 失败信息:[{sendCommandResult.Message}]"
                        });
                }
            }
            break;

            default:
                return(new SendCommandResult()
                {
                    Success = false,
                    Message = $"[sendCommandParam.Command]指令不正确"
                });
            }
            _log.InfoFormat("[SendCommand] 调用成功 [{0}]", sendCommandParam);
            return(new SendCommandResult()
            {
                Success = true, Message = "指令执行成功"
            });
        }