-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatcher.rb
44 lines (39 loc) · 1.18 KB
/
dispatcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Providence
class Dispatcher < Strategy
class << self
include Configurable
end
include Configurable
inherit_configuration_from Strategy
def self.strategies
configuration.strategies || []
end
def initialize agency, transport
@strategies = configuration.strategies.map { |klass| klass.new agency, transport }
end
def activate
EM::MultiRequest.new.tap do |multi|
@strategies.each { |st| multi.add st, st.activate }
end
end
def do_visits_between station, start_s, end_s, limit, **_
start_time = Time.parse(start_s)
end_time = Time.parse(end_s)
profile_time = Time.now
log = proc do |result|
puts <<~EOF
Received call to `providence.visits_between`:
stop: #{station}
start: #{start_s}
end: #{end_s}
count: #{limit}
Responded in #{(Time.now - profile_time) * 1000}ms with:
#{result}
EOF
end
@strategies.lazy.map do |strategy|
strategy.visits_between(station, start_time, end_time, limit)
end.select(&:itself).first.callback { |result| log.call result }
end
end
end