require 'with_advisory_lock' class Roqua::Scheduling::Scheduler def ping with_advisory_lock do jobs_to_run.each do |cron_job| begin run_task cron_job rescue Exception => ex Roqua::Support::Errors.report(ex) raise ex if Rails.env.test? end end end end def jobs_to_run Roqua::Scheduling::CronJob.where('next_run_at <= ?', Time.now) end def tasks schedule.tasks end def schedule Roqua::Scheduling::Schedule.current_schedule end def advisory_lock_name "#{ActiveRecord::Base.connection_config[:database]}_cron_lock" end private def with_advisory_lock ActiveRecord::Base.with_advisory_lock(advisory_lock_name, timeout_seconds: 0) do yield end end def run_task(cron_job) task = schedule.tasks[cron_job.name] task.run Appsignal.increment_counter("scheduler.run_task.completed", 1, task_name: task.name) cron_job.update completed_at: Time.now, next_run_at: task.next_run_at end end