Skip to content

Commit

Permalink
Add schedule information to UnobservedTaskException event. Fixes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
jgeurts committed Nov 14, 2013
1 parent 6a1bbce commit 2867910
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
5 changes: 3 additions & 2 deletions ConsoleTester/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading;
using System.Threading.Tasks;
using FluentScheduler;
using FluentScheduler.Model;

namespace ConsoleTester
{
Expand Down Expand Up @@ -58,9 +59,9 @@ static void Main(string[] args)
Console.ReadKey();
}

static void TaskManager_UnobservedTaskException(Task sender, UnhandledExceptionEventArgs e)
static void TaskManager_UnobservedTaskException(TaskExceptionInformation sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine("Something went wrong: " + e.ExceptionObject);
Console.WriteLine("Something went wrong with task: " + sender.Name + "\n" + e.ExceptionObject);
}
}

Expand Down
6 changes: 3 additions & 3 deletions FluentScheduler.Webtester/Global.asax.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Threading.Tasks;
using System.Web.Mvc;
using FluentScheduler.Model;
using FluentScheduler.WebTester.Infrastructure.Tasks;
using ServiceStack.Logging;

Expand All @@ -23,10 +23,10 @@ protected void Application_End(object sender, EventArgs e)
TaskManager.Stop();
}

static void TaskManager_UnobservedTaskException(Task sender, UnhandledExceptionEventArgs e)
static void TaskManager_UnobservedTaskException(TaskExceptionInformation sender, UnhandledExceptionEventArgs e)
{
var log = LogManager.GetLogger(typeof(MvcApplication));
log.Fatal("An error happened with a scheduled task: " + e.ExceptionObject);
log.Fatal("An error happened with a scheduled task: " + sender.Name + "\n" + e.ExceptionObject);
}
}
}
1 change: 1 addition & 0 deletions FluentScheduler/FluentScheduler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="GenericEventHandler.cs" />
<Compile Include="Model\TaskExceptionInformation.cs" />
<Compile Include="TaskFactory.cs" />
<Compile Include="Model\TaskEndScheduleInformation.cs" />
<Compile Include="Model\TaskStartScheduleInformation.cs" />
Expand Down
11 changes: 11 additions & 0 deletions FluentScheduler/Model/TaskExceptionInformation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Threading.Tasks;

namespace FluentScheduler.Model
{
public class TaskExceptionInformation
{
public string Name { get; set; }
public Task Task { get; set; }
}
}
15 changes: 11 additions & 4 deletions FluentScheduler/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace FluentScheduler
public static class TaskManager
{
public static ITaskFactory TaskFactory { get; set; }
public static event GenericEventHandler<Task, UnhandledExceptionEventArgs> UnobservedTaskException;
public static event GenericEventHandler<TaskExceptionInformation, UnhandledExceptionEventArgs> UnobservedTaskException;
public static event GenericEventHandler<TaskStartScheduleInformation, EventArgs> TaskStart;
public static event GenericEventHandler<TaskEndScheduleInformation, EventArgs> TaskEnd;

Expand Down Expand Up @@ -85,11 +85,18 @@ public static void Initialize(Registry registry)
RunAndInitializeSchedule(immediateTasks);
}

private static void RaiseUnobservedTaskException(Task t)
private static void RaiseUnobservedTaskException(Schedule schedule, Task t)
{
var handler = UnobservedTaskException;
if (handler != null && t.Exception != null)
handler(t, new UnhandledExceptionEventArgs(t.Exception.InnerException, true));
{
var info = new TaskExceptionInformation
{
Name = schedule.Name,
Task = t
};
handler(info, new UnhandledExceptionEventArgs(t.Exception.InnerException, true));
}
}
private static void RaiseTaskStart(Schedule schedule, DateTime startTime)
{
Expand Down Expand Up @@ -213,7 +220,7 @@ internal static void StartTask(Schedule schedule)
RaiseTaskEnd(schedule, start, stopwatch.Elapsed);
}
}, TaskCreationOptions.PreferFairness);
mainTask.ContinueWith(RaiseUnobservedTaskException, TaskContinuationOptions.OnlyOnFaulted);
mainTask.ContinueWith(task => RaiseUnobservedTaskException(schedule, task), TaskContinuationOptions.OnlyOnFaulted);
}

/// <summary>
Expand Down
5 changes: 4 additions & 1 deletion FluentScheduler/package.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
<summary>A task scheduler that uses fluent interface to configure schedules. Useful for running cron jobs/automated tasks from your application.</summary>
<language>en-US</language>
<tags>Library task-scheduler tasks cron</tags>
<releaseNotes>v2.0 Changes:
<releaseNotes>v3.0 Changes:
* Add schedule information to UnobservedTaskException event

v2.0 Changes:
* Strongly named assembly
* TaskManager.TaskFactory replaces Registry.GetTaskInstance() method
* Allow basic continuations with schedules
Expand Down

0 comments on commit 2867910

Please sign in to comment.