Marian Ene
Marian Ene

Reputation: 665

Quartz .net job is not executing

I've created an ASP.NET MVC website. Then I've created a class library named Site.Scheduler where I wanted to put all my triggers and jobs.

I've created a simple job for testing purposes

public class CurrencyRatesJob : IJob
{
    private readonly IBudgetsRepository budgetsRepository;

    public CurrencyRatesJob(IBudgetsRepository budgetsRepository)
    {
        this.budgetsRepository = budgetsRepository;
    }

    public void Execute(IJobExecutionContext context)
    {
        try
        {
            var budgets = new BudgetsDTO();
            var user = new UserDTO();

            budgets.Sum = 1;
            budgets.Name = "Quartz";
            user.Email = "[email protected]";

            budgetsRepository.InsertBudget(budgets, user);
        }
        catch (Exception ex)
        {
            throw new Quartz.JobExecutionException(ex);
        }
    }
}

and a Job Scheduler

public class CurrencyRatesJobScheduler
{
    public static void GetCurrencyRates()
    {
        try
        {
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

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

            ITrigger trigger = TriggerBuilder.Create()
                .StartNow()
                .WithSimpleSchedule
                    (s =>
                        s.WithIntervalInSeconds(10)
                        .RepeatForever()
                    )
                .Build();

            scheduler.ScheduleJob(job, trigger);
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
}

To start the scheduler when application starts, I've added the following in Global.asax.cs

CurrencyRatesJobScheduler.GetCurrencyRates();

So after all that, I was expecting the job to execute every 10 seconds and insert all that info in the DB, but it doesn't do anything and I get no errors either.

Does anyone know what could be the issue?

EDIT:

So I've created all the necessary tables (executed the script from Quartz.NET) and I've added a new App.config file in my class library

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="quartz.scheduler.instanceName" value="MyQuartzScheduler" />
    <add key="quartz.scheduler.instanceId" value="instance_one" />
    <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
    <add key="quartz.threadPool.threadCount" value="10" />
    <add key="quartz.threadPool.threadPriority" value="1" />
    <add key="quartz.jobStore.type" value="Quartz.Impl.AdoJobStore.JobStoreTX, Quartz" />
    <add key="quartz.jobStore.misfireThreshold" value="60000" />
    <add key="quartz.jobStore.dataSource" value="default" />
    <add key="quartz.jobStore.driverDelegateType" value="Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz" />
    <add key="quartz.jobStore.lockHandler.type" value="Quartz.Impl.AdoJobStore.UpdateLockRowSemaphore, Quartz" />
    <add key="quartz.jobStore.tablePrefix" value="QRTZ_" />
    <add key="quartz.dataSource.default.connectionString" value="Server=(local);Database=My.Database;UID=User;PWD=Password" />
    <add key="quartz.dataSource.default.provider" value="SqlServer-20" />
    <add key="quartz.jobStore.useProperties" value="true" />
  </appSettings>
</configuration>

Still no luck. Besides that, no triggers or jobs were stored in the DB.

Upvotes: 3

Views: 12475

Answers (3)

Innovative Thinker
Innovative Thinker

Reputation: 1795

i am also facing the same issue. i fixed the issue by placing config information in the app.config in the consuming application. As Class Library does not capable of running independently. It should be referenced by another app maybe console or web site. we need to place all config items in the consuming application this may be help. cheers. happy sharing

Upvotes: 1

Nick Patsaris
Nick Patsaris

Reputation: 2218

First thing that stands out, you haven't started scheduler. So quartz is not running.

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();

After starting it the server will scan the db for job details/triggers and proceed accordingly.

However, you also want to add proper identities to your job detail/trigger. These are needed for quartz to create primary keys, otherwise you will get a SchedulerException.

IJobDetail job = JobBuilder.Create<CurrencyRatesJob>()
                           .WithIdentity("currencyJob", "group1")
                           .Build();

ITrigger trigger = TriggerBuilder.Create()
                   .StartNow()
                   .WithIdentity("currencyJob", "group1")
                   .WithSimpleSchedule(s => s.WithIntervalInSeconds(10).RepeatForever())
                   .Build();

Regarding your config file, you need to declare an appropriate quartz section and add the settings there (by default this is where StdSchedulerFactory looks to create properties)

<configuration>
  <configSections>
    <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
  </configSections>

  <quartz>
     <add key="quartz.scheduler.instanceName" value="MyQuartzScheduler" />
     <add key="quartz.scheduler.instanceId" value="AUTO" />
      ...
  </quartz>   
</configuration>

Upvotes: 1

sgmoore
sgmoore

Reputation: 16067

You need to call

  scheduler.Start();

Upvotes: 6

Related Questions