message_bus 3.4.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
- source 'https://rubygems.org'
3
-
4
- gem 'message_bus', path: '../..'
5
- gem 'redis'
6
- gem 'puma'
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'message_bus'
3
-
4
- MessageBus.configure(backend: :redis, url: ENV['REDISURL'])
5
- MessageBus.enable_diagnostics
6
-
7
- MessageBus.user_id_lookup do |_env|
8
- 1
9
- end
10
-
11
- MessageBus.is_admin_lookup do |_env|
12
- true
13
- end
14
-
15
- use MessageBus::Rack::Middleware
16
- run lambda { |_env|
17
- [
18
- 200,
19
- { "Content-Type" => "text/html" },
20
- ['Howdy. Check out <a href="/message-bus/_diagnostics">the diagnostics UI</a>.']
21
- ]
22
- }
@@ -1,62 +0,0 @@
1
- # frozen_string_literal: true
2
- # MessageBus diagnostics are used for troubleshooting the bus and optimising its configuration
3
- # @see MessageBus::Rack::Diagnostics
4
- class MessageBus::Diagnostics
5
- class << self
6
- # Enables diagnostics functionality
7
- # @param [MessageBus::Instance] bus a specific instance of message_bus
8
- # @return [void]
9
- def enable(bus = MessageBus)
10
- full_path = full_process_path
11
- start_time = Time.now.to_f
12
- hostname = get_hostname
13
-
14
- # it may make sense to add a channel per machine/host to streamline
15
- # process to process comms
16
- bus.subscribe('/_diagnostics/hup') do |msg|
17
- if Process.pid == msg.data["pid"] && hostname == msg.data["hostname"]
18
- sleep 4
19
- Process.kill("HUP", $$)
20
- end
21
- end
22
-
23
- bus.subscribe('/_diagnostics/discover') do |msg|
24
- bus.on_connect.call msg.site_id if bus.on_connect
25
- bus.publish '/_diagnostics/process-discovery', {
26
- pid: Process.pid,
27
- process_name: $0,
28
- full_path: full_path,
29
- uptime: (Time.now.to_f - start_time).to_i,
30
- hostname: hostname
31
- }, user_ids: [msg.data["user_id"]]
32
- bus.on_disconnect.call msg.site_id if bus.on_disconnect
33
- end
34
- end
35
-
36
- private
37
-
38
- def full_process_path
39
- begin
40
- system = `uname`.strip
41
- if system == "Darwin"
42
- `ps -o "comm=" -p #{Process.pid}`
43
- elsif system == "FreeBSD"
44
- `ps -o command -p #{Process.pid}`.split("\n", 2)[1].strip
45
- else
46
- info = `ps -eo "%p|$|%a" | grep '^\\s*#{Process.pid}'`
47
- info.strip.split('|$|')[1]
48
- end
49
- rescue
50
- # skip it ... not linux or something weird
51
- end
52
- end
53
-
54
- def get_hostname
55
- begin
56
- `hostname`.strip
57
- rescue
58
- # skip it
59
- end
60
- end
61
- end
62
- end
@@ -1,120 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MessageBus::Rack; end
4
-
5
- # Accepts requests from clients interested in using diagnostics functionality
6
- # @see MessageBus::Diagnostics
7
- class MessageBus::Rack::Diagnostics
8
- # @param [Proc] app the rack app
9
- # @param [Hash] config
10
- # @option config [MessageBus::Instance] :message_bus (`MessageBus`) a specific instance of message_bus
11
- def initialize(app, config = {})
12
- @app = app
13
- @bus = config[:message_bus] || MessageBus
14
- end
15
-
16
- JS_ASSETS = %w{
17
- jquery-1.8.2.js
18
- react.js
19
- react-dom.js
20
- babel.min.js
21
- message-bus.js
22
- application.jsx
23
- }
24
-
25
- # Process an HTTP request from a subscriber client
26
- # @param [Rack::Request::Env] env the request environment
27
- def call(env)
28
- return @app.call(env) unless env['PATH_INFO'].start_with? "#{@bus.base_route}message-bus/_diagnostics"
29
-
30
- route = env['PATH_INFO'].split("#{@bus.base_route}message-bus/_diagnostics")[1]
31
-
32
- if @bus.is_admin_lookup.nil? || !@bus.is_admin_lookup.call(env)
33
- return [403, {}, ['not allowed']]
34
- end
35
-
36
- return index unless route
37
-
38
- if route == '/discover'
39
- user_id = @bus.user_id_lookup.call(env)
40
- @bus.publish('/_diagnostics/discover', user_id: user_id)
41
- return [200, {}, ['ok']]
42
- end
43
-
44
- if route =~ /^\/hup\//
45
- hostname, pid = route.split('/hup/')[1].split('/')
46
- @bus.publish('/_diagnostics/hup', hostname: hostname, pid: pid.to_i)
47
- return [200, {}, ['ok']]
48
- end
49
-
50
- asset = route.split('/assets/')[1]
51
-
52
- if asset && JS_ASSETS.include?(asset)
53
- content = asset_contents(asset)
54
- return [200, { 'Content-Type' => 'application/javascript;charset=UTF-8' }, [content]]
55
- end
56
-
57
- [404, {}, ['not found']]
58
- end
59
-
60
- private
61
-
62
- def js_asset(name, type = "text/javascript")
63
- return generate_script_tag(name, type) unless @bus.cache_assets
64
-
65
- @@asset_cache ||= {}
66
- @@asset_cache[name] ||= generate_script_tag(name, type)
67
- @@asset_cache[name]
68
- end
69
-
70
- def generate_script_tag(name, type)
71
- "<script src='/message-bus/_diagnostics/assets/#{name}?#{file_hash(name)}' type='#{type}'></script>"
72
- end
73
-
74
- def file_hash(asset)
75
- require 'digest/sha1'
76
- Digest::SHA1.hexdigest(asset_contents(asset))
77
- end
78
-
79
- def asset_contents(asset)
80
- File.open(asset_path(asset)).read
81
- end
82
-
83
- def asset_path(asset)
84
- File.expand_path("../../../../assets/#{asset}", __FILE__)
85
- end
86
-
87
- def script_tags
88
- tags = []
89
-
90
- JS_ASSETS.each do |asset|
91
- type =
92
- if asset.end_with?('.js')
93
- 'text/javascript'
94
- elsif asset.end_with?('.jsx')
95
- 'text/jsx'
96
- end
97
-
98
- tags << js_asset(asset, type)
99
- end
100
-
101
- tags.join("\n")
102
- end
103
-
104
- def index
105
- html = <<~HTML
106
- <!DOCTYPE html>
107
- <html>
108
- <head>
109
- </head>
110
- <body>
111
- <div id="app"></div>
112
-
113
- #{script_tags}
114
- </body>
115
- </html>
116
- HTML
117
-
118
- [200, { "content-type" => "text/html;" }, [html]]
119
- end
120
- end