Skip to content

Commit

Permalink
v3.0.0-beta2: IRunnable.Run changed!
Browse files Browse the repository at this point in the history
  • Loading branch information
justdmitry committed Oct 22, 2016
1 parent 38f378f commit 4b957e6
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -37,13 +37,15 @@ public class MyFirstTask : IRunnable
this.logger = logger;
}

public void Run(TaskRunStatus taskRunStatus)
public void Run(ITask currentTask)
{
// Place your code here
}
}
```

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).

### 2. Register and start your task in `Startup.cs`


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

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

You can add any parameters to constructor, while they are resolvable from DI container.

## Installation

Use NuGet package [RecurrentTasks](https://www.nuget.org/packages/RecurrentTasks/)
Expand Down
5 changes: 4 additions & 1 deletion sample/RecurrentTasks.Sample/SampleTask.cs
Expand Up @@ -15,11 +15,14 @@ public SampleTask(ILogger<SampleTask> logger, SampleTaskRunHistory runHistory)
this.runHistory = runHistory;
}

public void Run(TaskRunStatus taskRunStatus)
public void Run(ITask currentTask)
{
var msg = string.Format("Run at: {0}", DateTimeOffset.Now);
runHistory.Messages.Add(msg);
logger.LogDebug(msg);

// You can change interval for [all] next runs!
currentTask.Interval = currentTask.Interval.Add(TimeSpan.FromSeconds(1));
}
}
}
3 changes: 3 additions & 0 deletions sample/RecurrentTasks.Sample/Views/Home/Index.cshtml
Expand Up @@ -15,6 +15,9 @@
<dt>RunStatus.LastRunTime</dt>
<dd>@myTask.RunStatus.LastRunTime</dd>

<dt>Interval</dt>
<dd>@myTask.Interval</dd>

<dt>RunStatus.NextRunTime</dt>
<dd>@myTask.RunStatus.NextRunTime (in @(myTask.RunStatus.NextRunTime.Subtract(DateTime.Now)))</dd>
</dl>
Expand Down
2 changes: 1 addition & 1 deletion src/RecurrentTasks/IRunnable.cs
Expand Up @@ -2,6 +2,6 @@
{
public interface IRunnable
{
void Run(TaskRunStatus taskRunStatus);
void Run(ITask currentTask);
}
}
10 changes: 8 additions & 2 deletions src/RecurrentTasks/RecurrentTasksApplicationBuilderExtensions.cs
Expand Up @@ -9,13 +9,19 @@ public static class RecurrentTasksApplicationBuilderExtensions
public static void StartTask<TRunnable>(this IApplicationBuilder app, TimeSpan interval)
where TRunnable : IRunnable
{
StartTask<TRunnable>(app, interval, TimeSpan.FromSeconds(new Random().Next(10, 30)));
StartTask<TRunnable>(app, t => { t.Interval = interval; });
}

public static void StartTask<TRunnable>(this IApplicationBuilder app, TimeSpan interval, TimeSpan initialTimeout)
where TRunnable : IRunnable
{
StartTask<TRunnable>(app, t => { t.Interval = interval; }, TimeSpan.FromSeconds(new Random().Next(10, 30)));
StartTask<TRunnable>(app, t => { t.Interval = interval; }, initialTimeout);
}

public static void StartTask<TRunnable>(this IApplicationBuilder app, Action<ITask> setupAction)
where TRunnable : IRunnable
{
StartTask<TRunnable>(app, setupAction, TimeSpan.FromSeconds(new Random().Next(10, 30)));
}

public static void StartTask<TRunnable>(this IApplicationBuilder app, Action<ITask> setupAction, TimeSpan initialTimeout)
Expand Down
2 changes: 1 addition & 1 deletion src/RecurrentTasks/TaskRunner.cs
Expand Up @@ -131,7 +131,7 @@ protected void MainLoop(TimeSpan firstRunDelay)
var runnable = (TRunnable) scope.ServiceProvider.GetRequiredService(typeof(TRunnable));

logger.LogInformation("Calling Run()...");
runnable.Run(RunStatus);
runnable.Run(this);
logger.LogInformation("Done.");

RunStatus.LastRunTime = startTime;
Expand Down
2 changes: 1 addition & 1 deletion src/RecurrentTasks/project.json
@@ -1,5 +1,5 @@
{
"version": "3.0.0-beta1",
"version": "3.0.0-beta2",
"title": "RecurrentTasks",
"copyright": "Dmitry Popov, 2016",
"packOptions": {
Expand Down
3 changes: 1 addition & 2 deletions test/RecurrentTasks.Tests/SampleTask.cs
@@ -1,7 +1,6 @@
namespace RecurrentTasks
{
using System;
using System.Threading;

public class SampleTask : IRunnable
{
Expand All @@ -12,7 +11,7 @@ public SampleTask(SampleTaskSettings settings)
this.settings = settings;
}

public void Run(TaskRunStatus taskRunStatus)
public void Run(ITask currentTask)
{
settings.TaskRunCalled.Set();
if (settings.MustThrowError)
Expand Down
3 changes: 1 addition & 2 deletions test/RecurrentTasks.Tests/TaskRunnerTests.cs
Expand Up @@ -4,7 +4,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using System.Threading;

public class TaskRunnerTests : IDisposable
{
Expand Down Expand Up @@ -199,7 +198,7 @@ public void Task_AfterRunFailGeneratedAfterException()
sampleTask.Start(TimeSpan.FromSeconds(1));

// waiting 2 seconds max, then failing
Assert.True(settings.TaskRunCalled.Wait(TimeSpan.FromSeconds(2)));
Assert.True(settings.TaskRunCalled.Wait(TimeSpan.FromSeconds(3)));

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

Expand Down
8 changes: 4 additions & 4 deletions test/RecurrentTasks.Tests/project.lock.json
Expand Up @@ -2662,7 +2662,7 @@
"lib/netstandard1.1/xunit.runner.utility.dotnet.dll": {}
}
},
"RecurrentTasks/3.0.0": {
"RecurrentTasks/3.0.0-beta1": {
"type": "project",
"framework": ".NETStandard,Version=v1.3",
"dependencies": {
Expand Down Expand Up @@ -3348,7 +3348,7 @@
"lib/net45/xunit.runner.utility.desktop.dll": {}
}
},
"RecurrentTasks/3.0.0": {
"RecurrentTasks/3.0.0-beta1": {
"type": "project",
"framework": ".NETFramework,Version=v4.5.1",
"dependencies": {
Expand Down Expand Up @@ -4036,7 +4036,7 @@
"lib/net45/xunit.runner.utility.desktop.dll": {}
}
},
"RecurrentTasks/3.0.0": {
"RecurrentTasks/3.0.0-beta1": {
"type": "project",
"framework": ".NETFramework,Version=v4.6",
"dependencies": {
Expand Down Expand Up @@ -9190,7 +9190,7 @@
"xunit.runner.utility.nuspec"
]
},
"RecurrentTasks/3.0.0": {
"RecurrentTasks/3.0.0-beta1": {
"type": "project",
"path": "../../src/RecurrentTasks/project.json",
"msbuildProject": "../../src/RecurrentTasks/RecurrentTasks.xproj"
Expand Down

0 comments on commit 4b957e6

Please sign in to comment.