-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
260 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace NCronJob.Observer; | ||
|
||
#pragma warning disable CA1707 // Identifiers should not contain underscores | ||
#pragma warning disable S101 // Types should be named in PascalCase | ||
[Experimental("NCRONJOB_OBSERVER")] | ||
public record ExecutionProgress_Unstable | ||
#pragma warning restore S101 // Types should be named in PascalCase | ||
#pragma warning restore CA1707 // Identifiers should not contain underscores | ||
{ | ||
internal ExecutionProgress_Unstable(JobRun run) | ||
{ | ||
RunId = run.JobRunId; | ||
ParentRunId = run.ParentJobRunId; | ||
CorrelationId = run.CorrelationId; | ||
State = MapFrom(run.CurrentState.Type); | ||
} | ||
|
||
public Guid? RunId { get; init; } | ||
public Guid? ParentRunId { get; init; } | ||
public Guid CorrelationId { get; } | ||
public ExecutionState_Unstable State { get; init; } | ||
public DateTimeOffset Timestamp { get; } = DateTimeOffset.Now; | ||
|
||
private static ExecutionState_Unstable MapFrom(JobStateType currentState) | ||
{ | ||
return currentState switch | ||
{ | ||
JobStateType.NotStarted => ExecutionState_Unstable.NotStarted, | ||
JobStateType.Scheduled => ExecutionState_Unstable.Scheduled, | ||
JobStateType.Initializing => ExecutionState_Unstable.Initializing, | ||
JobStateType.Running => ExecutionState_Unstable.Running, | ||
JobStateType.Retrying => ExecutionState_Unstable.Retrying, | ||
JobStateType.Completing => ExecutionState_Unstable.Completing, | ||
JobStateType.WaitingForDependency => ExecutionState_Unstable.WaitingForDependency, | ||
JobStateType.Completed => ExecutionState_Unstable.Completed, | ||
JobStateType.Faulted => ExecutionState_Unstable.Faulted, | ||
JobStateType.Cancelled => ExecutionState_Unstable.Cancelled, | ||
JobStateType.Expired => ExecutionState_Unstable.Expired, | ||
_ => ExecutionState_Unstable.Undetermined, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace NCronJob.Observer; | ||
|
||
#pragma warning disable CA1707 // Identifiers should not contain underscores | ||
[Experimental("NCRONJOB_OBSERVER")] | ||
public enum ExecutionState_Unstable | ||
#pragma warning restore CA1707 // Identifiers should not contain underscores | ||
{ | ||
Undetermined = 0, | ||
NotStarted, | ||
Scheduled, | ||
Initializing, | ||
Running, | ||
Retrying, | ||
Completing, | ||
WaitingForDependency, | ||
Completed, | ||
Faulted, | ||
Cancelled, | ||
Expired, | ||
OrchestrationStarted, | ||
OrchestrationCompleted, | ||
} |
13 changes: 13 additions & 0 deletions
13
src/NCronJob/Observer/IJobExecutionProgressReporter_Unstable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace NCronJob.Observer; | ||
|
||
#pragma warning disable CA1707 // Identifiers should not contain underscores | ||
#pragma warning disable S101 // Types should be named in PascalCase | ||
[Experimental("NCRONJOB_OBSERVER")] | ||
public interface IJobExecutionProgressReporter_Unstable | ||
#pragma warning restore S101 // Types should be named in PascalCase | ||
#pragma warning restore CA1707 // Identifiers should not contain underscores | ||
{ | ||
IDisposable Register(Action<ExecutionProgress_Unstable> callback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System.Collections.Immutable; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace NCronJob.Observer; | ||
|
||
internal sealed class JobExecutionProgressObserver : IJobExecutionProgressReporter_Unstable | ||
{ | ||
private readonly List<Action<ExecutionProgress_Unstable>> callbacks = []; | ||
|
||
#if NET9_0_OR_GREATER | ||
private readonly Lock callbacksLock = new(); | ||
#else | ||
private readonly object callbacksLock = new(); | ||
#endif | ||
|
||
public IDisposable Register(Action<ExecutionProgress_Unstable> callback) | ||
{ | ||
ArgumentNullException.ThrowIfNull(callback); | ||
|
||
lock (callbacksLock) | ||
{ | ||
callbacks.Add(callback); | ||
} | ||
|
||
return new ActionDisposer(() => | ||
{ | ||
lock (callbacksLock) | ||
{ | ||
callbacks.Remove(callback); | ||
} | ||
}); | ||
} | ||
|
||
internal void Report(JobRun run) | ||
{ | ||
List<ExecutionProgress_Unstable> progresses = []; | ||
|
||
var progress = new ExecutionProgress_Unstable(run); | ||
progresses.Add(progress); | ||
|
||
if (run.IsOrchestrationRoot && progress.State == ExecutionState_Unstable.NotStarted) | ||
{ | ||
var orchestrationStarted = progress | ||
with | ||
{ | ||
State = ExecutionState_Unstable.OrchestrationStarted, | ||
RunId = null, | ||
ParentRunId = null, | ||
}; | ||
|
||
progresses.Insert(0, orchestrationStarted); | ||
} | ||
else if (run.IsCompleted && !run.RootJobHasPendingDependentJobs) | ||
{ | ||
var orchestrationCompleted = progress | ||
with | ||
{ | ||
State = ExecutionState_Unstable.OrchestrationCompleted, | ||
RunId = null, | ||
ParentRunId = null, | ||
}; | ||
|
||
progresses.Add(orchestrationCompleted); | ||
} | ||
|
||
foreach (var callback in callbacks) | ||
{ | ||
foreach (var entry in progresses) | ||
{ | ||
callback(entry); | ||
} | ||
} | ||
} | ||
|
||
internal sealed class ActionDisposer : IDisposable | ||
{ | ||
private bool disposed; | ||
private readonly Action disposer; | ||
|
||
public ActionDisposer(Action disposer) | ||
{ | ||
this.disposer = disposer; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (disposed) | ||
return; | ||
|
||
disposer(); | ||
|
||
disposed = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.