Skip to content

Commit

Permalink
Caching JSON serialization options
Browse files Browse the repository at this point in the history
Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
  • Loading branch information
WhitWaldo committed Sep 14, 2024
1 parent 964b1dd commit b8fd743
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 359 deletions.
42 changes: 8 additions & 34 deletions src/Dapr.Jobs/DaprJobsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,22 @@ public abstract class DaprJobsClient : IDisposable
private bool disposed;

/// <summary>
/// Schedules a recurring job using a systemd Cron-like expression or '@' prefixed period string.
/// Schedules a job with Dapr.
/// </summary>
/// <param name="jobName">The name of the job being scheduled.</param>
/// <param name="schedule">The job schedule to trigger the job by.</param>
/// <param name="schedule">The schedule defining when the job will be triggered.</param>
/// <param name="payload">The main payload of the job.</param>
/// <param name="startingFrom">The optional point-in-time from which the job schedule should start.</param>
/// <param name="repeats">The optional number of times the job should be triggered.</param>
/// <param name="ttl">Represents when the job should expire. If both this and DueTime are set, TTL needs to represent a later point in time.</param>
/// <param name="payload">The main payload of the job.</param>
/// <param name="cancellationToken">Cancellation token.</param>
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public abstract Task ScheduleRecurringJobAsync(string jobName, DaprJobSchedule schedule, DateTimeOffset? startingFrom = null,
int? repeats = null, DateTimeOffset? ttl = null, ReadOnlyMemory<byte>? payload = null,
CancellationToken cancellationToken = default);

/// <summary>
/// Schedules a recurring job with an optional future starting date.
/// </summary>
/// <param name="jobName">The name of the job being scheduled.</param>
/// <param name="interval">The interval at which the job should be triggered.</param>
/// <param name="startingFrom">The optional point-in-time from which the job schedule should start.</param>
/// <param name="repeats">The optional maximum number of times the job should be triggered.</param>
/// <param name="ttl">Represents when the job should expire. If both this and StartingFrom are set, TTL needs to represent a later point in time.</param>
/// <param name="payload">The main payload of the job.</param>
/// <param name="cancellationToken">Cancellation token.</param>
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public abstract Task ScheduleIntervalJobAsync(string jobName, TimeSpan interval,
DateTimeOffset? startingFrom = null, int? repeats = null, DateTimeOffset? ttl = null, ReadOnlyMemory<byte>? payload = null,
[Obsolete(
"The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public abstract Task ScheduleJobAsync(string jobName, DaprJobSchedule schedule,
ReadOnlyMemory<byte>? payload = null, DateTimeOffset? startingFrom = null, int? repeats = null,
DateTimeOffset? ttl = null,
CancellationToken cancellationToken = default);

/// <summary>
/// Schedules a one-time job.
/// </summary>
/// <param name="jobName">The name of the job being scheduled.</param>
/// <param name="scheduledTime">The point in time when the job should be run.</param>
/// <param name="payload">Stores the main payload of the job which is passed to the trigger function.</param>
/// <param name="cancellationToken">Cancellation token.</param>
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public abstract Task ScheduleOneTimeJobAsync(string jobName, DateTimeOffset scheduledTime,
ReadOnlyMemory<byte>? payload = null, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves the details of a registered job.
/// </summary>
Expand All @@ -95,9 +71,7 @@ public abstract Task ScheduleOneTimeJobAsync(string jobName, DateTimeOffset sche
internal static KeyValuePair<string, string>? GetDaprApiTokenHeader(string apiToken)
{
if (string.IsNullOrWhiteSpace(apiToken))
{
return null;
}

return new KeyValuePair<string, string>("dapr-api-token", apiToken);
}
Expand Down
107 changes: 6 additions & 101 deletions src/Dapr.Jobs/DaprJobsGrpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,24 @@ internal DaprJobsGrpcClient(
}

/// <summary>
/// Schedules a recurring job using a systemd Cron-like expression or '@' prefixed period string.
/// Schedules a job with Dapr.
/// </summary>
/// <param name="jobName">The name of the job being scheduled.</param>
/// <param name="schedule">The job schedule to trigger the job by.</param>
/// <param name="schedule">The schedule defining when the job will be triggered.</param>
/// <param name="startingFrom">The optional point-in-time from which the job schedule should start.</param>
/// <param name="repeats">The optional number of times the job should be triggered.</param>
/// <param name="ttl">Represents when the job should expire. If both this and DueTime are set, TTL needs to represent a later point in time.</param>
/// <param name="payload">The main payload of the job.</param>
/// <param name="cancellationToken">Cancellation token.</param>
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public override async Task ScheduleRecurringJobAsync(string jobName, DaprJobSchedule schedule, DateTimeOffset? startingFrom = null,
public override async Task ScheduleJobAsync(string jobName, DaprJobSchedule schedule, DateTimeOffset? startingFrom,
int? repeats = null, DateTimeOffset? ttl = null, ReadOnlyMemory<byte>? payload = null,
CancellationToken cancellationToken = default)
{
ArgumentVerifier.ThrowIfNullOrEmpty(jobName, nameof(jobName));
ArgumentNullException.ThrowIfNull(schedule, nameof(schedule));
var job = new Autogenerated.Job { Name = jobName, Schedule = schedule.ExpressionValue };
ArgumentVerifier.ThrowIfNull(schedule, nameof(schedule));

var job = new Autogenerated.Job { Name = jobName, Schedule = schedule.ExpressionValue };

if (startingFrom is not null)
job.DueTime = ((DateTimeOffset)startingFrom).ToString("O");
Expand All @@ -96,101 +96,6 @@ public override async Task ScheduleRecurringJobAsync(string jobName, DaprJobSche

job.Ttl = ((DateTimeOffset)ttl).ToString("O");
}

var envelope = new Autogenerated.ScheduleJobRequest { Job = job };

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

try
{
await client.ScheduleJobAlpha1Async(envelope, callOptions);
}
catch (RpcException ex)
{
throw new DaprException(
"Schedule job operation failed: the Dapr endpoint indicated a failure. See InnerException for details.",
ex);
}
}

/// <summary>
/// Schedules a recurring job with an optional future starting date.
/// </summary>
/// <param name="jobName">The name of the job being scheduled.</param>
/// <param name="interval">The interval at which the job should be triggered.</param>
/// <param name="startingFrom">The optional point-in-time from which the job schedule should start.</param>
/// <param name="repeats">The optional maximum number of times the job should be triggered.</param>
/// <param name="ttl">Represents when the job should expire. If both this and StartingFrom are set, TTL needs to represent a later point in time.</param>
/// <param name="payload">The main payload of the job.</param>
/// <param name="cancellationToken">Cancellation token.</param>
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public override async Task ScheduleIntervalJobAsync(string jobName, TimeSpan interval,
DateTimeOffset? startingFrom = null, int? repeats = null,
DateTimeOffset? ttl = null, ReadOnlyMemory<byte>? payload = null, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(jobName))
throw new ArgumentNullException(nameof(jobName));

var job = new Autogenerated.Job { Name = jobName, Schedule = interval.ToDurationString() };

if (startingFrom is not null)
job.DueTime = ((DateTimeOffset)startingFrom).ToString("O");

if (repeats is not null)
{
if (repeats < 0)
throw new ArgumentOutOfRangeException(nameof(repeats));

job.Repeats = (uint)repeats;
}

if (payload is not null)
job.Data = job.Data = new Any { Value = ByteString.CopyFrom(payload.Value.Span), TypeUrl = "dapr.io/schedule/jobpayload" };

if (ttl is not null)
{
if (ttl < startingFrom)
throw new ArgumentException(
$"When both {nameof(ttl)} and {nameof(startingFrom)} are specified, the {nameof(ttl)} must represent a later point in time");

job.Ttl = ((DateTimeOffset)ttl).ToString("O");
}

var envelope = new Autogenerated.ScheduleJobRequest { Job = job};

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

try
{
await client.ScheduleJobAlpha1Async(envelope, callOptions);
}
catch (RpcException ex)
{
throw new DaprException(
"Schedule job operation failed: the Dapr endpoint indicated a failure. See InnerException for details.",
ex);
}
}

/// <summary>
/// Schedules a one-time job.
/// </summary>
/// <param name="jobName">The name of the job being scheduled.</param>
/// <param name="scheduledTime">The point in time when the job should be run.</param>
/// <param name="payload">Stores the main payload of the job which is passed to the trigger function.</param>
/// <param name="cancellationToken">Cancellation token.</param>
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public override async Task ScheduleOneTimeJobAsync(string jobName, DateTimeOffset scheduledTime,
ReadOnlyMemory<byte>? payload = null,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(jobName))
throw new ArgumentNullException(nameof(jobName));

var job = new Autogenerated.Job { Name = jobName, DueTime = scheduledTime.ToString("O") };

if (payload is not null)
job.Data = job.Data = new Any { Value = ByteString.CopyFrom(payload.Value.Span), TypeUrl = "dapr.io/schedule/jobpayload" };

var envelope = new Autogenerated.ScheduleJobRequest { Job = job };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace Dapr.Jobs.Extensions.Helpers.Deserialization;
/// </summary>
public static class ByteArrayDeserializationExtensions
{
/// <summary>
/// Local JSON serializer option defaults.
/// </summary>
private static readonly JsonSerializerOptions defaultOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);

/// <summary>
/// Deserializes an array of UTF-8 encoded bytes to a string.
/// </summary>
Expand All @@ -25,7 +30,7 @@ public static class ByteArrayDeserializationExtensions
public static TJsonObject? DeserializeFromJsonBytes<TJsonObject>(this ReadOnlySpan<byte> bytes,
JsonSerializerOptions? jsonSerializerOptions = null)
{
var serializerOptions = jsonSerializerOptions ?? new JsonSerializerOptions(JsonSerializerDefaults.Web);
var serializerOptions = jsonSerializerOptions ?? defaultOptions;
return JsonSerializer.Deserialize<TJsonObject>(bytes, serializerOptions);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit b8fd743

Please sign in to comment.