Merge branch 'master' of github.com:openfoodfoundation/openfoodnetwork into state_translation_override

Conflicts:
	spec/lib/open_food_network/order_cycle_management_report_spec.rb
This commit is contained in:
Maikel Linke
2016-04-15 08:27:09 +10:00
9 changed files with 155 additions and 1 deletions

View File

@@ -0,0 +1,17 @@
module Api
class StatusesController < BaseController
respond_to :json
def job_queue
render json: {alive: job_queue_alive?}
end
private
def job_queue_alive?
Spree::Config.last_job_queue_heartbeat_at.present? &&
Time.parse(Spree::Config.last_job_queue_heartbeat_at) > 6.minutes.ago
end
end
end

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

@@ -143,6 +143,10 @@ Openfoodnetwork::Application.routes.draw do
get :managed, on: :collection
get :accessible, on: :collection
end
resource :status do
get :job_queue
end
end
namespace :open_food_network do

View File

@@ -4,8 +4,11 @@ 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/enqueue :task :priority :output"
every 1.hour do
rake 'openfoodnetwork:cache:check_products_integrity'
@@ -23,6 +26,10 @@ every 4.hours do
rake 'db2fog:backup'
end
every 5.minutes do
enqueue_job 'HeartbeatJob', priority: 0
end
every 1.day, at: '1:00am' do
rake 'openfoodnetwork:billing:update_account_invoices'
end

61
script/enqueue Executable file
View File

@@ -0,0 +1,61 @@
#!/usr/bin/env ruby
# Push a job onto the Delayed Job queue without booting the Rails stack
# Perfect for calling via cron.
#
# Use like this:
#
# ./script/enqueue <JobClassName>
# ./script/enqueue Background::ImportJobs
require 'erb'
require 'yaml'
ENV["RAILS_ENV"] ||= "development"
DATABASE_CONFIG = File.expand_path("../../config/database.yml", __FILE__)
def psql
raise "Missing database.yml" unless File.exists?(DATABASE_CONFIG)
file = File.read(DATABASE_CONFIG)
erb = ERB.new(file).result
env = ENV["RAILS_ENV"]
config = YAML.load(erb)[env]
raise "Missing config for #{env} environment" unless config
"psql".tap do |s|
s << " --host #{config['host']}" if config['host']
s << " --user #{config['username']}" if config['username']
s << " --port #{config['port']}" if config['port']
s << " #{config['database']}"
end
end
def enqueue_delayed_job(handler, priority=nil)
time = Time.now.utc.strftime("%Y-%m-%d %H:%M:%S")
priority ||= 50
sql = <<-SQL
INSERT INTO delayed_jobs (
handler,
created_at,
updated_at,
run_at,
priority
) VALUES (
'--- !ruby/object:#{handler} {}\n',
'#{time}',
'#{time}',
'#{time}',
#{priority}
);
SQL
IO.popen(psql, "w") do |io|
io.write sql
end
end
enqueue_delayed_job ARGV[0], ARGV[1]

View File

@@ -0,0 +1,30 @@
require 'spec_helper'
module Api
describe StatusesController do
render_views
describe "job queue status" do
it "returns alive when up to date" do
Spree::Config.last_job_queue_heartbeat_at = Time.now
spree_get :job_queue
response.should be_success
response.body.should == {alive: true}.to_json
end
it "returns dead otherwise" do
Spree::Config.last_job_queue_heartbeat_at = 10.minutes.ago
spree_get :job_queue
response.should be_success
response.body.should == {alive: false}.to_json
end
it "returns dead when no heartbeat recorded" do
Spree::Config.last_job_queue_heartbeat_at = nil
spree_get :job_queue
response.should be_success
response.body.should == {alive: false}.to_json
end
end
end
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

View File

@@ -92,7 +92,7 @@ module OpenFoodNetwork
# payment2 = create(:payment, order: order2, payment_method: pm2)
subject.stub(:params).and_return(payment_method_in: [pm1.id, pm3.id] )
subject.filter(orders).should == [order1, order3]
subject.filter(orders).should match_array [order1, order3]
end
it "filters to a shipping method" do