Skip to content

Commit 88f250c

Browse files
committed
deep symbolize keys on messages
1 parent c1b0fd1 commit 88f250c

File tree

4 files changed

+62
-23
lines changed

4 files changed

+62
-23
lines changed

lib/travis/hub/handler.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
require 'travis/hub/helper/context'
2+
require 'travis/hub/helper/hash'
23
require 'travis/hub/helper/string'
34
require 'travis/hub/service'
45

56
module Travis
67
module Hub
78
class Handler
8-
include Helper::Context, Helper::String
9+
include Helper::Context, Helper::Hash, Helper::String
910

1011
attr_reader :context, :type, :event, :payload, :object
1112

@@ -46,7 +47,7 @@ def normalize_event(event)
4647
end
4748

4849
def normalize_payload(payload)
49-
payload = payload.symbolize_keys
50+
payload = deep_symbolize_keys(payload)
5051
payload = normalize_state(payload)
5152
normalize_timestamps(payload)
5253
end

lib/travis/hub/helper/hash.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Travis
2+
module Hub
3+
module Helper
4+
module Hash
5+
def deep_symbolize_keys(hash)
6+
hash.map do |key, obj|
7+
obj = case obj
8+
when Array
9+
obj.map { |obj| deep_symbolize_keys(obj) }
10+
when ::Hash
11+
deep_symbolize_keys(obj)
12+
else
13+
obj
14+
end
15+
[key.to_sym, obj]
16+
end.to_h
17+
end
18+
end
19+
end
20+
end
21+
end

lib/travis/hub/service/state_update.rb

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
module Travis
22
module Hub
33
module Service
4-
class StateUpdate < Struct.new(:event, :data)
4+
class StateUpdate < Struct.new(:event, :data, :block)
55
class Counter < Struct.new(:job_id, :redis)
6-
TTL = 3600 * 12
6+
TTL = 3600 * 24
77

88
def count
99
@count ||= redis.get(key).to_i
1010
end
1111

12-
def increment
13-
count = redis.incr(key)
12+
def store(count)
13+
redis.set(key, count)
1414
redis.expire(key, TTL)
15-
count
1615
end
1716

1817
private
@@ -33,43 +32,60 @@ def key
3332
skip: 'Skipping the message.'
3433
}
3534

36-
def apply?
37-
return true if out_of_band? # TODO we need to increment the counter here, don't we?
38-
return missing unless given?
39-
apply = ordered? ? ordered : unordered
40-
return true unless ENV['UPDATE_COUNT']
41-
apply
35+
def apply
36+
if !enabled? || out_of_band?
37+
call
38+
elsif missing?
39+
missing
40+
elsif ordered?
41+
ordered
42+
else
43+
unordered
44+
end
4245
end
4346

4447
private
4548

49+
def call
50+
block.call
51+
end
52+
53+
def enabled?
54+
ENV['UPDATE_COUNT']
55+
end
56+
4657
def out_of_band?
4758
OUT_OF_BAND.include?(event)
4859
end
4960

50-
def given?
51-
!count.nil?
61+
def missing?
62+
count.nil?
5263
end
5364

5465
def missing
5566
warn :missing, event, job_id, counter.count
56-
true
67+
call
68+
store
5769
end
5870

5971
def ordered
6072
info :ordered, count, event, job_id, counter.count
61-
true
73+
call
74+
store
6275
end
6376

6477
def unordered
6578
warn :unordered, count, event, job_id, counter.count, ENV['UPDATE_COUNT'] ? MSGS[:skip] : ''
66-
false
6779
end
6880

6981
def ordered?
7082
count >= counter.count
7183
end
7284

85+
def store
86+
counter.store(count)
87+
end
88+
7389
def counter
7490
@counter ||= Counter.new(job_id, redis)
7591
end

lib/travis/hub/service/update_job.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ class UpdateJob < Struct.new(:event, :data)
2323
def run
2424
exclusive do
2525
validate
26-
update_job
27-
notify
26+
with_state_update do
27+
update_job
28+
notify
29+
end
2830
end
2931
end
3032
instrument :run
@@ -38,7 +40,6 @@ def job
3840
private
3941

4042
def update_job
41-
return skipped unless apply_state_update?
4243
return error_job if event == :reset && resets.limited? && !job.finished?
4344
return recancel if recancel?
4445
return skipped if skip_canceled?
@@ -74,8 +75,8 @@ def skipped
7475
warn :skipped, event, job.id, job.state, data[:state], data
7576
end
7677

77-
def apply_state_update?
78-
StateUpdate.new(context, event, data).apply?
78+
def with_state_update(&block)
79+
StateUpdate.new(context, event, data, block).apply
7980
end
8081

8182
def resets

0 commit comments

Comments
 (0)