Question

When creating a cron job through the Rufus Scheduler, is there some way within the code-block for the cron job to determine the job_id of the cron job itself?

What I want to do is something like this:

scheduler.cron '0 0 0 * * * Etc/GMT+0' do
  # Nix existing jobs.
  scheduler.cron_jobs.values.map { |j| if j.job_id != self.job_id then j.unschedule end }     
  ...
end

The problem is self.job_id. Any suggestions on how to retrieve the current job_id of the cron that is currently being triggered?

I know there is something like "triggered_threads", which returned currently running jobs. But I want something more specific: the job_id of the code-block I am currently within.

Is this possible?

Was it helpful?

Solution

I was able to resolve this issue by supplying the scheduler.cron method invocation with the an optional parameter that explicitly identified the new job's ID:

scheduler.cron '0 0 0 * * * Etc/GMT+0', {:job_id => explicit_job_id} do
  # Nix existing jobs.
  scheduler.cron_jobs.values.map { |j| if j.job_id != self.job_id then j.unschedule end }     
  ...
end

where explicit_job_id is some variable defined before the scheduler.cron invocation is made.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top