Skip to content

Commit 6747ac6

Browse files
committed
feat: Add TimeFileScheduleWorker and let the sample-app use it
1 parent b035166 commit 6747ac6

File tree

5 files changed

+95
-5
lines changed

5 files changed

+95
-5
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
.ConfigureServices(services =>
1010
{
1111
services.AddSingleton<ITimeProvider, SystemTimeProvider>();
12+
1213
services.Configure<TimeFileWorkerOptions>(configuration.GetSection(TimeFileWorkerOptions.SectionName));
1314
services.AddHostedService<TimeFileWorker>();
1415

16+
services.Configure<TimeFileScheduleWorkerOptions>(configuration.GetSection(TimeFileScheduleWorkerOptions.SectionName));
17+
services.AddHostedService<TimeFileScheduleWorker>();
18+
1519
services.AddSingleton<IBackgroundServiceHealthService, BackgroundServiceHealthService>(s =>
1620
{
1721
var healthService = new BackgroundServiceHealthService(s.GetRequiredService<ITimeProvider>());
@@ -24,4 +28,4 @@
2428
})
2529
.Build();
2630

27-
host.Run();
31+
await host.RunAsync();
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
namespace Atc.Hosting.TimeFile.Sample;
2+
3+
public class TimeFileScheduleWorker : BackgroundScheduleServiceBase<TimeFileScheduleWorker>
4+
{
5+
private readonly ITimeProvider timeProvider;
6+
7+
private readonly TimeFileScheduleWorkerOptions workerOptions;
8+
9+
public TimeFileScheduleWorker(
10+
ILogger<TimeFileScheduleWorker> logger,
11+
IBackgroundServiceHealthService healthService,
12+
ITimeProvider timeProvider,
13+
IOptions<TimeFileScheduleWorkerOptions> workerOptions)
14+
: base(
15+
logger,
16+
workerOptions.Value,
17+
healthService)
18+
{
19+
this.timeProvider = timeProvider;
20+
this.workerOptions = workerOptions.Value;
21+
}
22+
23+
public override Task StartAsync(
24+
CancellationToken cancellationToken)
25+
{
26+
return base.StartAsync(cancellationToken);
27+
}
28+
29+
public override Task StopAsync(
30+
CancellationToken cancellationToken)
31+
{
32+
return base.StopAsync(cancellationToken);
33+
}
34+
35+
public override Task DoWorkAsync(
36+
CancellationToken stoppingToken)
37+
{
38+
var isServiceRunning = healthService.IsServiceRunning(nameof(TimeFileWorker));
39+
40+
Directory.CreateDirectory(workerOptions.OutputDirectory);
41+
42+
var time = timeProvider.UtcNow;
43+
44+
var outFile = Path.Combine(
45+
workerOptions.OutputDirectory,
46+
$"{nameof(TimeFileWorker)}.txt");
47+
48+
return File.AppendAllLinesAsync(
49+
outFile,
50+
[$"{time:yyyy-MM-dd HH:mm:ss} - {ServiceName} - IsRunning={isServiceRunning}"],
51+
stoppingToken);
52+
}
53+
54+
protected override Task OnExceptionAsync(
55+
Exception exception,
56+
CancellationToken stoppingToken)
57+
{
58+
if (exception is IOException or UnauthorizedAccessException)
59+
{
60+
logger.LogCritical(exception, "Could not write file!");
61+
return StopAsync(stoppingToken);
62+
}
63+
64+
return base.OnExceptionAsync(exception, stoppingToken);
65+
}
66+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Atc.Hosting.TimeFile.Sample;
2+
3+
public class TimeFileScheduleWorkerOptions : IBackgroundScheduleServiceOptions
4+
{
5+
public const string SectionName = "TimeFileScheduleWorker";
6+
7+
public string OutputDirectory { get; set; } = Path.GetTempPath();
8+
9+
public string CronExpression { get; set; } = "*/5 * * * *";
10+
11+
public override string ToString()
12+
=> $"{nameof(OutputDirectory)}: {OutputDirectory}, {nameof(CronExpression)}: {CronExpression}";
13+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ public override Task DoWorkAsync(
4343

4444
var outFile = Path.Combine(
4545
workerOptions.OutputDirectory,
46-
$"{time:yyyy-MM-dd--HHmmss}-{isServiceRunning}.txt");
46+
$"{nameof(TimeFileWorker)}.txt");
4747

48-
return File.WriteAllTextAsync(outFile, $"{ServiceName}-{isServiceRunning}", stoppingToken);
48+
return File.AppendAllLinesAsync(
49+
outFile,
50+
[$"{time:yyyy-MM-dd HH:mm:ss} - {ServiceName} - IsRunning={isServiceRunning}"],
51+
stoppingToken);
4952
}
5053

5154
protected override Task OnExceptionAsync(
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{
22
"TimeFileWorker": {
3-
"OutputDirectory": "C:\\Temp\\TimeFileWorkerService",
3+
"OutputDirectory": "C:\\Temp\\TimeFileWorkerTesting",
44
"StartupDelaySeconds": 1,
5-
"RetryCount": 3,
5+
"RepeatIntervalSeconds": 10
6+
},
7+
"TimeFileScheduleWorker": {
8+
"OutputDirectory": "C:\\Temp\\TimeFileWorkerTesting",
9+
"CronExpression": " */1 * * * *",
610
"RepeatIntervalSeconds": 10
711
}
812
}

0 commit comments

Comments
 (0)