message_bus 3.3.6 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +3 -2
  3. data/.github/workflows/ci.yml +79 -32
  4. data/.prettierrc +1 -0
  5. data/CHANGELOG +104 -53
  6. data/DEV.md +0 -2
  7. data/Gemfile +0 -27
  8. data/LICENSE +1 -1
  9. data/README.md +40 -62
  10. data/Rakefile +31 -26
  11. data/assets/message-bus-ajax.js +3 -3
  12. data/bench/codecs/marshal.rb +1 -1
  13. data/bench/codecs/packed_string.rb +1 -1
  14. data/docker-compose.yml +1 -1
  15. data/examples/bench/bench.lua +2 -2
  16. data/lib/message_bus/backends/base.rb +8 -3
  17. data/lib/message_bus/backends/memory.rb +6 -0
  18. data/lib/message_bus/backends/postgres.rb +29 -16
  19. data/lib/message_bus/backends/redis.rb +11 -2
  20. data/lib/message_bus/client.rb +6 -7
  21. data/lib/message_bus/connection_manager.rb +1 -1
  22. data/lib/message_bus/distributed_cache.rb +3 -1
  23. data/lib/message_bus/http_client.rb +2 -2
  24. data/lib/message_bus/rack/middleware.rb +6 -6
  25. data/lib/message_bus/rack/thin_ext.rb +2 -1
  26. data/lib/message_bus/version.rb +1 -1
  27. data/lib/message_bus.rb +47 -77
  28. data/message_bus.gemspec +21 -3
  29. data/package-lock.json +1575 -23
  30. data/package.json +9 -7
  31. data/spec/assets/SpecHelper.js +6 -5
  32. data/spec/assets/message-bus.spec.js +9 -6
  33. data/spec/helpers.rb +23 -7
  34. data/spec/integration/http_client_spec.rb +1 -1
  35. data/spec/lib/fake_async_middleware.rb +1 -0
  36. data/spec/lib/message_bus/backend_spec.rb +15 -46
  37. data/spec/lib/message_bus/client_spec.rb +7 -6
  38. data/spec/lib/message_bus/connection_manager_spec.rb +4 -0
  39. data/spec/lib/message_bus/distributed_cache_spec.rb +5 -7
  40. data/spec/lib/message_bus/multi_process_spec.rb +21 -10
  41. data/spec/lib/message_bus/rack/middleware_spec.rb +8 -44
  42. data/spec/lib/message_bus/timer_thread_spec.rb +1 -5
  43. data/spec/lib/message_bus_spec.rb +22 -9
  44. data/spec/performance/publish.rb +4 -4
  45. data/spec/spec_helper.rb +8 -9
  46. data/spec/support/jasmine-browser.json +16 -0
  47. data/vendor/assets/javascripts/message-bus-ajax.js +3 -3
  48. metadata +220 -19
  49. data/assets/application.jsx +0 -121
  50. data/assets/babel.min.js +0 -25
  51. data/assets/react-dom.js +0 -19851
  52. data/assets/react.js +0 -3029
  53. data/examples/diagnostics/Gemfile +0 -6
  54. data/examples/diagnostics/config.ru +0 -22
  55. data/lib/message_bus/diagnostics.rb +0 -62
  56. data/lib/message_bus/rack/diagnostics.rb +0 -98
  57. data/spec/assets/support/jasmine.yml +0 -126
  58. data/spec/assets/support/jasmine_helper.rb +0 -11
@@ -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,98 +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
- # Process an HTTP request from a subscriber client
17
- # @param [Rack::Request::Env] env the request environment
18
- def call(env)
19
- return @app.call(env) unless env['PATH_INFO'].start_with? "#{@bus.base_route}message-bus/_diagnostics"
20
-
21
- route = env['PATH_INFO'].split("#{@bus.base_route}message-bus/_diagnostics")[1]
22
-
23
- if @bus.is_admin_lookup.nil? || !@bus.is_admin_lookup.call(env)
24
- return [403, {}, ['not allowed']]
25
- end
26
-
27
- return index unless route
28
-
29
- if route == '/discover'
30
- user_id = @bus.user_id_lookup.call(env)
31
- @bus.publish('/_diagnostics/discover', user_id: user_id)
32
- return [200, {}, ['ok']]
33
- end
34
-
35
- if route =~ /^\/hup\//
36
- hostname, pid = route.split('/hup/')[1].split('/')
37
- @bus.publish('/_diagnostics/hup', hostname: hostname, pid: pid.to_i)
38
- return [200, {}, ['ok']]
39
- end
40
-
41
- asset = route.split('/assets/')[1]
42
- if asset && !asset !~ /\//
43
- content = asset_contents(asset)
44
- split = asset.split('.')
45
- return [200, { 'Content-Type' => 'application/javascript;charset=UTF-8' }, [content]]
46
- end
47
-
48
- [404, {}, ['not found']]
49
- end
50
-
51
- private
52
-
53
- def js_asset(name, type = "text/javascript")
54
- return generate_script_tag(name, type) unless @bus.cache_assets
55
-
56
- @@asset_cache ||= {}
57
- @@asset_cache[name] ||= generate_script_tag(name, type)
58
- @@asset_cache[name]
59
- end
60
-
61
- def generate_script_tag(name, type)
62
- "<script src='/message-bus/_diagnostics/assets/#{name}?#{file_hash(name)}' type='#{type}'></script>"
63
- end
64
-
65
- def file_hash(asset)
66
- require 'digest/sha1'
67
- Digest::SHA1.hexdigest(asset_contents(asset))
68
- end
69
-
70
- def asset_contents(asset)
71
- File.open(asset_path(asset)).read
72
- end
73
-
74
- def asset_path(asset)
75
- File.expand_path("../../../../assets/#{asset}", __FILE__)
76
- end
77
-
78
- def index
79
- html = <<~HTML
80
- <!DOCTYPE html>
81
- <html>
82
- <head>
83
- </head>
84
- <body>
85
- <div id="app"></div>
86
- #{js_asset "jquery-1.8.2.js"}
87
- #{js_asset "react.js"}
88
- #{js_asset "react-dom.js"}
89
- #{js_asset "babel.min.js"}
90
- #{js_asset "message-bus.js"}
91
- #{js_asset "application.jsx", "text/jsx"}
92
- </body>
93
- </html>
94
- HTML
95
-
96
- [200, { "content-type" => "text/html;" }, [html]]
97
- end
98
- end
@@ -1,126 +0,0 @@
1
- # src_files
2
- #
3
- # Return an array of filepaths relative to src_dir to include before jasmine specs.
4
- # Default: []
5
- #
6
- # EXAMPLE:
7
- #
8
- # src_files:
9
- # - lib/source1.js
10
- # - lib/source2.js
11
- # - dist/**/*.js
12
- #
13
- src_files:
14
- - assets/message-bus.js
15
- - assets/message-bus-ajax.js
16
-
17
-
18
- # stylesheets
19
- #
20
- # Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs.
21
- # Default: []
22
- #
23
- # EXAMPLE:
24
- #
25
- # stylesheets:
26
- # - css/style.css
27
- # - stylesheets/*.css
28
- #
29
- # stylesheets:
30
- # - stylesheets/**/*.css
31
-
32
- # helpers
33
- #
34
- # Return an array of filepaths relative to spec_dir to include before jasmine specs.
35
- # Default: ["helpers/**/*.js"]
36
- #
37
- # EXAMPLE:
38
- #
39
- # helpers:
40
- # - helpers/**/*.js
41
- #
42
- helpers:
43
- - 'SpecHelper.js'
44
-
45
- # spec_files
46
- #
47
- # Return an array of filepaths relative to spec_dir to include.
48
- # Default: ["**/*[sS]pec.js"]
49
- #
50
- # EXAMPLE:
51
- #
52
- # spec_files:
53
- # - **/*[sS]pec.js
54
- #
55
- spec_files:
56
- - '**/*[sS]pec.js'
57
-
58
- # src_dir
59
- #
60
- # Source directory path. Your src_files must be returned relative to this path. Will use root if left blank.
61
- # Default: project root
62
- #
63
- # EXAMPLE:
64
- #
65
- # src_dir: public
66
- #
67
- src_dir:
68
-
69
- # spec_dir
70
- #
71
- # Spec directory path. Your spec_files must be returned relative to this path.
72
- # Default: spec/javascripts
73
- #
74
- # EXAMPLE:
75
- #
76
- # spec_dir: spec/javascripts
77
- #
78
- spec_dir: spec/assets
79
-
80
- spec_helper: spec/assets/support/jasmine_helper.rb
81
-
82
- # boot_dir
83
- #
84
- # Boot directory path. Your boot_files must be returned relative to this path.
85
- # Default: Built in boot file
86
- #
87
- # EXAMPLE:
88
- #
89
- # boot_dir: spec/javascripts/support/boot
90
- #
91
- boot_dir:
92
-
93
- # boot_files
94
- #
95
- # Return an array of filepaths relative to boot_dir to include in order to boot Jasmine
96
- # Default: Built in boot file
97
- #
98
- # EXAMPLE
99
- #
100
- # boot_files:
101
- # - '**/*.js'
102
- #
103
- boot_files:
104
-
105
- # rack_options
106
- #
107
- # Extra options to be passed to the rack server
108
- # by default, Port and AccessLog are passed.
109
- #
110
- # This is an advanced options, and left empty by default
111
- #
112
- # EXAMPLE
113
- #
114
- # rack_options:
115
- # server: 'thin'
116
-
117
- # random
118
- #
119
- # Run specs in semi-random order.
120
- # Default: false
121
- #
122
- # EXAMPLE:
123
- #
124
- # random: true
125
- #
126
- random:
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
- Jasmine.configure do |_config|
3
- # patch for travis
4
- if ENV['TRAVIS']
5
- module ::Phantomjs
6
- def self.version
7
- @phantom_version ||= `phantomjs --version`.strip
8
- end
9
- end
10
- end
11
- end