Skip to content

Commit

Permalink
Added braces for clarity
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 Nov 1, 2024
1 parent 76f1295 commit 1e2a02e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/Dapr.Jobs/CronExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,14 @@ public CronExpressionBuilder On(params DayOfWeek[] days)
public CronExpressionBuilder Through(ThroughCronPeriod period, int from, int to)
{
if (from > to)
{
throw new ArgumentException("The date representing the From property should precede the To property");
}

if (from == to)
{
throw new ArgumentException("The From and To properties should not be equivalent");
}

var stringValue = $"{from}-{to}";

Expand Down Expand Up @@ -145,9 +150,14 @@ public CronExpressionBuilder Through(ThroughCronPeriod period, int from, int to)
public CronExpressionBuilder Through(DayOfWeek from, DayOfWeek to)
{
if (from > to)
{
throw new ArgumentException("The day representing the From property should precede the To property");
}

if (from == to)
{
throw new ArgumentException("The From and To properties should not be equivalent");
}

dayOfWeek = $"{from.GetValueFromEnumMember()}-{to.GetValueFromEnumMember()}";
return this;
Expand All @@ -161,9 +171,14 @@ public CronExpressionBuilder Through(DayOfWeek from, DayOfWeek to)
public CronExpressionBuilder Through(MonthOfYear from, MonthOfYear to)
{
if (from > to)
{
throw new ArgumentException("The month representing the From property should precede the To property");
}

if (from == to)
{
throw new ArgumentException("The From and To properties should not be equivalent");
}

month = $"{from.GetValueFromEnumMember()}-{to.GetValueFromEnumMember()}";
return this;
Expand Down Expand Up @@ -210,7 +225,9 @@ public CronExpressionBuilder Each(CronPeriod period)
public CronExpressionBuilder Every(EveryCronPeriod period, int interval)
{
if (interval < 0)
{
throw new ArgumentOutOfRangeException(nameof(interval));
}

var value = $"*/{interval}";

Expand Down
2 changes: 2 additions & 0 deletions src/Dapr.Jobs/DaprJobsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public abstract Task ScheduleJobAsync(string jobName, DaprJobSchedule schedule,
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
14 changes: 14 additions & 0 deletions src/Dapr.Jobs/DaprJobsGrpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,32 @@ public override async Task ScheduleJobAsync(string jobName, DaprJobSchedule sche
var job = new Autogenerated.Job { Name = jobName, Schedule = schedule.ExpressionValue };

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 = 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, {nameof(ttl)} must represent a later point in time");
}

job.Ttl = ((DateTimeOffset)ttl).ToString("O");
}
Expand Down Expand Up @@ -131,7 +139,9 @@ public override async Task ScheduleJobAsync(string jobName, DaprJobSchedule sche
public override async Task<DaprJobDetails> GetJobAsync(string jobName, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(jobName))
{
throw new ArgumentNullException(nameof(jobName));
}

try
{
Expand Down Expand Up @@ -173,7 +183,9 @@ public override async Task<DaprJobDetails> GetJobAsync(string jobName, Cancellat
public override async Task DeleteJobAsync(string jobName, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(jobName))
{
throw new ArgumentNullException(nameof(jobName));
}

try
{
Expand Down Expand Up @@ -212,7 +224,9 @@ private CallOptions CreateCallOptions(Metadata? headers, CancellationToken cance
callOptions.Headers!.Add("User-Agent", this.userAgent);

if (apiTokenHeader is not null)
{
callOptions.Headers.Add(apiTokenHeader.Value.Key, apiTokenHeader.Value.Value);
}

return callOptions;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Dapr.Jobs/Extensions/EndpointRouteBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public static IEndpointRouteBuilder MapDaprScheduledJobHandler(this IEndpointRou
parameters.Add(cancellationToken);
var result = action.DynamicInvoke(parameters.ToArray());
if (result is Task task) await task;
if (result is Task task)
{
await task;
}
});

return endpoints;
Expand Down
16 changes: 16 additions & 0 deletions src/Dapr.Jobs/Extensions/TimeSpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,24 @@ public static string ToDurationString(this TimeSpan timespan)

//Hours is the largest unit of measure in the duration string
if (timespan.Hours > 0)
{
sb.Append($"{timespan.Hours}h");
}

if (timespan.Minutes > 0)
{
sb.Append($"{timespan.Minutes}m");
}

if (timespan.Seconds > 0)
{
sb.Append($"{timespan.Seconds}s");
}

if (timespan.Milliseconds > 0)
{
sb.Append($"{timespan.Milliseconds}ms");
}

return sb.ToString();
}
Expand Down Expand Up @@ -82,19 +90,27 @@ public static TimeSpan FromDurationString(this string interval)

var hourMatch = hourRegex.Match(interval);
if (hourMatch.Success)
{
hours = int.Parse(hourMatch.Groups[1].Value);
}

var minuteMatch = minuteRegex.Match(interval);
if (minuteMatch.Success)
{
minutes = int.Parse(minuteMatch.Groups[1].Value);
}

var secondMatch = secondRegex.Match(interval);
if (secondMatch.Success)
{
seconds = int.Parse(secondMatch.Groups[1].Value);
}

var millisecondMatch = millisecondRegex.Match(interval);
if (millisecondMatch.Success)
{
milliseconds = int.Parse(millisecondMatch.Groups[1].Value);
}

return new TimeSpan(0, hours, minutes, seconds, milliseconds);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Dapr.Jobs/JsonConverters/Iso8601DateTimeJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ public sealed class Iso8601DateTimeJsonConverter : JsonConverter<DateTimeOffset?
public override DateTimeOffset? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}

var dateString = reader.GetString();
if (DateTimeOffset.TryParse(dateString, out var dateTimeOffset))
{
return dateTimeOffset;
}

throw new JsonException($"Unable to convert \"{dateString}\" to {nameof(DateTimeOffset)}");
}
Expand Down

0 comments on commit 1e2a02e

Please sign in to comment.