Skip to content

Commit

Permalink
refactor: extract more log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mrahhal committed Apr 15, 2017
1 parent 2ffd38c commit 97509e0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace MR.AspNetCore.Jobs
{
internal static class JobsSqlServerLoggerExtensions
internal static class LoggerExtensions
{
private static Action<ILogger, Exception> _collectingExpiredEntities;

private static Action<ILogger, Exception> _installing;
private static Action<ILogger, Exception> _installingError;
private static Action<ILogger, Exception> _installingSuccess;

static JobsSqlServerLoggerExtensions()
static LoggerExtensions()
{
_collectingExpiredEntities = LoggerMessage.Define(
LogLevel.Debug,
Expand Down Expand Up @@ -44,9 +44,9 @@ public static void Installing(this ILogger logger)
_installing(logger, null);
}

public static void InstallingError(this ILogger logger, Exception exception)
public static void InstallingError(this ILogger logger, Exception ex)
{
_installingError(logger, exception);
_installingError(logger, ex);
}

public static void InstallingSuccess(this ILogger logger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageTags>aspnetcore;background;jobs;sql;sqlserver</PackageTags>
<PackageProjectUrl>https://github.com/mrahhal/MR.AspNetCore.Jobs</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/mrahhal/MR.AspNetCore.Jobs/blob/master/LICENSE.txt</PackageLicenseUrl>
<RootNamespace>MR.AspNetCore.Jobs</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace MR.AspNetCore.Jobs
{
internal static class JobsLoggerExtensions
internal static class LoggerExtensions
{
private static Action<ILogger, int, int, Exception> _serverStarting;
private static Action<ILogger, Exception> _serverShuttingDown;
Expand All @@ -20,9 +20,11 @@ internal static class JobsLoggerExtensions
private static Action<ILogger, Exception> _jobFailed;
private static Action<ILogger, Exception> _jobFailedWillRetry;
private static Action<ILogger, double, Exception> _jobExecuted;
private static Action<ILogger, Exception> _jobRetrying;
private static Action<ILogger, int, Exception> _jobRetrying;
private static Action<ILogger, int, Exception> _jobCouldNotBeLoaded;
private static Action<ILogger, int, Exception> _exceptionOccuredWhileExecutingJob;

static JobsLoggerExtensions()
static LoggerExtensions()
{
_serverStarting = LoggerMessage.Define<int, int>(
LogLevel.Debug,
Expand Down Expand Up @@ -69,15 +71,26 @@ static JobsLoggerExtensions()
2,
"Job failed to execute. Will retry.");

_jobRetrying = LoggerMessage.Define(
_jobRetrying = LoggerMessage.Define<int>(
LogLevel.Debug,
3,
"Job Retrying.");
"Retrying a job: {Retries}...");

_jobExecuted = LoggerMessage.Define<double>(
LogLevel.Debug,
4,
"Job executed. Took: {Seconds} secs.");

_jobCouldNotBeLoaded = LoggerMessage.Define<int>(
LogLevel.Warning,
5,
"Could not load a job: '{JobId}'.");

_exceptionOccuredWhileExecutingJob = LoggerMessage.Define<int>(
LogLevel.Error,
6,
"An exception occured while trying to execute a job: '{JobId}'. " +
"Requeuing for another retry.");
}

public static void ServerStarting(this ILogger logger, int machineProcessorCount, int processorCount)
Expand All @@ -90,9 +103,9 @@ public static void ServerShuttingDown(this ILogger logger)
_serverShuttingDown(logger, null);
}

public static void ExpectedOperationCanceledException(this ILogger logger, Exception exception)
public static void ExpectedOperationCanceledException(this ILogger logger, Exception ex)
{
_expectedOperationCanceledException(logger, exception.Message, exception);
_expectedOperationCanceledException(logger, ex.Message, ex);
}

public static void CronJobsNotFound(this ILogger logger)
Expand All @@ -110,29 +123,39 @@ public static void CronJobExecuted(this ILogger logger, string name, double seco
_cronJobExecuted(logger, name, seconds, null);
}

public static void CronJobFailed(this ILogger logger, string name, Exception exception)
public static void CronJobFailed(this ILogger logger, string name, Exception ex)
{
_cronJobFailed(logger, name, exception);
_cronJobFailed(logger, name, ex);
}

public static void JobFailed(this ILogger logger, Exception exception)
public static void JobFailed(this ILogger logger, Exception ex)
{
_jobFailed(logger, exception);
_jobFailed(logger, ex);
}

public static void JobFailedWillRetry(this ILogger logger, Exception exception)
public static void JobFailedWillRetry(this ILogger logger, Exception ex)
{
_jobFailedWillRetry(logger, exception);
_jobFailedWillRetry(logger, ex);
}

public static void JobRetrying(this ILogger logger)
public static void JobRetrying(this ILogger logger, int retries)
{
_jobRetrying(logger, null);
_jobRetrying(logger, retries, null);
}

public static void JobExecuted(this ILogger logger, double seconds)
{
_jobExecuted(logger, seconds, null);
}

public static void JobCouldNotBeLoaded(this ILogger logger, int jobId, Exception ex)
{
_jobCouldNotBeLoaded(logger, jobId, ex);
}

public static void ExceptionOccuredWhileExecutingJob(this ILogger logger, int jobId, Exception ex)
{
_exceptionOccuredWhileExecutingJob(logger, jobId, ex);
}
}
}
15 changes: 3 additions & 12 deletions src/MR.AspNetCore.Jobs/Server/IProcessor.DelayedJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ private async Task<bool> Step(ProcessingContext context)

if (job.Retries > 0)
{
_logger.LogDebug(
$"Retrying a job: {job.Retries}...");
_logger.JobRetrying(job.Retries);
}

var result = await ExecuteJob(method, instance);
Expand Down Expand Up @@ -134,22 +133,14 @@ private async Task<bool> Step(ProcessingContext context)
}
catch (JobLoadException ex)
{
_logger.LogWarning(
5,
ex,
"Could not load a job: '{JobId}'.",
job.Id);
_logger.JobCouldNotBeLoaded(job.Id, ex);

await _stateChanger.ChangeStateAsync(job, new FailedState(), connection);
fetched.RemoveFromQueue();
}
catch (Exception ex)
{
_logger.LogWarning(
6,
ex,
"An exception occured while trying to execute a job: '{JobId}'. Requeuing for another retry.",
job.Id);
_logger.ExceptionOccuredWhileExecutingJob(job.Id, ex);

fetched.Requeue();
}
Expand Down

0 comments on commit 97509e0

Please sign in to comment.