| | | 1 | | using System; |
| | | 2 | | using System.ComponentModel; |
| | | 3 | | |
| | | 4 | | namespace Lattice.ECS.Session |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// 会话运行器。 |
| | | 8 | | /// 为纯 C# 环境提供统一的 Start / Step / Stop 驱动入口。 |
| | | 9 | | /// 当前面向 `SessionRuntime` 工作,而不是绑定某一个具体运行模式。 |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class SessionRunner : IDisposable |
| | | 12 | | { |
| | | 13 | | private bool _disposed; |
| | | 14 | | private readonly bool _ownsRuntime; |
| | | 15 | | private readonly SessionRunnerLifecycleHooks _lifecycleHooks; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// 当前运行定义。 |
| | | 19 | | /// 由 `SessionRunnerBuilder` 构建的 runner 一定具备定义;手动传入 runtime 的兼容入口则没有。 |
| | | 20 | | /// </summary> |
| | 9 | 21 | | public SessionRunnerDefinition? Definition { get; } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// 当前绑定的运行时实例。 |
| | | 25 | | /// </summary> |
| | 249 | 26 | | public SessionRuntime Runtime { get; private set; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// 兼容入口:当前运行时实例。 |
| | | 30 | | /// </summary> |
| | | 31 | | [Obsolete("请改用 Runtime。Session 仅保留为兼容属性名。", false)] |
| | | 32 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 0 | 33 | | public SessionRuntime Session => Runtime; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// 当前运行时上下文。 |
| | | 37 | | /// </summary> |
| | 12 | 38 | | public SessionRuntimeContext Context => Runtime.Context; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// 运行器名称。 |
| | | 42 | | /// </summary> |
| | 16 | 43 | | public string Name { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// 运行器当前是否处于运行状态。 |
| | | 47 | | /// </summary> |
| | 13 | 48 | | public bool IsRunning => State == SessionRunnerState.Running; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// 当前运行器状态。 |
| | | 52 | | /// </summary> |
| | 120 | 53 | | public SessionRunnerState State { get; private set; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// 最近一次停止原因。 |
| | | 57 | | /// </summary> |
| | 26 | 58 | | public SessionRunnerShutdownCause LastShutdownCause { get; private set; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// 最近一次 runtime 重建原因。 |
| | | 62 | | /// </summary> |
| | 7 | 63 | | public SessionRunnerResetReason LastResetReason { get; private set; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// 最近一次生命周期失败信息。 |
| | | 67 | | /// </summary> |
| | 19 | 68 | | public SessionRunnerFailureInfo? LastFailure { get; private set; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// 当前运行时的稳定公开参数。 |
| | | 72 | | /// </summary> |
| | 1 | 73 | | public SessionRuntimeOptions RuntimeOptions => Runtime.RuntimeOptions; |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// 当前运行时类型。 |
| | | 77 | | /// </summary> |
| | 3 | 78 | | public SessionRuntimeKind RuntimeKind => Runtime.RuntimeKind; |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// 当前运行时边界。 |
| | | 82 | | /// </summary> |
| | 4 | 83 | | public SessionRuntimeBoundary RuntimeBoundary => Runtime.RuntimeBoundary; |
| | | 84 | | |
| | 9 | 85 | | public SessionRunner(SessionRuntime session) |
| | | 86 | | { |
| | 9 | 87 | | Runtime = session ?? throw new ArgumentNullException(nameof(session)); |
| | 9 | 88 | | Name = session.GetType().Name; |
| | 9 | 89 | | session.BindContextRunnerName(Name); |
| | 9 | 90 | | State = session.IsRunning ? SessionRunnerState.Running : SessionRunnerState.Created; |
| | 9 | 91 | | _ownsRuntime = false; |
| | 9 | 92 | | _lifecycleHooks = new SessionRunnerLifecycleHooks(); |
| | 9 | 93 | | } |
| | | 94 | | |
| | 20 | 95 | | public SessionRunner(SessionRunnerDefinition definition) |
| | | 96 | | { |
| | 20 | 97 | | Definition = definition ?? throw new ArgumentNullException(nameof(definition)); |
| | 20 | 98 | | Name = definition.RunnerName; |
| | 20 | 99 | | _lifecycleHooks = definition.LifecycleHooks.Clone(); |
| | 20 | 100 | | Runtime = definition.BuildRuntime(); |
| | | 101 | | |
| | | 102 | | try |
| | | 103 | | { |
| | 20 | 104 | | InvokeRuntimeCreated(Runtime); |
| | 20 | 105 | | } |
| | 0 | 106 | | catch |
| | | 107 | | { |
| | 0 | 108 | | Runtime.Dispose(); |
| | 0 | 109 | | throw; |
| | | 110 | | } |
| | | 111 | | |
| | 20 | 112 | | State = SessionRunnerState.Created; |
| | 20 | 113 | | _ownsRuntime = true; |
| | 20 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// 启动运行器。 |
| | | 118 | | /// 该方法是幂等的。 |
| | | 119 | | /// </summary> |
| | | 120 | | public void Start() |
| | | 121 | | { |
| | 17 | 122 | | ThrowIfDisposed(); |
| | | 123 | | |
| | 16 | 124 | | if (Runtime.IsRunning) |
| | | 125 | | { |
| | 1 | 126 | | State = SessionRunnerState.Running; |
| | 1 | 127 | | return; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | try |
| | | 131 | | { |
| | 15 | 132 | | InvokeLifecycleHook(_lifecycleHooks.BeforeStart, "BeforeStart"); |
| | 14 | 133 | | Runtime.Start(); |
| | 13 | 134 | | State = SessionRunnerState.Running; |
| | 13 | 135 | | InvokeLifecycleHook(_lifecycleHooks.AfterStart, "AfterStart"); |
| | 13 | 136 | | } |
| | 2 | 137 | | catch (Exception ex) |
| | | 138 | | { |
| | 2 | 139 | | HandleFailure(ResolveFailureKind(SessionRunnerFailureKind.Start, ex), ResolveOperationName("Start", ex), |
| | 2 | 140 | | throw; |
| | | 141 | | } |
| | 13 | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// 推进一步固定帧。 |
| | | 146 | | /// </summary> |
| | | 147 | | public void Step() |
| | | 148 | | { |
| | 21 | 149 | | ThrowIfDisposed(); |
| | | 150 | | |
| | 20 | 151 | | if (!Runtime.IsRunning) |
| | | 152 | | { |
| | 1 | 153 | | throw new InvalidOperationException("SessionRunner must be started before stepping."); |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | try |
| | | 157 | | { |
| | 19 | 158 | | Runtime.Update(); |
| | 18 | 159 | | } |
| | 1 | 160 | | catch (Exception ex) |
| | | 161 | | { |
| | 1 | 162 | | HandleFailure(ResolveFailureKind(SessionRunnerFailureKind.Step, ex), ResolveOperationName("Step", ex), e |
| | 1 | 163 | | throw; |
| | | 164 | | } |
| | 18 | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// 推进多个固定帧。 |
| | | 169 | | /// </summary> |
| | | 170 | | public void Step(int stepCount) |
| | | 171 | | { |
| | 5 | 172 | | ThrowIfDisposed(); |
| | | 173 | | |
| | 5 | 174 | | if (stepCount < 0) |
| | | 175 | | { |
| | 1 | 176 | | throw new ArgumentOutOfRangeException(nameof(stepCount)); |
| | | 177 | | } |
| | | 178 | | |
| | 20 | 179 | | for (int i = 0; i < stepCount; i++) |
| | | 180 | | { |
| | 6 | 181 | | Step(); |
| | | 182 | | } |
| | 4 | 183 | | } |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// 停止运行器。 |
| | | 187 | | /// 该方法是幂等的。 |
| | | 188 | | /// </summary> |
| | | 189 | | public void Stop() |
| | | 190 | | { |
| | 7 | 191 | | ThrowIfDisposed(); |
| | 6 | 192 | | StopCore(SessionRunnerShutdownCause.HostStopRequested, failureKind: SessionRunnerFailureKind.Stop, operation |
| | 6 | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// 依据当前定义重建一个新的运行时实例。 |
| | | 197 | | /// 仅适用于由 `SessionRunnerDefinition` 创建的 runner。 |
| | | 198 | | /// </summary> |
| | | 199 | | public void ResetRuntime() |
| | | 200 | | { |
| | 4 | 201 | | ThrowIfDisposed(); |
| | | 202 | | |
| | 4 | 203 | | if (Definition == null) |
| | | 204 | | { |
| | 1 | 205 | | throw new InvalidOperationException("ResetRuntime is only available for runners created from SessionRunn |
| | | 206 | | } |
| | | 207 | | |
| | 3 | 208 | | SessionRunnerResetReason reason = SessionRunnerResetReason.HostRequested; |
| | | 209 | | |
| | | 210 | | try |
| | | 211 | | { |
| | 3 | 212 | | InvokeLifecycleHook(_lifecycleHooks.BeforeReset, reason, "BeforeReset"); |
| | | 213 | | |
| | 3 | 214 | | if (Runtime.IsRunning) |
| | | 215 | | { |
| | 1 | 216 | | StopCore(SessionRunnerShutdownCause.RuntimeReset, failureKind: SessionRunnerFailureKind.Stop, operat |
| | | 217 | | } |
| | | 218 | | else |
| | | 219 | | { |
| | 2 | 220 | | LastShutdownCause = SessionRunnerShutdownCause.RuntimeReset; |
| | | 221 | | } |
| | | 222 | | |
| | 3 | 223 | | SessionRuntime previousRuntime = Runtime; |
| | 3 | 224 | | SessionRuntime nextRuntime = Definition.BuildRuntime(); |
| | | 225 | | |
| | | 226 | | try |
| | | 227 | | { |
| | 3 | 228 | | InvokeRuntimeCreated(nextRuntime); |
| | 3 | 229 | | } |
| | 0 | 230 | | catch |
| | | 231 | | { |
| | 0 | 232 | | nextRuntime.Dispose(); |
| | 0 | 233 | | throw; |
| | | 234 | | } |
| | | 235 | | |
| | 3 | 236 | | Runtime = nextRuntime; |
| | 3 | 237 | | previousRuntime.Dispose(); |
| | 3 | 238 | | State = SessionRunnerState.Created; |
| | 3 | 239 | | LastResetReason = reason; |
| | 3 | 240 | | InvokeLifecycleHook(_lifecycleHooks.AfterReset, reason, "AfterReset"); |
| | 3 | 241 | | } |
| | 0 | 242 | | catch (Exception ex) |
| | | 243 | | { |
| | 0 | 244 | | HandleFailure(ResolveFailureKind(SessionRunnerFailureKind.Reset, ex), ResolveOperationName("ResetRuntime |
| | 0 | 245 | | throw; |
| | | 246 | | } |
| | 3 | 247 | | } |
| | | 248 | | |
| | | 249 | | public void Dispose() |
| | | 250 | | { |
| | 30 | 251 | | if (_disposed) |
| | | 252 | | { |
| | 1 | 253 | | return; |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | try |
| | | 257 | | { |
| | 29 | 258 | | if (Runtime.IsRunning) |
| | | 259 | | { |
| | 7 | 260 | | StopCore(SessionRunnerShutdownCause.RunnerDisposed, failureKind: SessionRunnerFailureKind.Dispose, o |
| | | 261 | | } |
| | | 262 | | |
| | 29 | 263 | | if (_ownsRuntime) |
| | | 264 | | { |
| | 20 | 265 | | Runtime.Dispose(); |
| | | 266 | | } |
| | 29 | 267 | | } |
| | 0 | 268 | | catch (Exception ex) |
| | | 269 | | { |
| | 0 | 270 | | HandleFailure(ResolveFailureKind(SessionRunnerFailureKind.Dispose, ex), ResolveOperationName("Dispose", |
| | 0 | 271 | | throw; |
| | | 272 | | } |
| | | 273 | | |
| | 29 | 274 | | State = SessionRunnerState.Disposed; |
| | 29 | 275 | | _disposed = true; |
| | 29 | 276 | | } |
| | | 277 | | |
| | | 278 | | private void StopCore(SessionRunnerShutdownCause cause, SessionRunnerFailureKind failureKind, string operationNa |
| | | 279 | | { |
| | 14 | 280 | | if (!Runtime.IsRunning) |
| | | 281 | | { |
| | 2 | 282 | | if (cause == SessionRunnerShutdownCause.HostStopRequested && |
| | 2 | 283 | | (State == SessionRunnerState.Created || State == SessionRunnerState.Stopped)) |
| | | 284 | | { |
| | 2 | 285 | | return; |
| | | 286 | | } |
| | | 287 | | |
| | 0 | 288 | | if (cause != SessionRunnerShutdownCause.None) |
| | | 289 | | { |
| | 0 | 290 | | LastShutdownCause = cause; |
| | | 291 | | } |
| | | 292 | | |
| | 0 | 293 | | return; |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | try |
| | | 297 | | { |
| | 12 | 298 | | InvokeLifecycleHook(_lifecycleHooks.BeforeStop, cause, "BeforeStop"); |
| | 12 | 299 | | Runtime.Stop(); |
| | 12 | 300 | | State = SessionRunnerState.Stopped; |
| | 12 | 301 | | LastShutdownCause = cause; |
| | 12 | 302 | | InvokeLifecycleHook(_lifecycleHooks.AfterStop, cause, "AfterStop"); |
| | 12 | 303 | | } |
| | 0 | 304 | | catch (Exception ex) |
| | | 305 | | { |
| | 0 | 306 | | HandleFailure(ResolveFailureKind(failureKind, ex), ResolveOperationName(operationName, ex), ex, cause, s |
| | 0 | 307 | | throw; |
| | | 308 | | } |
| | 12 | 309 | | } |
| | | 310 | | |
| | | 311 | | private void HandleFailure( |
| | | 312 | | SessionRunnerFailureKind kind, |
| | | 313 | | string operationName, |
| | | 314 | | Exception exception, |
| | | 315 | | SessionRunnerShutdownCause shutdownCause, |
| | | 316 | | bool stopRuntimeIfNeeded) |
| | | 317 | | { |
| | 3 | 318 | | LastFailure = new SessionRunnerFailureInfo(kind, operationName, exception); |
| | 3 | 319 | | State = SessionRunnerState.Faulted; |
| | | 320 | | |
| | 3 | 321 | | if (shutdownCause != SessionRunnerShutdownCause.None) |
| | | 322 | | { |
| | 3 | 323 | | LastShutdownCause = shutdownCause; |
| | | 324 | | } |
| | | 325 | | |
| | 3 | 326 | | if (stopRuntimeIfNeeded && Runtime.IsRunning) |
| | | 327 | | { |
| | 2 | 328 | | TryStopAfterFailure(shutdownCause); |
| | | 329 | | } |
| | | 330 | | |
| | 3 | 331 | | TryInvokeFailureHook(LastFailure); |
| | 3 | 332 | | } |
| | | 333 | | |
| | | 334 | | private void TryStopAfterFailure(SessionRunnerShutdownCause cause) |
| | | 335 | | { |
| | | 336 | | try |
| | | 337 | | { |
| | 2 | 338 | | InvokeLifecycleHook(_lifecycleHooks.BeforeStop, cause, "BeforeStop"); |
| | 2 | 339 | | } |
| | 0 | 340 | | catch |
| | | 341 | | { |
| | 0 | 342 | | } |
| | | 343 | | |
| | | 344 | | try |
| | | 345 | | { |
| | 2 | 346 | | Runtime.Stop(); |
| | 2 | 347 | | } |
| | 0 | 348 | | catch |
| | | 349 | | { |
| | 0 | 350 | | } |
| | | 351 | | |
| | | 352 | | try |
| | | 353 | | { |
| | 2 | 354 | | InvokeLifecycleHook(_lifecycleHooks.AfterStop, cause, "AfterStop"); |
| | 2 | 355 | | } |
| | 0 | 356 | | catch |
| | | 357 | | { |
| | 0 | 358 | | } |
| | 2 | 359 | | } |
| | | 360 | | |
| | | 361 | | private void InvokeRuntimeCreated(SessionRuntime runtime) |
| | | 362 | | { |
| | 23 | 363 | | if (_lifecycleHooks.RuntimeCreated == null) |
| | | 364 | | { |
| | 20 | 365 | | return; |
| | | 366 | | } |
| | | 367 | | |
| | | 368 | | try |
| | | 369 | | { |
| | 3 | 370 | | _lifecycleHooks.RuntimeCreated(this, runtime); |
| | 3 | 371 | | } |
| | 0 | 372 | | catch (Exception ex) |
| | | 373 | | { |
| | 0 | 374 | | throw new SessionRunnerLifecycleHookException("RuntimeCreated", ex); |
| | | 375 | | } |
| | 3 | 376 | | } |
| | | 377 | | |
| | | 378 | | private void InvokeLifecycleHook(Action<SessionRunner>? hook, string hookName) |
| | | 379 | | { |
| | 28 | 380 | | if (hook == null) |
| | | 381 | | { |
| | 25 | 382 | | return; |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | try |
| | | 386 | | { |
| | 3 | 387 | | hook(this); |
| | 2 | 388 | | } |
| | 1 | 389 | | catch (Exception ex) |
| | | 390 | | { |
| | 1 | 391 | | throw new SessionRunnerLifecycleHookException(hookName, ex); |
| | | 392 | | } |
| | 2 | 393 | | } |
| | | 394 | | |
| | | 395 | | private void InvokeLifecycleHook<TArg>(Action<SessionRunner, TArg>? hook, TArg argument, string hookName) |
| | | 396 | | { |
| | 34 | 397 | | if (hook == null) |
| | | 398 | | { |
| | 30 | 399 | | return; |
| | | 400 | | } |
| | | 401 | | |
| | | 402 | | try |
| | | 403 | | { |
| | 4 | 404 | | hook(this, argument); |
| | 4 | 405 | | } |
| | 0 | 406 | | catch (Exception ex) |
| | | 407 | | { |
| | 0 | 408 | | throw new SessionRunnerLifecycleHookException(hookName, ex); |
| | | 409 | | } |
| | 4 | 410 | | } |
| | | 411 | | |
| | | 412 | | private void TryInvokeFailureHook(SessionRunnerFailureInfo failure) |
| | | 413 | | { |
| | 3 | 414 | | if (_lifecycleHooks.Failure == null) |
| | | 415 | | { |
| | 1 | 416 | | return; |
| | | 417 | | } |
| | | 418 | | |
| | | 419 | | try |
| | | 420 | | { |
| | 2 | 421 | | _lifecycleHooks.Failure(this, failure); |
| | 2 | 422 | | } |
| | 0 | 423 | | catch |
| | | 424 | | { |
| | 0 | 425 | | } |
| | 2 | 426 | | } |
| | | 427 | | |
| | | 428 | | private void ThrowIfDisposed() |
| | | 429 | | { |
| | 54 | 430 | | if (_disposed) |
| | | 431 | | { |
| | 3 | 432 | | throw new ObjectDisposedException(nameof(SessionRunner)); |
| | | 433 | | } |
| | 51 | 434 | | } |
| | | 435 | | |
| | | 436 | | private static SessionRunnerFailureKind ResolveFailureKind(SessionRunnerFailureKind defaultKind, Exception excep |
| | | 437 | | { |
| | 3 | 438 | | return exception is SessionRunnerLifecycleHookException |
| | 3 | 439 | | ? SessionRunnerFailureKind.LifecycleHook |
| | 3 | 440 | | : defaultKind; |
| | | 441 | | } |
| | | 442 | | |
| | | 443 | | private static string ResolveOperationName(string defaultOperationName, Exception exception) |
| | | 444 | | { |
| | 3 | 445 | | return exception is SessionRunnerLifecycleHookException hookException |
| | 3 | 446 | | ? hookException.HookName |
| | 3 | 447 | | : defaultOperationName; |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | private sealed class SessionRunnerLifecycleHookException : InvalidOperationException |
| | | 451 | | { |
| | | 452 | | public SessionRunnerLifecycleHookException(string hookName, Exception innerException) |
| | 1 | 453 | | : base($"SessionRunner lifecycle hook {hookName} failed.", innerException) |
| | | 454 | | { |
| | 1 | 455 | | HookName = hookName; |
| | 1 | 456 | | } |
| | | 457 | | |
| | 1 | 458 | | public string HookName { get; } |
| | | 459 | | } |
| | | 460 | | } |
| | | 461 | | } |