Job Scheduling using Quartz .NET and c#

265 Views Asked by At

I have created a job scheduler that runs every 10 minutes and sends emails if the data exists in the table. It is working fine in my local and our testing environment but not working, when I deploy it on production

My CODE:

public static void Start()
{
    IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
    scheduler.Start();

    IJobDetail job = JobBuilder.Create<EmailTrigger>().Build();

    ITrigger trigger = TriggerBuilder.Create()
        .WithDailyTimeIntervalSchedule
        (s =>
          s.WithIntervalInMinutes(10)
          .OnEveryDay()
          .InTimeZone(TimeZoneInfo.Local) //added later
        )
        .Build();

    scheduler.ScheduleJob(job, trigger);
}

Global.asax.cs

    protected void Application_Start(object sender, EventArgs e)
    {
        risknucleus.DataLayer.cls_DataLayer obj = new risknucleus.DataLayer.cls_DataLayer();
       
        obj.execute("insert into EmailSendLogs(Message,Date) values('Global Page 
        Hit',getdate())");
        JobScheduler.Start();
    }

EmailTrigger. cs

public void Execute(IJobExecutionContext context)
{
    try
    {
        
        clsEmail objEmail = new clsEmail();
        if (dtLayer.getValueFromStaticConfiguration("EmailTriggerOnApproval", "N") == "Y")
        {
            dtLayer.execute(@"Insert into EmailSendLogs(Message,Description,Date) 
                             values ('Trigger started 1','Trigger started 1' , GetDate())");

            DataTable dtData = dtLayer.getDataTableFromQuery("select * from EmailSendData where IsSend is null");
            if (dtData.Rows.Count > 0)
            {
                for (int i = 0; i < dtData.Rows.Count; i++)
                {
                    dtLayer.execute(@"Insert into EmailSendLogs(Message,Description,Date) 
                             values ('Trigger started 2','Trigger started 2' , GetDate())");

                    string NomineeList = dtData.Rows[i]["NomineeUser"].ToString();
                    string FromEmpid = dtData.Rows[i]["FromEmpID"].ToString();
                    string EmailID = dtData.Rows[i]["EmailID"].ToString();
                    string url = dtData.Rows[i]["URL"].ToString();
                    string Type = dtData.Rows[i]["Type"].ToString();
                    string query = dtData.Rows[i]["EmailDetails"].ToString();
                    string UserID = dtData.Rows[i]["UserID"].ToString();
                    string NotificationID = dtData.Rows[i]["NotificationID"].ToString();
                    string CCMail = dtData.Rows[i]["CCMail"].ToString();
                    string ApprovalCode = dtData.Rows[i]["ApprovalCode"].ToString();
                    string Id = dtData.Rows[i]["ID"].ToString();

                    DataTable dtEmailDetails = dtLayer.getDataTableFromQuery(query);
                    ArrayList emailDetails = dtLayer.datatableTOarraylist(dtEmailDetails);
                    emailDetails.Add(ApprovalCode);

                    if (dtLayer.getValueFromStaticConfiguration("SendEmail", "N") == "Y")
                        objEmail.sendMailNewFunction(NomineeList, FromEmpid, EmailID, url, Type, "", emailDetails, UserID, false, null, NotificationID, CCMail);

                    dtLayer.execute("update EmailSendData set isSend='Y' where ID= " + Id + " ");

                }
            }
            else
            {

                dtLayer.execute(@"Insert into EmailSendLogs(Message,Description,Date) 
                             values ('Job ran successfully','No approved cases pending' , GetDate())");
            }
        }
    }
    catch (Exception e)
    {
        dtLayer.execute(@"Insert into EmailSendLogs(Message,Description,Date) 
             values (" + dtLayer.checkNull(e.ToString()) + ",'Exception Catch',GetDate())");
        throw e;
    }
}

As you can see I am maintaining logs to check if the global asax page has been properly updated and adding some logs in my emaitrigger also. So even if dtData doesn't return anything it should make an entry to database in EmailSendLogs Table. I logged into my application and the hit was made to global.asax and then this line was executed

obj.execute("insert into EmailSendLogs(Message,Date) values('Global Page 
        Hit',getdate())");

but after that jobscheduler.start() does not trigger.

Here is what I have tried:

  • I restarted the IIS Server after deployment and also cleared caches
  • Some solutions suggested to set enableMode to AlwaysRunning so I did that too.
  • I added TimeZone in my function

None of these solutions work. And I really need this to work. I don't want to use Windows Service. If it is working in my testing environment, why isn't it working in production?

1

There are 1 best solutions below

2
On
  1. Make sure production and test environments are parity by verifying environment parity.

  2. Turn on thorough logging so that you may troubleshoot by recording faults and execution sequences.

  3. Verify access to resources, such as databases, by carrying out a permission check.

  4. Check settings and credentials to validate configurations.

  5. Keep track of resource limitations: Pay attention to server resources and load.

  6. Prevent numerous scheduler instances by managing concurrency.

  7. Verify outgoing connections by reviewing the network and firewall.

  8. If remote debugging tools are available, utilize them for troubleshooting in production.

  9. Examine in a staging environment: Conduct validation beforehand.