Code rearrange for services and charting

This commit is contained in:
2016-04-27 09:52:41 -04:00
parent 8c218cda48
commit 9730600164
11 changed files with 20 additions and 19 deletions
@@ -0,0 +1,70 @@
using System;
using System.Linq.Expressions;
using Hangfire;
using LeafWeb.Core.DAL;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Remote;
using NLog;
using Polly;
namespace LeafWeb.Web.Services.PiscalQueue
{
public abstract class PiscalQueueBase : IDisposable
{
protected readonly DataService DataService;
protected readonly PiscalService PiscalService;
protected readonly Logger Logger;
private readonly Policy _retryPolicy;
protected PiscalQueueBase(DataService dataService, PiscalService piscalService)
{
DataService = dataService;
PiscalService = piscalService;
Logger = LogManager.GetLogger(GetType().Name);
_retryPolicy =
Policy
.Handle<TimeoutException>()
.Retry(3,
(exception, i) => Logger.Warn($"Retry {i} after exception: {exception.Message}"));
}
protected PiscalQueueBase() : this(new DataService(), new PiscalService()) { }
protected string FormatException(Exception ex, int leafInputId)
{
return
$"LeafInput: {leafInputId}{Environment.NewLine}" +
$"Class: {GetType().Name}{Environment.NewLine}" +
$"Exception: {ex.Message}{Environment.NewLine}" +
(ex.InnerException != null ? $"InnerException: {ex.InnerException}{Environment.NewLine}" : string.Empty)
+ $"StackTrace: {ex.StackTrace}";
}
protected void PiscalExceptionHandle(PiscalClientException ex, LeafInput leafInput)
{
var errorMessage = FormatException(ex, ex.LeafInputId);
Logger.Error(errorMessage);
BackgroundJobEnqueueRetry<EmailNotificationService>(
email => email.SendAdministratorMessage($"LeafWeb: PiscalQueue {GetType().Name} Exception", errorMessage));
// TODO send user email too
if (leafInput != null)
{
DataService.SetLeafInputStatus(leafInput, LeafInputStatusType.Exception, "Error occurred processing LeafInput",
ex.Message);
}
}
public void Dispose()
{
DataService.Dispose();
}
protected string BackgroundJobEnqueueRetry<T>(Expression<Action<T>> a)
{
return _retryPolicy.Execute(() => BackgroundJob.Enqueue(a));
}
}
}