Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz trigger based on file existence?

I am very new to using Quartz and I have a question regarding triggers. Is it possible to trigger based on file existence? I would like to have Quartz run a job until a certain file is found, then stop running that job and perhaps move on to a different one.

For example, I would like to do something like this:

(1) Job1 checks if File.txt exists in a given directory every 60 seconds.

(2) If File.txt is found, trigger Job2 to start. Job1 stops checking for file existence.

Right now, I have:

// Job definitions
var Job1 = JobBuilder.Create<TestEmail>().WithIdentity("job1", "group1").Build();
var Job2 = JobBuilder.Create<TestFileTrigger>().WithIdentity("job2", "group2").Build();

// Triggers
ITrigger trigger1 = TriggerBuilder.Create()
      .WithIdentity("trigger1", "group1").StartNow()
      .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
      .Build();

ITrigger trigger2 = TriggerBuilder.Create()
      .WithIdentity("trigger2", "group2").StartNow()
      .Build();

// Schedule jobs
scheduler.ScheduleJob(Job1, trigger1);
if (TestFileTrigger.fileExistence == true) 
{
     scheduler.ScheduleJob(Job2, trigger2);
}

but it seems like Job2 never starts.

TestEmail and TestFileTrigger simply print to console at the moment. The boolean TestFileTrigger.fileExistence comes from checking if a file exists at a given location (which it does).

Edit: TestFileTrigger.fileExistence is a boolean. Added definitions of Job1/Job2 if that helps.

Edit: I found that if I put Thread.Sleep(TimeSpan.FromSeconds(x)); before the if statement, the if statement will run if the condition is met. (Where x is some number of seconds.) Why does it work in this case, but not otherwise? I cannot always know how many seconds it will take for the condition to be met.

like image 713
user2424607 Avatar asked Nov 10 '22 05:11

user2424607


1 Answers

What type of application is this?

If this is, for example, a Windows service - to keep the scheduler alive so that it hangs around to execute the jobs according to your triggers, you need to do something like:

ThreadStart start = SetupSchedules()
var thread = new Thread(start) { Name = "mysvc" }
thread.Start();

.. this would go into the override void OnStart(string[] args) method of the Windows service.

The SetupSchedules method would be the thing that hooks into Quartz jobs and would be something like (The code you've written above in the OP would make a good start):

ISchedulerFactory factory = new StdSchedulerFactory();
JobScheduler = factory.GetScheduler();
JobScheduler.ScheduleJob(job1, trigger1);

This should keep it alive so that it executes the jobs. I've omitted a bunch of stuff here, but hopefully this should give you a few pointers to help weave it into your app.

You will also need something like this:

private void ManageThread()
{
   var _thread = Thread.CurrentThread;
   while (!_threadMustStop) // false by default, set this to true in a 'shutdown' process
   {
       Thread.Sleep(10000);
   }
}

...which you call from your SetupSchedules method

like image 80
SpaceBison Avatar answered Nov 14 '22 22:11

SpaceBison