Skip to content

Commit

Permalink
Add logging to labels, along with a command to show it
Browse files Browse the repository at this point in the history
Per request from @theckman, here's a very rudimentary logging setup for
labels.
  • Loading branch information
esigler committed Jan 13, 2015
1 parent 54effd8 commit 9b911bc
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Expand Up @@ -12,4 +12,4 @@ MethodLength:
Max: 20

ClassLength:
Max: 105
Max: 150
16 changes: 16 additions & 0 deletions lib/lita/handlers/locker_misc.rb
Expand Up @@ -30,6 +30,22 @@ class LockerMisc < Handler
help: { t('help.dequeue.syntax') => t('help.dequeue.desc') }
)

route(
/^locker\slog\s#{LABEL_REGEX}$/,
:log,
command: true,
help: { t('help.log.syntax.') => t('help.log.desc') }
)

def log(response)
name = response.match_data['label']
return response.reply(failed(t('subject.does_not_exist', name: name))) unless Label.exists?(name)
l = Label.new(name)
l.journal.range(-10, -1).each do |entry|
response.reply(t('label.log_entry', entry: entry))
end
end

def status(response)
name = response.match_data['label']
return response.reply(status_label(name)) if Label.exists?(name)
Expand Down
13 changes: 12 additions & 1 deletion lib/locker/label.rb
Expand Up @@ -12,6 +12,7 @@ class Label

set :membership
list :wait_queue
list :journal

lock :coord, expiration: 5

Expand All @@ -32,12 +33,13 @@ def self.create(key)
l = Label.new(key)
l.state = 'unlocked'
l.owner_id = ''
l.log('Created')
l
end

def self.delete(key)
fail 'Unknown label key' unless Label.exists?(key)
%w(state, owner_id, membership, wait_queue).each do |item|
%w(state, owner_id, membership, wait_queue, journal).each do |item|
redis.del("label:#{key}:#{item}")
end
redis.srem('label-list', Label.normalize(key))
Expand Down Expand Up @@ -67,6 +69,7 @@ def lock!(owner_id)
self.state = 'locked'
self.taken_at = Time.now.utc
end
log("Locked by #{owner_id}")
true
end

Expand All @@ -81,6 +84,7 @@ def unlock!
r.unlock!
end
end
log('Unlocked')

# FIXME: Possible race condition where resources become unavailable between unlock and relock
if wait_queue.count > 0
Expand All @@ -91,6 +95,7 @@ def unlock!
end

def steal!(owner_id)
log("Stolen from #{owner.id} to #{owner_id}")
wait_queue.unshift(owner_id)
self.unlock!
end
Expand All @@ -100,10 +105,12 @@ def locked?
end

def add_resource(resource)
log("Resource #{resource.id} added")
membership << resource.id
end

def remove_resource(resource)
log("Resource #{resource.id} removed")
membership.delete(resource.id)
end

Expand All @@ -130,6 +137,10 @@ def to_json

val.to_json
end

def log(statement)
journal << "#{Time.now.utc}: #{statement}"
end
end

def label_ownership(name)
Expand Down
4 changes: 4 additions & 0 deletions locales/en.yml
Expand Up @@ -7,6 +7,9 @@ en:
already_unlocked: "%{label} was already unlocked"
self: Why are you stealing the lock from yourself?
help:
log:
syntax: locker log <label>
desc: Show up to the last 10 activity log entries for <label>
dequeue:
syntax: locker dequeue <label>
desc: Remove yourself from the queue for a label
Expand Down Expand Up @@ -72,6 +75,7 @@ en:
subject:
does_not_exist: "Sorry, that does not exist"
label:
log_entry: "%{entry}"
self_lock: "You already have the lock on %{name}"
unlock: "%{name} unlocked"
owned_lock: "%{name} is locked by %{owner_name} %{mention} (taken %{time}), you have been added to the queue, type 'locker dequeue %{name}' to be removed"
Expand Down
28 changes: 24 additions & 4 deletions spec/lita/handlers/locker_misc_spec.rb
Expand Up @@ -17,8 +17,14 @@
it { is_expected.to route_command("locker status #{r}").to(:status) }
end

it { is_expected.to route_command('locker list @alice').to(:list) }
it { is_expected.to route_command('locker list Alice').to(:list) }
it do
is_expected.to route_command('locker list @alice').to(:list)
is_expected.to route_command('locker list Alice').to(:list)
end

it do
is_expected.to route_command('locker log something').to(:log)
end

let(:alice) do
Lita::User.create('9001@hipchat', name: 'Alice', mention_name: 'alice')
Expand All @@ -28,6 +34,22 @@
Lita::User.create('9002@hipchat', name: 'Bob', mention_name: 'bob')
end

describe '#log' do
it 'shows an activity log for labels if one exists' do
send_command('locker resource create bar')
send_command('locker label create foo')
send_command('locker label add bar to foo')
send_command('lock foo', as: alice)
send_command('locker log foo')
expect(replies.count).to eq(7)
end

it 'shows a warning if the label does not exist' do
send_command('locker log something')
expect(replies.last).to eq('(failed) Sorry, that does not exist')
end
end

describe '#dequeue' do
it 'shows a successful dequeue' do
send_command('locker resource create bar')
Expand Down Expand Up @@ -75,8 +97,6 @@
end
end

describe '#user_queue'

describe '#user_locks' do
it 'shows if a user has taken any locks' do
send_command('locker resource create foobar')
Expand Down

0 comments on commit 9b911bc

Please sign in to comment.