-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from atc-net/feature/BackgroundScheduleService…
…Base Add BackgroundScheduleServiceBase<T>
- Loading branch information
Showing
14 changed files
with
578 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
sample/Atc.Hosting.TimeFile.Sample/TimeFileScheduleWorker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
namespace Atc.Hosting.TimeFile.Sample; | ||
|
||
public class TimeFileScheduleWorker : BackgroundScheduleServiceBase<TimeFileScheduleWorker> | ||
{ | ||
private readonly ITimeProvider timeProvider; | ||
|
||
private readonly TimeFileScheduleWorkerOptions workerOptions; | ||
|
||
public TimeFileScheduleWorker( | ||
ILogger<TimeFileScheduleWorker> logger, | ||
IBackgroundServiceHealthService healthService, | ||
ITimeProvider timeProvider, | ||
IOptions<TimeFileScheduleWorkerOptions> workerOptions) | ||
: base( | ||
logger, | ||
workerOptions.Value, | ||
healthService) | ||
{ | ||
this.timeProvider = timeProvider; | ||
this.workerOptions = workerOptions.Value; | ||
} | ||
|
||
public override Task StartAsync( | ||
CancellationToken cancellationToken) | ||
{ | ||
return base.StartAsync(cancellationToken); | ||
} | ||
|
||
public override Task StopAsync( | ||
CancellationToken cancellationToken) | ||
{ | ||
return base.StopAsync(cancellationToken); | ||
} | ||
|
||
public override Task DoWorkAsync( | ||
CancellationToken stoppingToken) | ||
{ | ||
var isServiceRunning = healthService.IsServiceRunning(nameof(TimeFileWorker)); | ||
|
||
Directory.CreateDirectory(workerOptions.OutputDirectory); | ||
|
||
var time = timeProvider.UtcNow; | ||
|
||
var outFile = Path.Combine( | ||
workerOptions.OutputDirectory, | ||
$"{nameof(TimeFileWorker)}.txt"); | ||
|
||
return File.AppendAllLinesAsync( | ||
outFile, | ||
[$"{time:yyyy-MM-dd HH:mm:ss} - {ServiceName} - IsRunning={isServiceRunning}"], | ||
stoppingToken); | ||
} | ||
|
||
protected override Task OnExceptionAsync( | ||
Exception exception, | ||
CancellationToken stoppingToken) | ||
{ | ||
if (exception is IOException or UnauthorizedAccessException) | ||
{ | ||
logger.LogCritical(exception, "Could not write file!"); | ||
return StopAsync(stoppingToken); | ||
} | ||
|
||
return base.OnExceptionAsync(exception, stoppingToken); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
sample/Atc.Hosting.TimeFile.Sample/TimeFileScheduleWorkerOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Atc.Hosting.TimeFile.Sample; | ||
|
||
public class TimeFileScheduleWorkerOptions : IBackgroundScheduleServiceOptions | ||
{ | ||
public const string SectionName = "TimeFileScheduleWorker"; | ||
|
||
public string OutputDirectory { get; set; } = Path.GetTempPath(); | ||
|
||
public string CronExpression { get; set; } = "*/5 * * * *"; | ||
|
||
public override string ToString() | ||
=> $"{nameof(OutputDirectory)}: {OutputDirectory}, {nameof(CronExpression)}: {CronExpression}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
{ | ||
"TimeFileWorker": { | ||
"OutputDirectory": "C:\\Temp\\TimeFileWorkerService", | ||
"OutputDirectory": "C:\\Temp\\TimeFileWorkerTesting", | ||
"StartupDelaySeconds": 1, | ||
"RetryCount": 3, | ||
"RepeatIntervalSeconds": 10 | ||
}, | ||
"TimeFileScheduleWorker": { | ||
"OutputDirectory": "C:\\Temp\\TimeFileWorkerTesting", | ||
"CronExpression": " */1 * * * *", | ||
"RepeatIntervalSeconds": 10 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.