Skip to content

Commit 4d7c97c

Browse files
committed
New: AfterRunFail event with Exception info
1 parent 0b41df3 commit 4d7c97c

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace RecurrentTasks
2+
{
3+
using System;
4+
5+
public class ExceptionEventArgs : EventArgs
6+
{
7+
public ExceptionEventArgs(Exception exception)
8+
{
9+
if (exception == null)
10+
{
11+
throw new ArgumentNullException(nameof(exception));
12+
}
13+
this.Exception = exception;
14+
}
15+
16+
public Exception Exception { get; protected set; }
17+
}
18+
}

src/RecurrentTasks/TaskBase.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public TaskBase(ILoggerFactory loggerFactory, TimeSpan interval, IServiceScopeFa
2828
RunStatus = new TRunStatus();
2929
}
3030

31+
public event EventHandler<ExceptionEventArgs> AfterRunFail;
32+
3133
TaskRunStatus ITask.RunStatus { get { return RunStatus; } }
3234

3335
public TRunStatus RunStatus { get; protected set; }
@@ -138,7 +140,7 @@ protected void MainLoop(TimeSpan initialTimeout)
138140
}
139141
catch (Exception ex)
140142
{
141-
Logger.LogWarning("Ooops, error (ignoring, see RunStatus.LastException):", ex);
143+
Logger.LogWarning(0, ex, "Ooops, error (ignoring, see RunStatus.LastException or handle AfterRunFail event)");
142144
RunStatus.LastResult = TaskRunResult.Fail;
143145
RunStatus.LastException = ex;
144146
if (RunStatus.FailsCount == 0)
@@ -149,6 +151,15 @@ protected void MainLoop(TimeSpan initialTimeout)
149151
IsRunningRightNow = false;
150152

151153
OnAfterRunFail();
154+
155+
try
156+
{
157+
AfterRunFail?.Invoke(this, new ExceptionEventArgs(ex));
158+
}
159+
catch (Exception ex2)
160+
{
161+
Logger.LogError(0, ex2, "Error while processing AfterRunFail event (ignored)");
162+
}
152163
}
153164
finally
154165
{

src/RecurrentTasks/project.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"version": "2.1.0",
2+
"version": "2.2.0",
33
"title": "RecurrentTasks",
44
"copyright": "Dmitry Popov, 2016",
55
"packOptions": {
66
"summary": "RecurrentTasks for .NET allows you to run simple recurrent background tasks with specific intervals, without complex frameworks, persistance, etc...",
77
"tags": [ "task", "job", "recurrent", "recurring", "aspnetcore" ],
88
"owners": [ "Dmitry Popov" ],
9-
"releaseNotes": "Upgrade to RC2 and netstandard",
9+
"releaseNotes": "New: AfterRunFail event (with Exception info)",
1010
"licenseUrl": "https://github.com/justdmitry/RecurrentTasks/blob/master/LICENSE",
1111
"projectUrl": "https://github.com/justdmitry/RecurrentTasks",
1212
"repository": {

test/RecurrentTasks.Tests/TaskBaseTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,26 @@ public void Task_RunningAgainAfterException()
175175
// should run again - waiting twice default interval
176176
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(10)));
177177
}
178+
179+
[Fact]
180+
public void Task_AfterRunFailGeneratedAfterException()
181+
{
182+
var eventGenerated = false;
183+
184+
sampleTask.MustThrowError = true;
185+
sampleTask.AfterRunFail += (object sender, ExceptionEventArgs e) =>
186+
{
187+
eventGenerated = true;
188+
};
189+
190+
sampleTask.Start(TimeSpan.FromSeconds(2));
191+
192+
// waiting 5 seconds max, then failing
193+
Assert.True(sampleTask.TaskRunCalled.Wait(TimeSpan.FromSeconds(5)));
194+
195+
System.Threading.Thread.Sleep(200); // wait for run cycle completed
196+
197+
Assert.True(eventGenerated);
198+
}
178199
}
179200
}

0 commit comments

Comments
 (0)