From e19fdbf11e1e66bab930cd884c3b8eaa9c0254d8 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Fri, 20 Oct 2023 18:50:59 +0300 Subject: [PATCH] 1.0.3 The ExecutionTimeInSeconds parameter has been replaced with ExecutionFrequency which accepts values in the format 30s, 10m, 1h or 1d. --- Directory.Build.props | 2 +- README.md | 6 +++++ src/GoDaddyDDNS/Utils/TimeSpanUtils.cs | 37 ++++++++++++++++++++++++++ src/GoDaddyDDNS/Worker.cs | 7 ++--- src/GoDaddyDDNS/appsettings.json | 2 +- 5 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 src/GoDaddyDDNS/Utils/TimeSpanUtils.cs diff --git a/Directory.Build.props b/Directory.Build.props index 0c6ec45..e961946 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 1.0.2 + 1.0.3 Tudor Stanciu STA GoDaddyDDNS diff --git a/README.md b/README.md index 174cca9..64fec6d 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,9 @@ To make this worker functional, a minimum configuration is required: ## Stack * .NET (C#) * Docker + +## Changelog +**1.0.0** - The first release version of the service. +**1.0.1** - ConfigureAwait(false) was set for the http client get async calls. +**1.0.2** - .NET 6 upgrade. +**1.0.3** - The ```ExecutionTimeInSeconds``` parameter has been replaced with ```ExecutionFrequency``` which accepts values in the format ```30s```, ```10m```, ```1h``` or ```1d```. \ No newline at end of file diff --git a/src/GoDaddyDDNS/Utils/TimeSpanUtils.cs b/src/GoDaddyDDNS/Utils/TimeSpanUtils.cs new file mode 100644 index 0000000..d28034d --- /dev/null +++ b/src/GoDaddyDDNS/Utils/TimeSpanUtils.cs @@ -0,0 +1,37 @@ +using System; +using System.Text.RegularExpressions; + +namespace GoDaddyDDNS.Utils +{ + internal static class TimeSpanUtils + { + public static TimeSpan ParseTimeInterval(string timeInterval) + { + if (string.IsNullOrEmpty(timeInterval)) + throw new ArgumentException("Time interval cannot be null or empty."); + + var formatRegex = new Regex(@"^(\d+)([smhd])$", RegexOptions.IgnoreCase); + var match = formatRegex.Match(timeInterval); + + if (!match.Success) + throw new ArgumentException("Invalid time interval format. Use format like '30s', '10m', '1h', or '1d'."); + + var value = int.Parse(timeInterval.Substring(0, timeInterval.Length - 1)); + var unit = timeInterval.Substring(timeInterval.Length - 1).ToLower(); + + switch (unit) + { + case "s": // seconds + return TimeSpan.FromSeconds(value); + case "m": // minutes + return TimeSpan.FromMinutes(value); + case "h": // hours + return TimeSpan.FromHours(value); + case "d": // days + return TimeSpan.FromDays(value); + default: + throw new ArgumentException("Invalid time interval unit."); + } + } + } +} diff --git a/src/GoDaddyDDNS/Worker.cs b/src/GoDaddyDDNS/Worker.cs index 27922fc..f210aa2 100644 --- a/src/GoDaddyDDNS/Worker.cs +++ b/src/GoDaddyDDNS/Worker.cs @@ -1,4 +1,5 @@ using GoDaddyDDNS.Abstractions; +using GoDaddyDDNS.Utils; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -12,15 +13,15 @@ namespace GoDaddyDDNS { private readonly ILogger _logger; private readonly IDynamicDNSService _dynamicDNSService; - private readonly int _delay; + private readonly TimeSpan _delay; public Worker(ILogger logger, IDynamicDNSService dynamicDNSService, IConfiguration configuration) { _logger=logger; _dynamicDNSService=dynamicDNSService; - var delayInSeconds = configuration.GetValue("ExecutionTimeInSeconds"); - _delay = Convert.ToInt32(TimeSpan.FromSeconds(delayInSeconds).TotalMilliseconds); + var delay = configuration.GetValue("ExecutionFrequency"); + _delay = TimeSpanUtils.ParseTimeInterval(delay); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) diff --git a/src/GoDaddyDDNS/appsettings.json b/src/GoDaddyDDNS/appsettings.json index 6881c7f..e2ec8bc 100644 --- a/src/GoDaddyDDNS/appsettings.json +++ b/src/GoDaddyDDNS/appsettings.json @@ -17,5 +17,5 @@ "Records": [ "@" ], "Key": "*********", "Secret": "*********", - "ExecutionTimeInSeconds": 60 + "ExecutionFrequency": "60s" }