Report job queue status via API

This commit is contained in:
Rohan Mitchell
2016-04-13 16:48:07 +10:00
parent 193580d5d9
commit 59b564c4be
3 changed files with 51 additions and 0 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

@@ -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

@@ -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