Quartz.net not firing and not throwing error

1.2k Views Asked by At

I'm attempting to run a Quartz.net scheduler using HttpModules as we don't have access to the Global.ascx. The code up to the point of the executing the job seems to be working fine but when it comes to the job itself it doesn't fire nor is any error thrown. I've even gone as far as making the job as simple as a redirect in case there was just something not working in the code but that doesn't fire either. Anyone have success with Quartz.net working in this way?

Here is the HTTPModule:

public void Init(HttpApplication application)
    {
        application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
        application.EndRequest += (new EventHandler(this.Application_EndRequest));
    }

    // Your BeginRequest event handler.
    private void Application_BeginRequest(Object source, EventArgs e)
    {
        try
        {
            Auction123CSVScheduler.Start();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    // Your EndRequest event handler.
    private void Application_EndRequest(Object source, EventArgs e)
    {

    }

    public void Dispose()
    {
    }

Here is the scheduler:

public class Auction123CSVScheduler
{
    public Auction123CSVScheduler()
    {
        //
        // TODO: Add constructor logic here
        //
    }

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

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

            ITrigger trigger = TriggerBuilder.Create()
                .StartNow().WithSimpleSchedule(x => x.WithIntervalInSeconds(120).RepeatForever()).Build();

            scheduler.ScheduleJob(job, trigger);

        }
        catch (SchedulerException se)
        {
            throw se;
        }
    }
}

And here is our Job:

public class Auction123CSVJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        try
        {
            VehiclesDataSet vds = new VehiclesDataSet();

            DataSet ds = vds.Auction123Info();

            CreateCSVFile(ds);
        }
        catch (Exception e)
        {
            throw new JobExecutionException(e);
        }
    }


    //create csv file format from dataset passed in and save to the file system
    private void CreateCSVFile(DataSet ds)
    {

        if (ds.Tables.Count > 0)
        {
            //make the dataset into a datatable for use with streamwriter
            DataTable dt = ds.Tables[0];

            try
            {
                //identify file path. set to overwrite
                StreamWriter sw = new StreamWriter("c://inetpub/wwwroot/customer/myfile.txt", false);

                //write headers
                for (int c = 0; c < dt.Columns.Count; c++)
                {
                    sw.Write(dt.Columns[c]);
                    if (c < dt.Columns.Count - 1)
                    {
                        sw.Write("|");
                    }
                }

                sw.Write(sw.NewLine);

                //write content items
                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            sw.Write(dr[i].ToString());
                        }

                        if (i < dt.Columns.Count - 1)
                        {
                            sw.Write("|");
                        }
                    }

                    sw.Write(sw.NewLine);
                }

                sw.Close();
            }
            catch (Exception ex)
            {
                throw new JobExecutionException(ex);
            }
        }
    }
}
1

There are 1 best solutions below

1
On BEST ANSWER

You're not maintaining a reference to the scheduler. As soon as the start method exits the scheduler may be garbage collected and so it doesn't live long enough to run the job. In your case, you should create the scheduler on application start and maintain a reference to it somewhere where it doesn't run out of scope. This is a less the ideal scenario though. Ideally you should set up Quartz to be a separate service, because IIS will can recycle the app at any point in time, killing your scheduler.