Skip to content

Commit e0e9aec

Browse files
committed
Merge branch 'v3'
2 parents 4cf6281 + 4b957e6 commit e0e9aec

File tree

10 files changed

+27
-17
lines changed

10 files changed

+27
-17
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ public class MyFirstTask : IRunnable
3737
this.logger = logger;
3838
}
3939

40-
public void Run()
40+
public void Run(ITask currentTask)
4141
{
4242
// Place your code here
4343
}
4444
}
4545
```
4646

47+
You can add any parameters to constructor, while they are resolvable from DI container (including scope-lifetime services, because new scope is created for every task run).
48+
4749
### 2. Register and start your task in `Startup.cs`
4850

4951

@@ -65,8 +67,6 @@ public void Configure(IApplicationBuilder app, ...)
6567

6668
And viola! Your task will run every 5 minutes. Until you application alive, of course.
6769

68-
You can add any parameters to constructor, while they are resolvable from DI container.
69-
7070
## Installation
7171

7272
Use NuGet package [RecurrentTasks](https://www.nuget.org/packages/RecurrentTasks/)

sample/RecurrentTasks.Sample/SampleTask.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ public SampleTask(ILogger<SampleTask> logger, SampleTaskRunHistory runHistory)
1515
this.runHistory = runHistory;
1616
}
1717

18-
public void Run(TaskRunStatus taskRunStatus)
18+
public void Run(ITask currentTask)
1919
{
2020
var msg = string.Format("Run at: {0}", DateTimeOffset.Now);
2121
runHistory.Messages.Add(msg);
2222
logger.LogDebug(msg);
23+
24+
// You can change interval for [all] next runs!
25+
currentTask.Interval = currentTask.Interval.Add(TimeSpan.FromSeconds(1));
2326
}
2427
}
2528
}

sample/RecurrentTasks.Sample/Views/Home/Index.cshtml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
<dt>RunStatus.LastRunTime</dt>
1616
<dd>@myTask.RunStatus.LastRunTime</dd>
1717

18+
<dt>Interval</dt>
19+
<dd>@myTask.Interval</dd>
20+
1821
<dt>RunStatus.NextRunTime</dt>
1922
<dd>@myTask.RunStatus.NextRunTime (in @(myTask.RunStatus.NextRunTime.Subtract(DateTime.Now)))</dd>
2023
</dl>

src/RecurrentTasks/IRunnable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
public interface IRunnable
44
{
5-
void Run(TaskRunStatus taskRunStatus);
5+
void Run(ITask currentTask);
66
}
77
}

src/RecurrentTasks/RecurrentTasksApplicationBuilderExtensions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ public static class RecurrentTasksApplicationBuilderExtensions
99
public static void StartTask<TRunnable>(this IApplicationBuilder app, TimeSpan interval)
1010
where TRunnable : IRunnable
1111
{
12-
StartTask<TRunnable>(app, interval, TimeSpan.FromSeconds(new Random().Next(10, 30)));
12+
StartTask<TRunnable>(app, t => { t.Interval = interval; });
1313
}
1414

1515
public static void StartTask<TRunnable>(this IApplicationBuilder app, TimeSpan interval, TimeSpan initialTimeout)
1616
where TRunnable : IRunnable
1717
{
18-
StartTask<TRunnable>(app, t => { t.Interval = interval; }, TimeSpan.FromSeconds(new Random().Next(10, 30)));
18+
StartTask<TRunnable>(app, t => { t.Interval = interval; }, initialTimeout);
19+
}
20+
21+
public static void StartTask<TRunnable>(this IApplicationBuilder app, Action<ITask> setupAction)
22+
where TRunnable : IRunnable
23+
{
24+
StartTask<TRunnable>(app, setupAction, TimeSpan.FromSeconds(new Random().Next(10, 30)));
1925
}
2026

2127
public static void StartTask<TRunnable>(this IApplicationBuilder app, Action<ITask> setupAction, TimeSpan initialTimeout)

src/RecurrentTasks/TaskRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected void MainLoop(TimeSpan firstRunDelay)
131131
var runnable = (TRunnable) scope.ServiceProvider.GetRequiredService(typeof(TRunnable));
132132

133133
logger.LogInformation("Calling Run()...");
134-
runnable.Run(RunStatus);
134+
runnable.Run(this);
135135
logger.LogInformation("Done.");
136136

137137
RunStatus.LastRunTime = startTime;

src/RecurrentTasks/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "3.0.0-beta1",
2+
"version": "3.0.0-beta2",
33
"title": "RecurrentTasks",
44
"copyright": "Dmitry Popov, 2016",
55
"packOptions": {

test/RecurrentTasks.Tests/SampleTask.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace RecurrentTasks
22
{
33
using System;
4-
using System.Threading;
54

65
public class SampleTask : IRunnable
76
{
@@ -12,7 +11,7 @@ public SampleTask(SampleTaskSettings settings)
1211
this.settings = settings;
1312
}
1413

15-
public void Run(TaskRunStatus taskRunStatus)
14+
public void Run(ITask currentTask)
1615
{
1716
settings.TaskRunCalled.Set();
1817
if (settings.MustThrowError)

test/RecurrentTasks.Tests/TaskRunnerTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Microsoft.Extensions.Logging;
55
using Microsoft.Extensions.DependencyInjection;
66
using Xunit;
7-
using System.Threading;
87

98
public class TaskRunnerTests : IDisposable
109
{
@@ -199,7 +198,7 @@ public void Task_AfterRunFailGeneratedAfterException()
199198
sampleTask.Start(TimeSpan.FromSeconds(1));
200199

201200
// waiting 2 seconds max, then failing
202-
Assert.True(settings.TaskRunCalled.Wait(TimeSpan.FromSeconds(2)));
201+
Assert.True(settings.TaskRunCalled.Wait(TimeSpan.FromSeconds(3)));
203202

204203
System.Threading.Thread.Sleep(200); // wait for run cycle completed
205204

test/RecurrentTasks.Tests/project.lock.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,7 +2662,7 @@
26622662
"lib/netstandard1.1/xunit.runner.utility.dotnet.dll": {}
26632663
}
26642664
},
2665-
"RecurrentTasks/3.0.0": {
2665+
"RecurrentTasks/3.0.0-beta1": {
26662666
"type": "project",
26672667
"framework": ".NETStandard,Version=v1.3",
26682668
"dependencies": {
@@ -3348,7 +3348,7 @@
33483348
"lib/net45/xunit.runner.utility.desktop.dll": {}
33493349
}
33503350
},
3351-
"RecurrentTasks/3.0.0": {
3351+
"RecurrentTasks/3.0.0-beta1": {
33523352
"type": "project",
33533353
"framework": ".NETFramework,Version=v4.5.1",
33543354
"dependencies": {
@@ -4036,7 +4036,7 @@
40364036
"lib/net45/xunit.runner.utility.desktop.dll": {}
40374037
}
40384038
},
4039-
"RecurrentTasks/3.0.0": {
4039+
"RecurrentTasks/3.0.0-beta1": {
40404040
"type": "project",
40414041
"framework": ".NETFramework,Version=v4.6",
40424042
"dependencies": {
@@ -9190,7 +9190,7 @@
91909190
"xunit.runner.utility.nuspec"
91919191
]
91929192
},
9193-
"RecurrentTasks/3.0.0": {
9193+
"RecurrentTasks/3.0.0-beta1": {
91949194
"type": "project",
91959195
"path": "../../src/RecurrentTasks/project.json",
91969196
"msbuildProject": "../../src/RecurrentTasks/RecurrentTasks.xproj"

0 commit comments

Comments
 (0)