-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathapp.rb
More file actions
executable file
·59 lines (47 loc) · 1.41 KB
/
app.rb
File metadata and controls
executable file
·59 lines (47 loc) · 1.41 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'bundler/setup'
Bundler.require
EventMachine.set_max_timers 1_250_000
DB = Redis.new
DB.set :connections, 0
module FormatHelper
def humanize_number n
n.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, '\\1,')
end
module_function :humanize_number
end
class App < E
map '/'
# index and status_watcher actions should return event-stream content type
before :index, :status_watcher do
content_type 'text/event-stream'
end
def index
stream :keep_open do |stream|
# communicate to client every 15 seconds
timer = EM.add_periodic_timer(15) {stream << "\0"}
stream.errback do # when connection closed/errored:
DB.decr :connections # 1. decrement connections amount by 1
timer.cancel # 2. cancel timer that communicate to client
end
# increment connections amount by 1
DB.incr :connections
end
end
# frontend for status watchers - http://localhost:5252/status
def status
render
end
# backend for status watchers
def status_watcher
stream :keep_open do |stream|
# adding a timer that will update status watchers every second
timer = EM.add_periodic_timer(1) do
connections = FormatHelper.humanize_number(DB.get :connections)
stream << "data: %s\n\n" % connections
end
stream.errback { timer.cancel } # cancel timer if connection closed/errored
end
end
def get_ping
end
end