/// <summary> /// 等待所有对象信号(全开或全关) /// </summary> private static async Task WaitAllObjectSafeClose() { lock (ActiveObjects) { if (FlowServices.Length == 0 || ActiveObjects.Count == 0) { return; } } await ActiveSemaphore.WaitAsync(); }
/// <summary> /// 对象关闭时登记 /// </summary> public static void OnObjectClose(IService obj) { Logger.Information("[服务关闭] {0}", obj.ServiceName); bool can; lock (ActiveObjects) { ActiveObjects.Remove(obj); } can = ActiveObjects.Count == 0; if (can) { ActiveSemaphore.Release(); //发出完成信号 } }
/// <summary> /// 对象关闭时登记 /// </summary> public static void OnObjectFailed(IService obj) { Logger.Information("[OnObjectFailed] {0}", obj.ServiceName); bool can; lock (FailedObjects) { FailedObjects.Add(obj); } can = ActiveObjects.Count + FailedObjects.Count == FlowServices.Length; if (can) { ActiveSemaphore.Release(); //发出完成信号 } }
/// <summary> /// 系统启动时调用 /// </summary> static async Task <bool> OpenAll() { if (ZeroAppOption.Instance.ApplicationState >= StationState.BeginRun) { return(false); } ZeroAppOption.Instance.SetApplicationState(StationState.BeginRun); Logger.Information("【启动】开始"); foreach (var mid in Middlewares) { try { Logger.Information("[启动流程] {0}", mid.Name); _ = mid.Open(); } catch (Exception e) { Logger.Exception(e, "[启动流程] {0}", mid.Name); } } FlowServices = Services.Values.Where(service => !(service is EmptyService) && (service.CanRun == null || service.CanRun())).OrderBy(p => p.Level).ToArray(); foreach (var service in FlowServices) { try { Logger.Information("[启动服务] {0}", service.ServiceName); _ = service.Open(); } catch (Exception e) { Logger.Exception(e, "[启动服务] {0}", service.ServiceName); } } //等待所有对象信号(Active or Failed) if (FlowServices.Length > 0) { await ActiveSemaphore.WaitAsync(); } ZeroAppOption.Instance.SetApplicationState(StationState.Run); Logger.Information("【启动】完成"); return(true); }
/// <summary> /// 重新启动未正常启动的项目 /// </summary> public static void StartFailed() { if (Interlocked.Increment(ref inFailed) != 1) { return; } var faileds = FailedObjects.ToArray(); if (faileds.Length == 0) { Logger.Information("[StartFailed] all service is runing,no action"); return; } Logger.Information("[StartFailed>>"); FailedObjects.Clear(); foreach (var service in faileds) { try { Logger.Information("[StartFailed] {0}", service.ServiceName); service.Open(); } catch (Exception e) { Logger.Exception(e, "[StartFailed] {0}", service.ServiceName); } } //等待所有对象信号(全开或全关) ActiveSemaphore.Wait(); Interlocked.Decrement(ref inFailed); Logger.Information("<<StartFailed]"); }