I have Quartz persisting to SQL server. I have not had this in my test environment, but now I moved to production and it is happening. Now the job runs once and results in the trigger being set to ERROR. I don't see any reason why. My job looks like this...nothing is getting logged. Any idea why?
public class SendEmailsJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
IEmailRepo _emailService = new EmailRepo();
IContactHistory _contactHistoryService = new ContactHistoryRepo();
IPolicyRepo _policyService = new PolicyRepo();
NLog.Logger _logger = Log.Instance;
IUtility _utilityService = new UtilityRepo();
const string DIRECT_WELCOME = "DirectWelcome";
const string DIRECT_POST_WELCOME = "DirectPostWelcome";
const string DIRECT_SIGN_NOPAY = "DirectSignNoPay";
const string DIRECT_NOSIGN_NOPAY = "DirectNoSignNoPay";
try
{
var templates = _utilityService.GetEmailTemplates();
foreach (var template in templates)
{
if (template.Value.isActive)
{
var queueItems = _emailService.GetQueueItemsForTemplate(template.Value.name);
foreach (var queueItem in queueItems)
{
var previousEmails = _contactHistoryService.GetAllForPolicy(queueItem.policyNo);
var emailLookup = previousEmails.ToLookup(x => x.contactReference);
bool skip = (queueItem.name == DIRECT_POST_WELCOME && emailLookup.Contains(DIRECT_POST_WELCOME)) || emailLookup.Contains(queueItem.name);
if (skip)
_emailService.DeleteQueueItem(queueItem);
else
{
var policy = _policyService.GetPolicyByPolicyNo(queueItem.policyNo);
var status = SendGridService.Send(queueItem.email, $"{policy.firstName} {policy.lastName}", queueItem.policyNo,
queueItem.name);
if (status == HttpStatusCode.Accepted)
{
_contactHistoryService.Insert(new ContactHistoryModel()
{
contactType = "email",
description = $"{queueItem.name} email sent to {queueItem.email} and was {status}",
contactReference = queueItem.name,
policyId = queueItem.policyId,
policyNo = queueItem.policyNo,
quoteId = queueItem.quoteId,
quoteNumber = queueItem.quoteNumber,
email = queueItem.email
});
_emailService.DeleteQueueItem(queueItem);
}
else
_logger.Error(
$"Error sending email {queueItem.name} for policy {queueItem.policyNo} to {queueItem.email} ending in status {status}");
}
}
}
}
return Task.CompletedTask;
}
catch (JobExecutionException e)
{
_logger.Error($"Error sending emails {e.StackTrace}");
return Task.CompletedTask;
}
catch (Exception e)
{
_logger.Error($"Error sending emails {e.StackTrace}");
return Task.CompletedTask;
}
return Task.CompletedTask;
}
}