0

I am using quartz.NET in my project. I have the following problem. I want to run a Scheduled task everyday at 23 o'clock and I am using this code to do that:

public class HelloJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        //Download file
    }
}
public interface ISchedule
{
    void Run();
}

public class HelloSchedule : ISchedule
{
    public void Run()
    {

        IJobDetail job = JobBuilder.Create<HelloJob>()
                                   .WithIdentity("job1")
                                   .Build();

        ITrigger trigger = TriggerBuilder.Create()
                                         .ForJob(job)
                                         .WithIdentity("trigger1")
                                         .StartNow()
                                         .WithCronSchedule("0 0 23 ? * MON-FRI *")
                                         .Build();

        ISchedulerFactory sf = new StdSchedulerFactory();
        IScheduler sc = sf.GetScheduler();
        sc.ScheduleJob(job, trigger);

        sc.Start();
    }
}

but unfortunately it's not firing. How can I know what the problem is and solve it?
Thanks for your advice

walen
  • 7,103
  • 2
  • 37
  • 58
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110
  • 1
    Looks ok. You may need to enable logging and look at the log files. Also, if this is an website, make sure it isn't getting shutdown due to being idle. – sgmoore Jun 26 '13 at 11:29

2 Answers2

1

Your job will fire at 11pm.
You can check the next fire time for you job using this code:

private static DateTime getNextFireTimeForJob(IScheduler scheduler, string jobName, string groupName = "")
    {
        JobKey jobKey = null;

        if (!string.IsNullOrEmpty(groupName))
        {
            jobKey = new JobKey(jobName, groupName);
        }
        else
        {
            jobKey = new JobKey(jobName);
        }

        DateTime nextFireTime = DateTime.MinValue;

        bool isJobExisting = Scheduler.CheckExists(jobKey);
        if (isJobExisting)
        {
            var detail = scheduler.GetJobDetail(jobKey);
            var triggers = scheduler.GetTriggersOfJob(jobKey);

            var myTrigger = triggers.Where(f => f.Key.Name == "SecondTrigger").SingleOrDefault();


            if (triggers.Count > 0)
            {
                var nextFireTimeUtc = triggers[0].GetNextFireTimeUtc();
                nextFireTime = TimeZone.CurrentTimeZone.ToLocalTime(nextFireTimeUtc.Value.DateTime);
            }
        }

        return (nextFireTime);
    }

and get the info using this:

var nextFireTime = getNextFireTimeForJob(sc, "job1");

Make sure your HelloJob implements IJob.

If you're integrating your Quartz.net scheduler in a WinApp make sure it's created singleton cause it might be destroyed when it goes out of scope.

I test my jobs in a console application and use Console.ReadLine(); to wait the jobs execution.

LeftyX
  • 35,328
  • 21
  • 132
  • 193
  • I've updated my answer. Give us more infos about your winApp. – LeftyX Jun 26 '13 at 12:45
  • my application should run everyday at 11pm and do specific task(the task is downloading a file on internet that run in Execute method) – Sirwan Afifi Jun 26 '13 at 13:54
  • Consider using some kind of logging system [nLog](http://stackoverflow.com/a/6570161/219406). – LeftyX Jun 27 '13 at 08:55
  • Logs would be helpful as your cron expression seems ok and next fire times will be at 11pm as per [this](http://www.cronmaker.com/) – Shailesh Pratapwar Jun 28 '13 at 06:54
  • I am getting this error : 'System.Collections.Generic.IList' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Collections.Generic.IList' could be found (are you missing a using directive or an assembly reference?) – Sirwan Afifi Jun 28 '13 at 09:49
0

Use org.Quartz and try this:

    JobDetail job = new JobDetail();
    job.setName(Constants.JOB_NAME);
    job.setJobClass(YOUR_JOB_CLASS.class);



    CronTrigger trigger = new CronTrigger();
    trigger.setName(Constants.TRIGGER_NAME);
    trigger.setCronExpression("0 0 23 ? * MON-FRI *");

    // schedule it
    Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    scheduler.start();
    scheduler.scheduleJob(job, trigger);

Your Job class should implement org.Quartz.Job interface and override its execute method which does the actual thing the job needs to perform.

roger_that
  • 9,493
  • 18
  • 66
  • 102