Sha256: 8eab6817d6f79efa7b1b6a7525d2476de4640d17f23f096a09745bb01fa70b7d

Contents?: true

Size: 943 Bytes

Versions: 9

Compression:

Stored size: 943 Bytes

Contents

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

    cron_job.update completed_at: Time.now, next_run_at: task.next_run_at
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
roqua-support-0.3.0 lib/roqua/scheduling/scheduler.rb
roqua-support-0.1.34 lib/roqua/scheduling/scheduler.rb
roqua-support-0.1.33 lib/roqua/scheduling/scheduler.rb
roqua-support-0.1.32 lib/roqua/scheduling/scheduler.rb
roqua-support-0.1.31 lib/roqua/scheduling/scheduler.rb
roqua-support-0.1.30 lib/roqua/scheduling/scheduler.rb
roqua-support-0.1.29 lib/roqua/scheduling/scheduler.rb
roqua-support-0.1.28 lib/roqua/scheduling/scheduler.rb
roqua-support-0.1.27 lib/roqua/scheduling/scheduler.rb