Skip to content

Commit

Permalink
changed event signatures and datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
tallesl committed Jul 17, 2016
1 parent de41c69 commit d1203f4
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 112 deletions.
16 changes: 0 additions & 16 deletions Library/Event/GenericEventHandler.cs

This file was deleted.

12 changes: 11 additions & 1 deletion Library/Info/JobEndInfo.cs → Library/Event/JobEndInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@
/// <summary>
/// Information of a job end.
/// </summary>
public class JobEndInfo : JobStartInfo
public class JobEndInfo
{
/// <summary>
/// Name of the job.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Date and time of the start.
/// </summary>
public DateTime StartTime { get; set; }

/// <summary>
/// The elapsed time of the job.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace FluentScheduler
{
using System.Threading.Tasks;
using System;

/// <summary>
/// Information of an exception occurred in a job.
Expand All @@ -13,8 +13,8 @@ public class JobExceptionInfo
public string Name { get; set; }

/// <summary>
/// Job's task.
/// Job's exception.
/// </summary>
public Task Task { get; set; }
public Exception Exception { get; set; }
}
}
File renamed without changes.
26 changes: 0 additions & 26 deletions Library/Event/UnhandledExceptionEventArgs.cs

This file was deleted.

60 changes: 29 additions & 31 deletions Library/JobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,63 +105,61 @@ private static void DisposeIfNeeded(IJob job)
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly",
Justification = "Using strong-typed GenericEventHandler<TSender, TEventArgs> event handler pattern.")]
public static event GenericEventHandler<JobExceptionInfo, FluentScheduler.UnhandledExceptionEventArgs> JobException;
public static event Action<JobExceptionInfo> JobException;

/// <summary>
/// Event raised when a job starts.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly",
Justification = "Using strong-typed GenericEventHandler<TSender, TEventArgs> event handler pattern.")]
public static event GenericEventHandler<JobStartInfo, EventArgs> JobStart;
public static event Action<JobStartInfo> JobStart;

/// <summary>
/// Evemt raised when a job ends.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly",
Justification = "Using strong-typed GenericEventHandler<TSender, TEventArgs> event handler pattern.")]
public static event GenericEventHandler<JobEndInfo, EventArgs> JobEnd;
public static event Action<JobEndInfo> JobEnd;

private static void RaiseJobException(Schedule schedule, Task t)
{
var handler = JobException;
if (handler != null && t.Exception != null)
if (JobException != null && t.Exception != null)
{
var info = new JobExceptionInfo
{
Name = schedule.Name,
Task = t
};
handler(info, new FluentScheduler.UnhandledExceptionEventArgs(t.Exception.InnerException, true));
JobException(
new JobExceptionInfo
{
Name = schedule.Name,
Exception = t.Exception.InnerException,
}
);
}
}
private static void RaiseJobStart(Schedule schedule, DateTime startTime)
{
var handler = JobStart;
if (handler != null)
if (JobStart != null)
{
var info = new JobStartInfo
{
Name = schedule.Name,
StartTime = startTime
};
handler(info, new EventArgs());
JobStart(
new JobStartInfo
{
Name = schedule.Name,
StartTime = startTime,
}
);
}
}
private static void RaiseJobEnd(Schedule schedule, DateTime startTime, TimeSpan duration)
{
var handler = JobEnd;
if (handler != null)
if (JobEnd != null)
{
var info = new JobEndInfo
{
Name = schedule.Name,
StartTime = startTime,
Duration = duration
};
if (schedule.NextRun != default(DateTime))
info.NextRun = schedule.NextRun;

handler(info, new EventArgs());
JobEnd(
new JobEndInfo
{
Name = schedule.Name,
StartTime = startTime,
Duration = duration,
NextRun = schedule.NextRun,
}
);
}
}

Expand Down
8 changes: 3 additions & 5 deletions Library/Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,20 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Extension\DelayForExtensions.cs" />
<Compile Include="Event\GenericEventHandler.cs" />
<Compile Include="Unit\IDayRestrictableUnit.cs" />
<Compile Include="Unit\ITimeRestrictableUnit.cs" />
<Compile Include="Unit\MinuteUnit.cs" />
<Compile Include="Extension\RestrictableUnitExtensions.cs" />
<Compile Include="Unit\SecondUnit.cs" />
<Compile Include="Info\JobExceptionInfo.cs" />
<Compile Include="Event\JobExceptionInfo.cs" />
<Compile Include="Unit\DelayTimeUnit.cs" />
<Compile Include="Enum\TimeOfDayRunnable.cs" />
<Compile Include="Util\TimeOfDayRunnableCalculator.cs" />
<Compile Include="Unit\WeekdayUnit.cs" />
<Compile Include="Util\ScheduleCollection.cs" />
<Compile Include="JobFactory.cs" />
<Compile Include="Info\JobEndInfo.cs" />
<Compile Include="Info\JobStartInfo.cs" />
<Compile Include="Event\JobEndInfo.cs" />
<Compile Include="Event\JobStartInfo.cs" />
<Compile Include="Registry.cs" />
<Compile Include="Extension\DateTimeExtensions.cs" />
<Compile Include="IJob.cs" />
Expand All @@ -88,7 +87,6 @@
<Compile Include="JobManager.cs" />
<Compile Include="Schedule.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Event\UnhandledExceptionEventArgs.cs" />
</ItemGroup>
<ItemGroup>
<None Include="keyfile.snk" />
Expand Down
1 change: 1 addition & 0 deletions Library/Rules.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
<Rule Id="CA1702" Action="None" />
<Rule Id="CA1704" Action="None" />
<Rule Id="CA1710" Action="None" />
</Rules>
</RuleSet>
18 changes: 6 additions & 12 deletions PortableLibrary/PortableLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@
<Compile Include="..\Library\Enum\Week.cs">
<Link>Enum\Week.cs</Link>
</Compile>
<Compile Include="..\Library\Event\GenericEventHandler.cs">
<Link>Event\GenericEventHandler.cs</Link>
</Compile>
<Compile Include="..\Library\Event\UnhandledExceptionEventArgs.cs">
<Link>Event\UnhandledExceptionEventArgs.cs</Link>
</Compile>
<Compile Include="..\Library\Extension\DateTimeExtensions.cs">
<Link>Extension\DateTimeExtensions.cs</Link>
</Compile>
Expand All @@ -61,14 +55,14 @@
<Compile Include="..\Library\Extension\RestrictableUnitExtensions.cs">
<Link>Extension\RestrictableUnitExtensions.cs</Link>
</Compile>
<Compile Include="..\Library\Info\JobEndInfo.cs">
<Link>Information\JobEndInfo.cs</Link>
<Compile Include="..\Library\Event\JobEndInfo.cs">
<Link>Event\JobEndInfo.cs</Link>
</Compile>
<Compile Include="..\Library\Info\JobExceptionInfo.cs">
<Link>Information\JobExceptionInfo.cs</Link>
<Compile Include="..\Library\Event\JobExceptionInfo.cs">
<Link>Event\JobExceptionInfo.cs</Link>
</Compile>
<Compile Include="..\Library\Info\JobStartInfo.cs">
<Link>Information\JobStartInfo.cs</Link>
<Compile Include="..\Library\Event\JobStartInfo.cs">
<Link>Event\JobStartInfo.cs</Link>
</Compile>
<Compile Include="..\Library\Unit\DayUnit.cs">
<Link>Unit\DayUnit.cs</Link>
Expand Down
14 changes: 2 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,24 +173,14 @@ protected void Application_Start()

## Unexpected exceptions

To observe unhandled exceptions from your scheduled jobs, you will need to hook the [JobException] event on
To observe unhandled exceptions from your scheduled jobs, you will need to hook the JobException event on
[JobManager].
That event will give you access to the underlying [System.Threading.Tasks.Task] and thrown exception details.

```cs
protected void Application_Start()
{
JobManager.JobException += JobExceptionHandler;
JobManager.Initialize(new JobRegistry());
}

static void JobExceptionHandler(Task sender, UnhandledExceptionEventArgs e)
{
Log.Fatal("An error happened with a scheduled job: " + e.ExceptionObject);
}
JobManager.JobException += (info) => Log.Fatal("An error just happened with a scheduled job: " + info.Exception);
```

[JobException]: Library/JobManager.cs#L32
[System.Threading.Tasks.Task]: https://msdn.microsoft.com/library/System.Threading.Tasks.Task

## Daylight Saving Time
Expand Down
11 changes: 5 additions & 6 deletions TestApplication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ static void Main(string[] args)
private static void ListenForStart()
{
L.Register("[job start]", "{0} has started.");
JobManager.JobStart += (schedule, e) => L.Log("[job start]", schedule.Name);
JobManager.JobStart += (info) => L.Log("[job start]", info.Name);
}

private static void ListenForEnd()
{
L.Register("[job end]", "{0} has ended{1}.");

JobManager.JobEnd += (schedule, e) =>
L.Log("[job end]", schedule.Name,
schedule.Duration > TimeSpan.FromSeconds(1) ?
" with duration of " + schedule.Duration : string.Empty);
JobManager.JobEnd += (info) =>
L.Log("[job end]", info.Name,
info.Duration > TimeSpan.FromSeconds(1) ? " with duration of " + info.Duration : string.Empty);
}

private static void ListenForException()
{
L.Register("[job exception]", "An error just happened:" + Environment.NewLine + "{0}");
JobManager.JobException += (sender, e) => L.Log("[job exception]", e.ExceptionObject);
JobManager.JobException += (info) => L.Log("[job exception]", info.Exception);
}

private static void Initialize()
Expand Down

0 comments on commit d1203f4

Please sign in to comment.