Skip to content

Commit 47bc37a

Browse files
author
Per Kops
committed
feat!: add optional servicename to backgroundserviceoptions
1 parent 2658030 commit 47bc37a

11 files changed

+37
-8
lines changed

sample/Atc.Hosting.TimeFile.Sample/TimeFileScheduleWorkerOptions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public class TimeFileScheduleWorkerOptions : IBackgroundScheduleServiceOptions
88

99
public string CronExpression { get; set; } = "*/5 * * * *";
1010

11+
public string? ServiceName { get; set; }
12+
1113
public override string ToString()
12-
=> $"{nameof(OutputDirectory)}: {OutputDirectory}, {nameof(CronExpression)}: {CronExpression}";
14+
=> $"{nameof(OutputDirectory)}: {OutputDirectory}, {nameof(CronExpression)}: {CronExpression}, {nameof(ServiceName)}: {ServiceName}";
1315
}

sample/Atc.Hosting.TimeFile.Sample/TimeFileWorkerOptions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class TimeFileWorkerOptions : IBackgroundServiceOptions
1010

1111
public ushort RepeatIntervalSeconds { get; set; } = 20;
1212

13+
public string? ServiceName { get; set; }
14+
1315
public override string ToString()
14-
=> $"{nameof(OutputDirectory)}: {OutputDirectory}, {nameof(StartupDelaySeconds)}: {StartupDelaySeconds}, {nameof(RepeatIntervalSeconds)}: {RepeatIntervalSeconds}";
16+
=> $"{nameof(OutputDirectory)}: {OutputDirectory}, {nameof(StartupDelaySeconds)}: {StartupDelaySeconds}, {nameof(RepeatIntervalSeconds)}: {RepeatIntervalSeconds}, {nameof(ServiceName)}: {ServiceName}";
1517
}

sample/Atc.Hosting.TimeFile.Sample/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"TimeFileScheduleWorker": {
88
"OutputDirectory": "C:\\Temp\\TimeFileWorkerTesting",
99
"CronExpression": " */1 * * * *",
10+
"ServiceName": "time-file-worker-service-custom-name"
1011
}
1112
}

src/Atc.Hosting/BackgroundScheduleServiceBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected BackgroundScheduleServiceBase(
1616
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
1717
ServiceOptions = backgroundScheduleServiceOptions ?? throw new ArgumentNullException(nameof(backgroundScheduleServiceOptions));
1818
this.healthService = healthService ?? throw new ArgumentNullException(nameof(healthService));
19-
ServiceName = typeof(T).Name;
19+
ServiceName = backgroundScheduleServiceOptions.ServiceName ?? typeof(T).Name;
2020
}
2121

2222
/// <summary>

src/Atc.Hosting/BackgroundServiceBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected BackgroundServiceBase(
2121
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
2222
ServiceOptions = backgroundServiceOptions ?? throw new ArgumentNullException(nameof(backgroundServiceOptions));
2323
this.healthService = healthService ?? throw new ArgumentNullException(nameof(healthService));
24-
ServiceName = typeof(T).Name;
24+
ServiceName = backgroundServiceOptions.ServiceName ?? typeof(T).Name;
2525
}
2626

2727
/// <summary>

src/Atc.Hosting/DefaultBackgroundScheduleServiceOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,12 @@ public class DefaultBackgroundScheduleServiceOptions : IBackgroundScheduleServic
99
/// Gets the cron expression for scheduling - the default value is "*/5 * * * *" (every 5 minutes).
1010
/// </summary>
1111
public string CronExpression { get; set; } = "*/5 * * * *";
12+
13+
/// <summary>
14+
/// Gets or sets the name of the service.
15+
/// </summary>
16+
/// <value>
17+
/// The name of the service.
18+
/// </value>
19+
public string? ServiceName { get; set; }
1220
}

src/Atc.Hosting/DefaultBackgroundServiceOptions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ public class DefaultBackgroundServiceOptions : IBackgroundServiceOptions
1515
/// </summary>
1616
public ushort RepeatIntervalSeconds { get; set; } = 30;
1717

18+
/// <summary>
19+
/// Gets or sets the name of the service.
20+
/// </summary>
21+
/// <value>
22+
/// The name of the service.
23+
/// </value>
24+
public string? ServiceName { get; set; }
25+
1826
/// <inheritdoc />
1927
public override string ToString()
20-
=> $"{nameof(StartupDelaySeconds)}: {StartupDelaySeconds}, {nameof(RepeatIntervalSeconds)}: {RepeatIntervalSeconds}";
28+
=> $"{nameof(StartupDelaySeconds)}: {StartupDelaySeconds}, {nameof(RepeatIntervalSeconds)}: {RepeatIntervalSeconds}, {nameof(ServiceName)}: {ServiceName}";
2129
}

src/Atc.Hosting/IBackgroundScheduleServiceOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace Atc.Hosting;
3636
/// </item>
3737
/// </list>
3838
/// </remarks>
39-
public interface IBackgroundScheduleServiceOptions
39+
public interface IBackgroundScheduleServiceOptions : IBackgroundServiceBaseOptions
4040
{
4141
/// <summary>
4242
/// Gets the cron expression for scheduling.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Atc.Hosting;
2+
3+
public interface IBackgroundServiceBaseOptions
4+
{
5+
public string? ServiceName { get; set; }
6+
}

src/Atc.Hosting/IBackgroundServiceOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace Atc.Hosting;
33
/// <summary>
44
/// The interface definition for BackgroundServiceOptions used in <see cref="BackgroundServiceBase{T}"/>.
55
/// </summary>
6-
public interface IBackgroundServiceOptions
6+
public interface IBackgroundServiceOptions : IBackgroundServiceBaseOptions
77
{
88
/// <summary>
99
/// Defines the delay period before the first unit of work.

0 commit comments

Comments
 (0)