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); } } } }
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); } }