0

I'm using newest 3.0.6 version of Quartz.net I want o schedule a process to run each day (currently in example it's every 40seconds but I will change that) I have method where I call it to execute, however it does not seem to start any process which it should as I initiated it in `Job' class. I'm not sure why it's not trigerring?

 public static void Run()
    {

        var isRunning = false;
        IServiceCollection services = new ServiceCollection();
        Startup startup = new Startup();
        var serviceProvider = startup.ConfigureServices(services);
        IWindsorContainer _container = serviceProvider.GetService<IWindsorContainer>();
        while (true)
        {
            if (!isRunning)
            {
                isRunning = true;
                var configuration = _container.Resolve<IConfigurationRoot>();
                var _process = _container.Resolve<Process.Process>();
                int secondsForSleep = Convert.ToUInt16(configuration[Enums.SleepTime]);
                {
                    try
                    {
                        execute();
                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        isRunning = false;
                        Thread.Sleep(secondsForSleep);
                        _container.Release(_process);
                    }
                }
            }
        }
    }

    public static async void execute()
    {

        // construct a scheduler factory
        NameValueCollection props = new NameValueCollection
        {
            { "quartz.serializer.type", "binary" }
        };
        StdSchedulerFactory factory = new StdSchedulerFactory(props);

        // get a scheduler
        IScheduler sched = await factory.GetScheduler();
        await sched.Start();


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

        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("myTrigger", "group1")
            .StartNow()
            .WithSimpleSchedule(x => x
                .WithIntervalInSeconds(1)
                .RepeatForever())
            .Build();

        await sched.ScheduleJob(job, trigger);

    }
}

public class Job : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        JobKey key = context.JobDetail.Key;
        IServiceCollection services = new ServiceCollection();
        Startup startup = new Startup();
        var serviceProvider = startup.ConfigureServices(services);
        IWindsorContainer _container = serviceProvider.GetService<IWindsorContainer>();
        var configuration = _container.Resolve<IConfigurationRoot>();
        var _process = _container.Resolve<Process.Process>();
        Task t = new Task(() =>
        {
             _process.MainProcess();
        });
       t.Start();
        await t;
    }
}

Do I incorrectly set IJobDetail object or something? does anyone had a similar problem? Seems like i did eveerything according to documentation but I still can't get it working

user122222
  • 2,179
  • 4
  • 35
  • 78

1 Answers1

1

This is not directly the answer to your question but may help you if the problem is related to below.

Look, you have defined the execute method as async void. As a result, you cannot call it via await execute() and so

  • your try-catch block will not handle any exceptions raised in the execute method
  • finally block runs directly after calling execute() method (not waiting for method finishing)

In other words, change your code to async Task execute() and await execute() and test it again.


For more theory I suggest you read this SO: async/await - when to return a Task vs void? and msdn: Async/Await - Best Practices

Set
  • 47,577
  • 22
  • 132
  • 150