Run a job queue heartbeat every 5 minutes

This commit is contained in:
Rohan Mitchell
2016-04-13 15:53:53 +10:00
parent e6b1d545a3
commit eb846e27fd
4 changed files with 43 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
class HeartbeatJob
def perform
Spree::Config.last_job_queue_heartbeat_at = Time.now
end
end

View File

@@ -20,4 +20,7 @@ Spree::AppConfiguration.class_eval do
preference :account_invoices_monthly_rate, :decimal, default: 0
preference :account_invoices_monthly_cap, :decimal, default: 0
preference :account_invoices_tax_rate, :decimal, default: 0
# Monitoring
preference :last_job_queue_heartbeat_at, :string, default: nil
end

View File

@@ -4,9 +4,13 @@ require 'whenever'
env "MAILTO", "rohan@rohanmitchell.com"
# If we use -e with a file containing specs, rspec interprets it and filters out our examples
job_type :run_file, "cd :path; :environment_variable=:environment bundle exec script/rails runner :task :output"
job_type :enqueue_job, "cd :path; :environment_variable=:environment bundle exec script/rails runner 'Delayed::Job.enqueue(:task.new, priority: :priority)' :output"
every 1.hour do
rake 'openfoodnetwork:cache:check_products_integrity'
end
@@ -23,6 +27,10 @@ every 4.hours do
rake 'db2fog:backup'
end
every 5.minutes do
enqueue_job 'QueueHeartbeatJob', priority: 0
end
every 1.day, at: '1:00am' do
rake 'openfoodnetwork:billing:update_account_invoices'
end

View File

@@ -0,0 +1,27 @@
require 'spec_helper'
describe HeartbeatJob do
context "with time frozen" do
let(:run_time) { Time.zone.local(2016, 4, 13, 13, 0, 0) }
before { Spree::Config.last_job_queue_heartbeat_at = nil }
around do |example|
Timecop.freeze(run_time) { example.run }
end
it "updates the last_job_queue_heartbeat_at config var" do
run_job
Time.parse(Spree::Config.last_job_queue_heartbeat_at).should == run_time
end
end
private
def run_job
clear_jobs
Delayed::Job.enqueue HeartbeatJob.new
flush_jobs ignore_exceptions: false
end
end