Skip to content

Commit

Permalink
Fixed Sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasArdal committed Jan 11, 2024
1 parent 28be0ff commit 08a2fdf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
24 changes: 10 additions & 14 deletions src/Elmah.Io.Heartbeats.Hangfire/ElmahIoHeartbeatAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace Elmah.Io.Heartbeats.Hangfire
/// </summary>
public class ElmahIoHeartbeatAttribute : JobFilterAttribute, IServerFilter
{
internal static string _assemblyVersion = typeof(ElmahIoHeartbeatAttribute).Assembly.GetName().Version.ToString();
internal static string _hangfireAssemblyVersion = typeof(JobFilterAttribute).Assembly.GetName().Version.ToString();
private static string _assemblyVersion = typeof(ElmahIoHeartbeatAttribute).Assembly.GetName().Version.ToString();
private static string _hangfireAssemblyVersion = typeof(JobFilterAttribute).Assembly.GetName().Version.ToString();

private const string StopwatchKeyName = "elmahio-timing";
private readonly Guid logId;
Expand Down Expand Up @@ -47,32 +47,28 @@ public ElmahIoHeartbeatAttribute(string apiKey, string logId, string heartbeatId
/// <summary>
/// Called by Hangire just before executing the job.
/// </summary>
public void OnPerforming(PerformingContext context)
public void OnPerforming(PerformingContext filterContext)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
context.Items.Add(StopwatchKeyName, stopwatch);
filterContext.Items.Add(StopwatchKeyName, stopwatch);
}

/// <summary>
/// Called by Hangfire just after executing the job.
/// </summary>
public void OnPerformed(PerformedContext context)
public void OnPerformed(PerformedContext filterContext)
{
long? took = null;
if (context.Items.ContainsKey(StopwatchKeyName))
if (filterContext.Items.ContainsKey(StopwatchKeyName) && filterContext.Items[StopwatchKeyName] is Stopwatch stopwatch)
{
var stopwatch = context.Items[StopwatchKeyName] as Stopwatch;
if (stopwatch != null)
{
stopwatch.Stop();
took = stopwatch.ElapsedMilliseconds;
}
stopwatch.Stop();
took = stopwatch.ElapsedMilliseconds;
}

if (context.Exception != null)
if (filterContext.Exception != null)
{
heartbeats.Unhealthy(logId, heartbeatId, context.Exception.ToString(), took: took);
heartbeats.Unhealthy(logId, heartbeatId, filterContext.Exception.ToString(), took: took);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void CanReportUnhealthyOnException()
heartbeatsClientMock.DidNotReceive().Healthy(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<long?>());
}

private HangfireServer.PerformedContext PerformedContext(Exception exception)
private static HangfireServer.PerformedContext PerformedContext(Exception exception)
{
return new HangfireServer.PerformedContext(
new HangfireServer.PerformContext(
Expand Down

0 comments on commit 08a2fdf

Please sign in to comment.