-
Notifications
You must be signed in to change notification settings - Fork 0
Add failure policy in dapr schedular #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using System; | ||
|
|
||
| namespace BBT.Aether.BackgroundJob; | ||
|
|
||
| public sealed class JobScheduleFailurePolicy | ||
| { | ||
| public FailurePolicyType PolicyType { get; private init; } | ||
| public TimeSpan? Interval { get; private init; } | ||
| public uint? MaxRetries { get; private init; } | ||
|
|
||
| public static JobScheduleFailurePolicy Drop() => | ||
| new() { PolicyType = FailurePolicyType.Drop }; | ||
|
|
||
| public static JobScheduleFailurePolicy Constant(TimeSpan interval, uint? maxRetries = null) => | ||
| new() { PolicyType = FailurePolicyType.Constant, Interval = interval, MaxRetries = maxRetries }; | ||
| } | ||
|
|
||
| public enum FailurePolicyType { Drop, Constant } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ public async Task<Guid> EnqueueAsync<TPayload>( | |
| TPayload payload, | ||
| string schedule, | ||
| Dictionary<string, object>? metadata = null, | ||
| JobScheduleFailurePolicy? failurePolicyOptions = null, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(handlerName)) | ||
|
|
@@ -116,7 +117,7 @@ public async Task<Guid> EnqueueAsync<TPayload>( | |
| // This prevents race condition where scheduler triggers before DB write completes | ||
| uow.OnCompleted(async _ => | ||
| { | ||
| await jobScheduler.ScheduleAsync(handlerName, jobName, schedule, payloadBytes, cancellationToken); | ||
| await jobScheduler.ScheduleAsync(handlerName, jobName, schedule, payloadBytes, failurePolicyOptions, cancellationToken); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While the failure policy is correctly passed to the scheduler here, it is not being persisted in the |
||
|
|
||
| logger.LogInformation( | ||
| "Successfully scheduled job handler '{HandlerName}' with job name '{JobName}'. Entity ID: {EntityId}", | ||
|
|
@@ -126,10 +127,6 @@ public async Task<Guid> EnqueueAsync<TPayload>( | |
| // Commit transaction - OnCompleted handlers run after this completes | ||
| await uow.CommitAsync(cancellationToken); | ||
|
|
||
| logger.LogInformation( | ||
| "Successfully enqueued job handler '{HandlerName}' with job name '{JobName}'. Entity ID: {EntityId}", | ||
| handlerName, jobName, jobId); | ||
|
|
||
| activity?.SetStatus(ActivityStatusCode.Ok); | ||
| return jobId; | ||
| } | ||
|
|
@@ -195,7 +192,7 @@ public async Task UpdateAsync(Guid id, string newSchedule, CancellationToken can | |
| // Reschedule with new schedule | ||
| var payloadBytes = eventSerializer.Serialize(envelope); | ||
| await jobScheduler.ScheduleAsync(jobInfo.HandlerName, jobInfo.JobName, newSchedule, payloadBytes, | ||
| cancellationToken); | ||
| cancellationToken: cancellationToken); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // Save updated entity | ||
| await jobStore.SaveAsync(jobInfo, cancellationToken); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ public async Task ScheduleAsync( | |
| string jobName, | ||
| string schedule, | ||
| ReadOnlyMemory<byte> payload, | ||
| JobScheduleFailurePolicy? failurePolicyOptions = null, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(handlerName)) | ||
|
|
@@ -57,6 +58,7 @@ await daprJobsClient.ScheduleJobAsync( | |
| schedule: daprSchedule, | ||
| payload: new ReadOnlyMemory<byte>(payloadBytes), | ||
| overwrite: true, | ||
| failurePolicyOptions: MapFailurePolicy(failurePolicyOptions), | ||
| cancellationToken: cancellationToken); | ||
|
|
||
| activity?.SetStatus(ActivityStatusCode.Ok); | ||
|
|
@@ -92,7 +94,7 @@ public async Task UpdateScheduleAsync( | |
| { | ||
| var jobInfo = await daprJobsClient.GetJobAsync(jobName, cancellationToken); | ||
| await daprJobsClient.DeleteJobAsync(jobName, cancellationToken); | ||
| await ScheduleAsync(handlerName, jobName, newSchedule, jobInfo.Payload, cancellationToken); | ||
| await ScheduleAsync(handlerName, jobName, newSchedule, jobInfo.Payload, cancellationToken: cancellationToken); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This call to |
||
|
|
||
| activity?.SetStatus(ActivityStatusCode.Ok); | ||
| } | ||
|
|
@@ -157,6 +159,16 @@ private static void RecordException(Activity? activity, Exception ex) | |
| })); | ||
| } | ||
|
|
||
| private static IJobFailurePolicyOptions? MapFailurePolicy(JobScheduleFailurePolicy? policy) => | ||
| policy switch | ||
| { | ||
| null => null, | ||
| { PolicyType: FailurePolicyType.Drop } => new JobFailurePolicyDropOptions(), | ||
| { PolicyType: FailurePolicyType.Constant, Interval: { } interval } => | ||
| new JobFailurePolicyConstantOptions(interval) { MaxRetries = policy.MaxRetries }, | ||
| _ => null | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// Parses a schedule string into a DaprJobSchedule. | ||
| /// Supports cron expressions and simple delay formats. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commenting out package references is generally discouraged as it leaves dead code in the project file. If the Zipkin exporter is no longer needed, it should be removed entirely. If it is intended to be optional or used only in specific environments, consider using conditional property groups or documenting the reason for keeping it commented out.