4

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;
    }
}
bschulz
  • 191
  • 2
  • 12
  • Is your default Quartz log being generated at all (It should at least show the scheduler starting and stopping)? If not then It may benefit you to solve the logging issue first. – Matthew D Jun 19 '18 at 17:45
  • I've configured Nlog and see messages like this in my local debugger. I'm not sure what I should be seeing. Does this look right? 2018-06-19 Debug Prepared SQL: INSERT INTO QRTZ_FIRED_TRIGGERS (SCHED_NAME, ENTRY_ID, TRIGGER_NAME, TRIGGER_GROUP, INSTANCE_NAME, FIRED_TIME, SCHED_TIME, STATE, JOB_NAME, JOB_GROUP, IS_NONCONCURRENT, REQUESTS_RECOVERY, PRIORITY) VALUES('QuartzScheduler', – bschulz Jun 20 '18 at 00:15
  • Were you running your test environment scheduler within the context of the app server and now running the production environment scheduler as a standalone service? – Matthew D Jun 20 '18 at 13:06
  • Its pretty strange. I'm running four different App Service environments on Azure. I use slots on my production and UAT environments to eliminate (reduce) downtime during deployments. I deployed an update to production last night. One of my triggers kept going into an ERROR status. I simply deployed the same version again, and swapped slots - so far today I have not seen any errors. That makes me think it is environmental, but I am perplexed what that might be. I'll keep observing and report back if I find anything. Thanks for your help. – bschulz Jun 22 '18 at 00:19
  • @bschulz Did you figure it out? – Marzouk Feb 13 '20 at 10:48
  • @bschulz did you find anyway to figure this out? I am too facing this same issue but unable to know who and why the trigger state is ERROR – ScanQR Feb 17 '21 at 07:23

0 Answers0