Skip to content

Commit

Permalink
Changed how exceptions are thrown so we're not reporting a Dapr endpo…
Browse files Browse the repository at this point in the history
…int failure when a cancellation was requested

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
  • Loading branch information
WhitWaldo committed Sep 26, 2024
1 parent d6e88d2 commit ff3a037
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions src/Dapr.Jobs/DaprJobsGrpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ public override async Task ScheduleJobAsync(string jobName, DaprJobSchedule sche
{
await client.ScheduleJobAlpha1Async(envelope, callOptions);
}
catch (RpcException ex)
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
//Ignore our own cancellation
}
catch (RpcException ex) when (ex.StatusCode == StatusCode.Cancelled && cancellationToken.IsCancellationRequested)
{
// Ignore a remote cancellation due to our own cancellation
}
catch (Exception ex)
{
throw new DaprException(
"Schedule job operation failed: the Dapr endpoint indicated a failure. See InnerException for details.",
Expand All @@ -127,28 +135,34 @@ public override async Task<DaprJobDetails> GetJobAsync(string jobName, Cancellat
if (string.IsNullOrWhiteSpace(jobName))
throw new ArgumentNullException(nameof(jobName));

var envelope = new Autogenerated.GetJobRequest { Name = jobName };

var callOptions = CreateCallOptions(headers: null, cancellationToken);
Autogenerated.GetJobResponse response;

try
{
response = await client.GetJobAlpha1Async(envelope, callOptions);
var envelope = new Autogenerated.GetJobRequest { Name = jobName };
var callOptions = CreateCallOptions(headers: null, cancellationToken);
var response = await client.GetJobAlpha1Async(envelope, callOptions);
return new DaprJobDetails(new DaprJobSchedule(response.Job.Schedule))
{
DueTime = response.Job.DueTime is not null ? DateTime.Parse(response.Job.DueTime) : null,
Ttl = response.Job.Ttl is not null ? DateTime.Parse(response.Job.Ttl) : null,
RepeatCount = response.Job.Repeats == default ? null : (int?)response.Job.Repeats,
Payload = response.Job.Data.ToByteArray()
};
}
catch (RpcException ex)
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
//Ignore our own cancellation
}
catch (RpcException ex) when (ex.StatusCode == StatusCode.Cancelled && cancellationToken.IsCancellationRequested)
{
// Ignore a remote cancellation due to our own cancellation
}
catch (Exception ex)
{
throw new DaprException(
"Get job operation failed: the Dapr endpoint indicated a failure. See InnerException for details.", ex);
}

return new DaprJobDetails(new DaprJobSchedule(response.Job.Schedule))
{
DueTime = response.Job.DueTime is not null ? DateTime.Parse(response.Job.DueTime) : null,
Ttl = response.Job.Ttl is not null ? DateTime.Parse(response.Job.Ttl) : null,
RepeatCount = response.Job.Repeats == default ? null : (int?)response.Job.Repeats,
Payload = response.Job.Data.ToByteArray()
};
throw new DaprException("Get job operation failed: the Dapr endpoint did not return the expected value.");
}

/// <summary>
Expand All @@ -163,15 +177,21 @@ public override async Task DeleteJobAsync(string jobName, CancellationToken canc
if (string.IsNullOrWhiteSpace(jobName))
throw new ArgumentNullException(nameof(jobName));

var envelope = new Autogenerated.DeleteJobRequest { Name = jobName };

var callOptions = CreateCallOptions(headers: null, cancellationToken);

try
{
var envelope = new Autogenerated.DeleteJobRequest { Name = jobName };
var callOptions = CreateCallOptions(headers: null, cancellationToken);
await client.DeleteJobAlpha1Async(envelope, callOptions);
}
catch (RpcException ex)
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
//Ignore our own cancellation
}
catch (RpcException ex) when (ex.StatusCode == StatusCode.Cancelled && cancellationToken.IsCancellationRequested)
{
// Ignore a remote cancellation due to our own cancellation
}
catch (Exception ex)
{
throw new DaprException(
"Delete job operation failed: the Dapr endpoint indicated a failure. See InnerException for details.",
Expand Down

0 comments on commit ff3a037

Please sign in to comment.