Commit 0d50fdfb authored by Tiger's avatar Tiger Committed by Bob Van Landuyt

Log an activity event when an Agent connects

Changelog: added
parent 6ba31f27
...@@ -4,6 +4,8 @@ module Clusters ...@@ -4,6 +4,8 @@ module Clusters
class Agent < ApplicationRecord class Agent < ApplicationRecord
self.table_name = 'cluster_agents' self.table_name = 'cluster_agents'
INACTIVE_AFTER = 1.hour.freeze
belongs_to :created_by_user, class_name: 'User', optional: true belongs_to :created_by_user, class_name: 'User', optional: true
belongs_to :project, class_name: '::Project' # Otherwise, it will load ::Clusters::Project belongs_to :project, class_name: '::Project' # Otherwise, it will load ::Clusters::Project
...@@ -33,5 +35,9 @@ module Clusters ...@@ -33,5 +35,9 @@ module Clusters
def has_access_to?(requested_project) def has_access_to?(requested_project)
requested_project == project requested_project == project
end end
def active?
agent_tokens.where("last_used_at > ?", INACTIVE_AFTER.ago).exists?
end
end end
end end
...@@ -28,8 +28,12 @@ module Clusters ...@@ -28,8 +28,12 @@ module Clusters
cache_attributes(track_values) cache_attributes(track_values)
# Use update_column so updated_at is skipped if can_update_track_values?
update_columns(track_values) if can_update_track_values? log_activity_event!(track_values[:last_used_at]) unless agent.active?
# Use update_column so updated_at is skipped
update_columns(track_values)
end
end end
private private
...@@ -44,5 +48,14 @@ module Clusters ...@@ -44,5 +48,14 @@ module Clusters
real_last_used_at.nil? || real_last_used_at.nil? ||
(Time.current - real_last_used_at) >= last_used_at_max_age (Time.current - real_last_used_at) >= last_used_at_max_age
end end
def log_activity_event!(recorded_at)
agent.activity_events.create!(
kind: :agent_connected,
level: :info,
recorded_at: recorded_at,
agent_token: self
)
end
end end
end end
...@@ -18,7 +18,10 @@ module Clusters ...@@ -18,7 +18,10 @@ module Clusters
nullify_if_blank :detail nullify_if_blank :detail
enum kind: { enum kind: {
token_created: 0 token_created: 0,
token_revoked: 1,
agent_connected: 2,
agent_disconnected: 3
}, _prefix: true }, _prefix: true
enum level: { enum level: {
......
...@@ -75,4 +75,37 @@ RSpec.describe Clusters::Agent do ...@@ -75,4 +75,37 @@ RSpec.describe Clusters::Agent do
expect(agent.has_access_to?(create(:project))).to be_falsey expect(agent.has_access_to?(create(:project))).to be_falsey
end end
end end
describe '#active?' do
let_it_be(:agent) { create(:cluster_agent) }
let!(:token) { create(:cluster_agent_token, agent: agent, last_used_at: last_used_at) }
subject { agent.active? }
context 'agent has never connected' do
let(:last_used_at) { nil }
it { is_expected.to be_falsey }
end
context 'agent has connected, but not recently' do
let(:last_used_at) { 2.hours.ago }
it { is_expected.to be_falsey }
end
context 'agent has connected recently' do
let(:last_used_at) { 2.minutes.ago }
it { is_expected.to be_truthy }
end
context 'agent has multiple tokens' do
let!(:inactive_token) { create(:cluster_agent_token, agent: agent, last_used_at: 2.hours.ago) }
let(:last_used_at) { 2.minutes.ago }
it { is_expected.to be_truthy }
end
end
end end
...@@ -39,7 +39,9 @@ RSpec.describe Clusters::AgentToken do ...@@ -39,7 +39,9 @@ RSpec.describe Clusters::AgentToken do
end end
describe '#track_usage', :clean_gitlab_redis_cache do describe '#track_usage', :clean_gitlab_redis_cache do
let(:agent_token) { create(:cluster_agent_token) } let_it_be(:agent) { create(:cluster_agent) }
let(:agent_token) { create(:cluster_agent_token, agent: agent) }
subject { agent_token.track_usage } subject { agent_token.track_usage }
...@@ -73,6 +75,34 @@ RSpec.describe Clusters::AgentToken do ...@@ -73,6 +75,34 @@ RSpec.describe Clusters::AgentToken do
expect_redis_update expect_redis_update
end end
end end
context 'agent is inactive' do
before do
allow(agent).to receive(:active?).and_return(false)
end
it 'creates an activity event' do
expect { subject }.to change { agent.activity_events.count }
event = agent.activity_events.last
expect(event).to have_attributes(
kind: 'agent_connected',
level: 'info',
recorded_at: agent_token.reload.read_attribute(:last_used_at),
agent_token: agent_token
)
end
end
context 'agent is active' do
before do
allow(agent).to receive(:active?).and_return(true)
end
it 'does not create an activity event' do
expect { subject }.not_to change { agent.activity_events.count }
end
end
end end
def expect_redis_update def expect_redis_update
......
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