From 47bc37a663e790edf9c55dd928c2a53d7ce61434 Mon Sep 17 00:00:00 2001 From: Per Kops Date: Thu, 12 Dec 2024 11:16:05 +0100 Subject: [PATCH] feat!: add optional servicename to backgroundserviceoptions --- .../TimeFileScheduleWorkerOptions.cs | 4 +++- .../TimeFileWorkerOptions.cs | 4 +++- sample/Atc.Hosting.TimeFile.Sample/appsettings.json | 1 + src/Atc.Hosting/BackgroundScheduleServiceBase.cs | 2 +- src/Atc.Hosting/BackgroundServiceBase.cs | 2 +- .../DefaultBackgroundScheduleServiceOptions.cs | 8 ++++++++ src/Atc.Hosting/DefaultBackgroundServiceOptions.cs | 10 +++++++++- src/Atc.Hosting/IBackgroundScheduleServiceOptions.cs | 2 +- src/Atc.Hosting/IBackgroundServiceBaseOptions.cs | 6 ++++++ src/Atc.Hosting/IBackgroundServiceOptions.cs | 2 +- .../XUnitTestTypes/MyServiceOptions.cs | 4 +++- 11 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 src/Atc.Hosting/IBackgroundServiceBaseOptions.cs diff --git a/sample/Atc.Hosting.TimeFile.Sample/TimeFileScheduleWorkerOptions.cs b/sample/Atc.Hosting.TimeFile.Sample/TimeFileScheduleWorkerOptions.cs index 89cf9fe..7b0aa5a 100644 --- a/sample/Atc.Hosting.TimeFile.Sample/TimeFileScheduleWorkerOptions.cs +++ b/sample/Atc.Hosting.TimeFile.Sample/TimeFileScheduleWorkerOptions.cs @@ -8,6 +8,8 @@ public class TimeFileScheduleWorkerOptions : IBackgroundScheduleServiceOptions public string CronExpression { get; set; } = "*/5 * * * *"; + public string? ServiceName { get; set; } + public override string ToString() - => $"{nameof(OutputDirectory)}: {OutputDirectory}, {nameof(CronExpression)}: {CronExpression}"; + => $"{nameof(OutputDirectory)}: {OutputDirectory}, {nameof(CronExpression)}: {CronExpression}, {nameof(ServiceName)}: {ServiceName}"; } \ No newline at end of file diff --git a/sample/Atc.Hosting.TimeFile.Sample/TimeFileWorkerOptions.cs b/sample/Atc.Hosting.TimeFile.Sample/TimeFileWorkerOptions.cs index 1dc1a70..819db6f 100644 --- a/sample/Atc.Hosting.TimeFile.Sample/TimeFileWorkerOptions.cs +++ b/sample/Atc.Hosting.TimeFile.Sample/TimeFileWorkerOptions.cs @@ -10,6 +10,8 @@ public class TimeFileWorkerOptions : IBackgroundServiceOptions public ushort RepeatIntervalSeconds { get; set; } = 20; + public string? ServiceName { get; set; } + public override string ToString() - => $"{nameof(OutputDirectory)}: {OutputDirectory}, {nameof(StartupDelaySeconds)}: {StartupDelaySeconds}, {nameof(RepeatIntervalSeconds)}: {RepeatIntervalSeconds}"; + => $"{nameof(OutputDirectory)}: {OutputDirectory}, {nameof(StartupDelaySeconds)}: {StartupDelaySeconds}, {nameof(RepeatIntervalSeconds)}: {RepeatIntervalSeconds}, {nameof(ServiceName)}: {ServiceName}"; } \ No newline at end of file diff --git a/sample/Atc.Hosting.TimeFile.Sample/appsettings.json b/sample/Atc.Hosting.TimeFile.Sample/appsettings.json index c046dcf..8bf2a82 100644 --- a/sample/Atc.Hosting.TimeFile.Sample/appsettings.json +++ b/sample/Atc.Hosting.TimeFile.Sample/appsettings.json @@ -7,5 +7,6 @@ "TimeFileScheduleWorker": { "OutputDirectory": "C:\\Temp\\TimeFileWorkerTesting", "CronExpression": " */1 * * * *", + "ServiceName": "time-file-worker-service-custom-name" } } \ No newline at end of file diff --git a/src/Atc.Hosting/BackgroundScheduleServiceBase.cs b/src/Atc.Hosting/BackgroundScheduleServiceBase.cs index 612d57a..9e172e5 100644 --- a/src/Atc.Hosting/BackgroundScheduleServiceBase.cs +++ b/src/Atc.Hosting/BackgroundScheduleServiceBase.cs @@ -16,7 +16,7 @@ protected BackgroundScheduleServiceBase( this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); ServiceOptions = backgroundScheduleServiceOptions ?? throw new ArgumentNullException(nameof(backgroundScheduleServiceOptions)); this.healthService = healthService ?? throw new ArgumentNullException(nameof(healthService)); - ServiceName = typeof(T).Name; + ServiceName = backgroundScheduleServiceOptions.ServiceName ?? typeof(T).Name; } /// diff --git a/src/Atc.Hosting/BackgroundServiceBase.cs b/src/Atc.Hosting/BackgroundServiceBase.cs index beccc56..7f88d88 100644 --- a/src/Atc.Hosting/BackgroundServiceBase.cs +++ b/src/Atc.Hosting/BackgroundServiceBase.cs @@ -21,7 +21,7 @@ protected BackgroundServiceBase( this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); ServiceOptions = backgroundServiceOptions ?? throw new ArgumentNullException(nameof(backgroundServiceOptions)); this.healthService = healthService ?? throw new ArgumentNullException(nameof(healthService)); - ServiceName = typeof(T).Name; + ServiceName = backgroundServiceOptions.ServiceName ?? typeof(T).Name; } /// diff --git a/src/Atc.Hosting/DefaultBackgroundScheduleServiceOptions.cs b/src/Atc.Hosting/DefaultBackgroundScheduleServiceOptions.cs index bc6ba06..8970b71 100644 --- a/src/Atc.Hosting/DefaultBackgroundScheduleServiceOptions.cs +++ b/src/Atc.Hosting/DefaultBackgroundScheduleServiceOptions.cs @@ -9,4 +9,12 @@ public class DefaultBackgroundScheduleServiceOptions : IBackgroundScheduleServic /// Gets the cron expression for scheduling - the default value is "*/5 * * * *" (every 5 minutes). /// public string CronExpression { get; set; } = "*/5 * * * *"; + + /// + /// Gets or sets the name of the service. + /// + /// + /// The name of the service. + /// + public string? ServiceName { get; set; } } \ No newline at end of file diff --git a/src/Atc.Hosting/DefaultBackgroundServiceOptions.cs b/src/Atc.Hosting/DefaultBackgroundServiceOptions.cs index 2b88651..327c757 100644 --- a/src/Atc.Hosting/DefaultBackgroundServiceOptions.cs +++ b/src/Atc.Hosting/DefaultBackgroundServiceOptions.cs @@ -15,7 +15,15 @@ public class DefaultBackgroundServiceOptions : IBackgroundServiceOptions /// public ushort RepeatIntervalSeconds { get; set; } = 30; + /// + /// Gets or sets the name of the service. + /// + /// + /// The name of the service. + /// + public string? ServiceName { get; set; } + /// public override string ToString() - => $"{nameof(StartupDelaySeconds)}: {StartupDelaySeconds}, {nameof(RepeatIntervalSeconds)}: {RepeatIntervalSeconds}"; + => $"{nameof(StartupDelaySeconds)}: {StartupDelaySeconds}, {nameof(RepeatIntervalSeconds)}: {RepeatIntervalSeconds}, {nameof(ServiceName)}: {ServiceName}"; } \ No newline at end of file diff --git a/src/Atc.Hosting/IBackgroundScheduleServiceOptions.cs b/src/Atc.Hosting/IBackgroundScheduleServiceOptions.cs index df253cc..96e7268 100644 --- a/src/Atc.Hosting/IBackgroundScheduleServiceOptions.cs +++ b/src/Atc.Hosting/IBackgroundScheduleServiceOptions.cs @@ -36,7 +36,7 @@ namespace Atc.Hosting; /// /// /// -public interface IBackgroundScheduleServiceOptions +public interface IBackgroundScheduleServiceOptions : IBackgroundServiceBaseOptions { /// /// Gets the cron expression for scheduling. diff --git a/src/Atc.Hosting/IBackgroundServiceBaseOptions.cs b/src/Atc.Hosting/IBackgroundServiceBaseOptions.cs new file mode 100644 index 0000000..a915ede --- /dev/null +++ b/src/Atc.Hosting/IBackgroundServiceBaseOptions.cs @@ -0,0 +1,6 @@ +namespace Atc.Hosting; + +public interface IBackgroundServiceBaseOptions +{ + public string? ServiceName { get; set; } +} \ No newline at end of file diff --git a/src/Atc.Hosting/IBackgroundServiceOptions.cs b/src/Atc.Hosting/IBackgroundServiceOptions.cs index 8c06202..09c5c30 100644 --- a/src/Atc.Hosting/IBackgroundServiceOptions.cs +++ b/src/Atc.Hosting/IBackgroundServiceOptions.cs @@ -3,7 +3,7 @@ namespace Atc.Hosting; /// /// The interface definition for BackgroundServiceOptions used in . /// -public interface IBackgroundServiceOptions +public interface IBackgroundServiceOptions : IBackgroundServiceBaseOptions { /// /// Defines the delay period before the first unit of work. diff --git a/test/Atc.Hosting.Tests/XUnitTestTypes/MyServiceOptions.cs b/test/Atc.Hosting.Tests/XUnitTestTypes/MyServiceOptions.cs index eae5b4e..20c1f5f 100644 --- a/test/Atc.Hosting.Tests/XUnitTestTypes/MyServiceOptions.cs +++ b/test/Atc.Hosting.Tests/XUnitTestTypes/MyServiceOptions.cs @@ -6,6 +6,8 @@ public class MyServiceOptions : IBackgroundServiceOptions public ushort RepeatIntervalSeconds { get; set; } = 1; + public string? ServiceName { get; set; } + public override string ToString() - => $"{nameof(StartupDelaySeconds)}: {StartupDelaySeconds}, {nameof(RepeatIntervalSeconds)}: {RepeatIntervalSeconds}"; + => $"{nameof(StartupDelaySeconds)}: {StartupDelaySeconds}, {nameof(RepeatIntervalSeconds)}: {RepeatIntervalSeconds}, {nameof(ServiceName)}: {ServiceName}"; } \ No newline at end of file