Commit b06f1781 authored by Valery Sizov's avatar Valery Sizov

[Geo] If node status is not build, return 404 error

parent 885fb839
......@@ -91,8 +91,18 @@ class GeoNodeStatus < ActiveRecord::Base
end
def self.fast_current_node_status
attrs = Rails.cache.read(cache_key) || {}
new(attrs)
attrs = Rails.cache.read(cache_key)
if attrs
new(attrs)
else
spawn_worker
nil
end
end
def self.spawn_worker
::Geo::MetricsUpdateWorker.perform_async
end
def self.cache_key
......
......@@ -67,7 +67,7 @@ module API
if geo_node.current?
GeoNodeStatus.fast_current_node_status
else
geo_node.find_or_build_status
geo_node.status
end
end
end
......
......@@ -20,6 +20,8 @@ describe GeoNodeStatus, :geo do
describe '#fast_current_node_status' do
it 'reads the cache and spawns the worker' do
expect(described_class).to receive(:spawn_worker).once
rails_cache = double
expect(rails_cache).to receive(:read).with(described_class.cache_key)
expect(Rails).to receive(:cache).and_return(rails_cache)
......
......@@ -92,6 +92,7 @@ describe API::GeoNodes, :geo, :prometheus, api: true do
it 'fetches the current node status' do
stub_current_geo_node(secondary)
expect(GeoNodeStatus).to receive(:fast_current_node_status).and_return(secondary_status)
expect(GeoNode).to receive(:find).and_return(secondary)
get api("/geo_nodes/#{secondary.id}/status", admin)
......@@ -100,6 +101,16 @@ describe API::GeoNodes, :geo, :prometheus, api: true do
expect(response).to match_response_schema('public_api/v4/geo_node_status', dir: 'ee')
end
it 'shows 404 response if current node status does not exist yet' do
stub_current_geo_node(secondary)
expect(GeoNode).to receive(:find).and_return(secondary)
get api("/geo_nodes/#{secondary.id}/status", admin)
expect(response).to have_gitlab_http_status(404)
end
it_behaves_like '404 response' do
let(:request) { get api("/geo_nodes/#{unexisting_node_id}/status", admin) }
end
......@@ -123,6 +134,8 @@ describe API::GeoNodes, :geo, :prometheus, api: true do
end
it 'returns 200 for the primary node' do
expect(GeoNodeStatus).to receive(:fast_current_node_status).and_return(secondary_status)
post api("/geo_nodes/#{primary.id}/repair", admin)
expect(response).to have_gitlab_http_status(200)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment