short_bus 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bffed4e5f3b30116df68c93687d47a3169a9b429
4
+ data.tar.gz: a027b9b56200688d754b5172fd04a0f4cb17cfb4
5
+ SHA512:
6
+ metadata.gz: 3d5951e107d3cb9591a010f1ef1a250e40ed24ff1e2f706072514bb1542d5512e275f0b9ac988523203d0389459e2a9b6cad7de011c374607e9950437e1914e2
7
+ data.tar.gz: e2a8c52a1e1adbce728eed8cecca069ed2008ba398cebbe8d11f89430fccf5eeb603888237947a4088b666be48da250b8e97ba241632d604caa48449f14d412d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .sw[o-z]
11
+ .*.sw[o-z]
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in short_bus.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Rob Zwissler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,138 @@
1
+ # ShortBus
2
+ Easy to use multi-threaded pub-sub message dispatcher for implementing self-contained service-oriented Ruby apps.
3
+
4
+ ## What does it do?
5
+ The goal is to provide a minimal, lightweight message dispatcher/service API, with multi-threaded message publishing and subscription for Ruby closures (Lambdas/Blocks) and Methods.
6
+
7
+ ShortBus has no dependencies outside of the Ruby Core & Standard Libraries.
8
+
9
+ ## What are the components?
10
+ A service is a participant in the SOA (Service Oriented Architecture) for publishing and/or subscribing to messages. To receive messages, the service subscribes to the Driver (Driver#subscribe); and is run as a callback in a dedicated thread or thread pool.
11
+
12
+ A message (as simple as a String, but ultimately converted to a ShortBus::Message object) is what is received, routed and sent to the recipient services by the Driver. A message can have an optional payload object, and subscribers can return values directly back to the publisher by using the Message object as a Queue (see "Message return values" below).
13
+
14
+ The Driver (ShortBus::Driver) is the brains of the operation. Once instantiated, a dedicated thread monitors the incoming queue, converts and routes the messages to the appropriate subscribers based on the message\_spec(s) supplied at the time of subscription.
15
+
16
+ ## What does a message and a message\_spec look like?
17
+ In it's simplest form, a message can be a simple String like `'shutdown'`, but typically a more flexible, component based format is used, delimited by `::`, like `'OwnerService::Action::Argument'`. The Driver will convert the message String into a ShortBus::Message object before routing.
18
+
19
+ A message\_spec can be supplied when subscribing in order to select which messages are received (ie: run the callback). A message\_spec can be a String (`'shutdown'`), a wildcard String (`'OwnerService::**'`), a Regexp, or even an Array or Set of multiple Strings and/or Regexps.
20
+
21
+ #### Wildcard String?
22
+ To simplify filtering, a message\_spec String can contain a `*` or a `**` wildcard. A `*` wildcard matches just one field between `::` delimiters. A `**` wildcard matches one or more.
23
+
24
+ `'Service::*'` matches `'Service::Start'`, but not `'Service::Start::Now'`
25
+
26
+ `'Service::**'` matches both `'Service::Start'` and `'Service::Start::Now'`
27
+
28
+ Wilcard Strings are turned into Regexps by the Driver.
29
+
30
+ ## Message return values (Message as a Queue)
31
+ Typically speaking, services participating in a SOA do not get immediate return values, as an SOA is asynchronous. Since ShortBus generally runs as a monolithic application, we can cheat a bit for convenience, and pass return values back through the Message object (which is an inherited Queue class).
32
+
33
+ When a new Message is published via the Driver#publish method, the return value is the same Message object that subscribers receive.
34
+
35
+ The publisher can then #pop from that Message, which will block and wait for one of the subscribers to #push a "return value" into the Message on the other side. To make things more flexible, #pop (and #shift, #deq) has been extended to accept a numeric value, which acts as a timeout in seconds.
36
+
37
+ ```ruby
38
+ return_val = driver.publish('Testing::Message')
39
+ .pop(3)
40
+ ```
41
+
42
+ If you don't want to use the Message return value functionality, you can ignore it, and Ruby's garbage collection will destroy the Message automatically when all subscriber callbacks have completed.
43
+
44
+ ## How do you use it?
45
+
46
+ ```ruby
47
+ require_relative '../short_bus'
48
+
49
+ # Instantiate Driver, start message routing thread
50
+ #
51
+ driver = ShortBus::Driver.new
52
+
53
+ # Subscribes a block to all messages (no filtering)
54
+ #
55
+ driver.subscribe { |message| puts "1. I like all foods, including #{message}" }
56
+
57
+ # Subscribes a block with a message_spec filtering only some messages
58
+ # Also, replies back to the driver with a new message.
59
+ #
60
+ driver.subscribe(message_spec: 'Chocolate::**') do |message|
61
+ puts "2. Did I hear you say Chocolate? (#{message}). I know what I'm making."
62
+ 'Chocolate::And::Strawberries'
63
+ end
64
+
65
+ # Subscribes a block with a message_spec filtering only some messages
66
+ #
67
+ driver.subscribe(message_spec: '**::Strawberries') do |message|
68
+ puts "3. I only care about Strawberries: #{message}"
69
+ 'Strawberries'
70
+ end
71
+
72
+ # First lets just test it with an unrelated message
73
+ #
74
+ driver.publish 'Cookies::And::Cream'
75
+ sleep 0.1
76
+ puts
77
+
78
+ # Now lets try some interaction going between services
79
+ #
80
+ driver.publish 'Chocolate::Anything'
81
+ sleep 0.1
82
+ ```
83
+ And here's what it looks like when we run it:
84
+
85
+ ```
86
+ 1. I like all foods, including Cookies::And::Cream
87
+
88
+ 1. I like all foods, including Chocolate::Anything
89
+ 2. Did I hear you say Chocolate? (Chocolate::Anything). I know what I'm making.
90
+ 1. I like all foods, including Chocolate::And::Strawberries
91
+ 3. I only care about Strawberries: Chocolate::And::Strawberries
92
+ 1. I like all foods, including Strawberries
93
+ ```
94
+
95
+ ## TODO
96
+ - HIGH: make mixin for easier integration (provide #driver #publish #register #unregister; callback method -> #subscribe)
97
+ - HIGH: create class for automated benchmarking & testing
98
+ - HIGH: make examples easier to read, smaller, more repeatable
99
+ - MEDIUM: convert all Queue's to SizedQueue's, with reasonable/adjustable limits
100
+ - MEDIUM: cascade block to Service object to avoid block.to\_proc slowdown
101
+ - MEDIUM: document api , make gem, publish
102
+ - LOW: Redis connector with JSON and binary-serialized object passing
103
+ - LOW: class based services (object instantiation on callback -> ?)
104
+
105
+ ## Installation
106
+
107
+ Add this line to your application's Gemfile:
108
+
109
+ ```ruby
110
+ gem 'short_bus'
111
+ ```
112
+
113
+ And then execute:
114
+
115
+ $ bundle
116
+
117
+ Or install it yourself as:
118
+
119
+ $ gem install short_bus
120
+
121
+ ## Development
122
+
123
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
124
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
125
+ prompt that will allow you to experiment.
126
+
127
+ To install this gem onto your local machine, run `bundle exec rake install`. To
128
+ release a new version, update the version number in `version.rb`, and then run
129
+ `bundle exec rake release`, which will create a git tag for the version, push
130
+ git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
131
+
132
+ ## Contributing
133
+
134
+ Bug reports and pull requests are welcome on GitHub at https://github.com/robzr/short_bus.
135
+
136
+ ## License
137
+
138
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "short_bus"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pp'
4
+ require 'short_bus'
5
+
6
+ driver = ShortBus::Driver.new(debug: false)
7
+
8
+ #monitor = ShortBus::Monitor.new driver
9
+
10
+ driver.subscribe(
11
+ name: 'lambie',
12
+ service: lambda { |message|
13
+ sleep 0.2
14
+ raise Exception.new 'random explosion' if rand(4) == 0
15
+ 'lambie::response'
16
+ }
17
+ )
18
+
19
+ driver.subscribe(
20
+ debug: false,
21
+ name: 'got',
22
+ publisher_spec: 'lambie',
23
+ thread_count: 2
24
+ ) do |msg|
25
+ sleep 0.3
26
+ puts "anon-1 #{msg}"
27
+ nil
28
+ end
29
+
30
+ driver.subscribe(
31
+ message_spec: ['h**', '**::hello'],
32
+ name: 'inline_bob'
33
+ ) do |message|
34
+ puts "inline_bob received: #{message} from #{message.publisher}"
35
+ puts " payload: #{message.payload}" if message.payload
36
+ message << 'arbitrary object sent via Message'
37
+ message.payload += 1 if message.payload.is_a?(Numeric)
38
+ message.merge 'new::message'
39
+ end
40
+
41
+ Thread.new {
42
+ 5.times do
43
+ driver << "publish::thread"
44
+ sleep 0.15
45
+ end
46
+ }
47
+
48
+ first_message = driver << ["hi::bob", 1]
49
+ sleep 0.2
50
+ driver << ["hello::jim", "pot"]
51
+ sleep 0.2
52
+ driver << ["hola::xxx", "stew"]
53
+
54
+ puts "Shift-back from first message #{first_message.shift(2)}"
55
+
56
+ sleep 1
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # First example - basic publishing / subscription
4
+ #
5
+ require_relative '../short_bus'
6
+
7
+ # Instantiate Driver, start message routing thread
8
+ #
9
+ driver = ShortBus::Driver.new
10
+
11
+ # Subscribes a block to all messages (no filtering)
12
+ #
13
+ driver.subscribe { |message| puts "1. I like all foods, including #{message}" }
14
+
15
+ # Subscribes a block with a message_spec filtering only some messages
16
+ # Also, replies back to the driver with a new message.
17
+ #
18
+ driver.subscribe(message_spec: 'Chocolate::**') do |message|
19
+ puts "2. Did I hear you say Chocolate? (#{message}). I know what I'm making."
20
+ 'Chocolate::And::Strawberries'
21
+ end
22
+
23
+ # Subscribes a block with a message_spec filtering only some messages
24
+ #
25
+ driver.subscribe(message_spec: '**::Strawberries') do |message|
26
+ puts "3. I only care about Strawberries: #{message}"
27
+ 'Strawberries'
28
+ end
29
+
30
+ # First lets just test it with an unrelated message
31
+ #
32
+ driver.publish 'Cookies::And::Cream'
33
+ sleep 0.1
34
+ puts
35
+
36
+ # Now lets try some interaction going between services
37
+ #
38
+ driver.publish 'Chocolate::Anything'
39
+ sleep 0.1
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # ShortBus example passing return values via Message object
4
+
5
+ require_relative '../short_bus'
6
+
7
+ driver = ShortBus::Driver.new
8
+
9
+ def house_cleaner(message)
10
+ puts "Lets blow this popsicle stand..."
11
+ sleep 0.5
12
+
13
+ # Send value back into the Message object
14
+ message << "I'm working on it!"
15
+
16
+ # pause
17
+ sleep 1
18
+
19
+ # and exit the program
20
+ exit
21
+ end
22
+
23
+ driver.subscribe(
24
+ message_spec: '*::Shutdown',
25
+ service: method(:house_cleaner)
26
+ )
27
+
28
+ # driver.publish returns the message object we just sent
29
+ our_message = driver.publish('Everyone::Shutdown')
30
+
31
+ # Since it is inherited from a Queue, we can pop right off it
32
+ return_value = our_message.shift
33
+
34
+ # And see what the callback wanted to tell us!
35
+ puts "I heard back from the house_cleaner, who says: #{return_value}"
36
+
37
+ # sleep indefinitely, or until we somehow exit...
38
+ sleep
@@ -0,0 +1,9 @@
1
+ module DebugMessage
2
+ def debug_message(message)
3
+ (@debug_message_output_filehandle || STDERR).printf(
4
+ "%s::%s\n",
5
+ caller.first.sub(/^.*\/([^:]*).*/, '\1'),
6
+ message
7
+ ) if @debug
8
+ end
9
+ end
@@ -0,0 +1,92 @@
1
+ require 'pp'
2
+ require 'set'
3
+
4
+ module ShortBus
5
+ class Driver
6
+ include DebugMessage
7
+
8
+ attr_reader :services
9
+ attr_accessor :debug
10
+
11
+ DEFAULT_DRIVER_OPTIONS = {
12
+ debug: false,
13
+ default_message_spec: nil,
14
+ default_publisher_spec: nil,
15
+ default_thread_count: 1,
16
+ max_message_queue_size: 1_000_000,
17
+ }
18
+
19
+ def initialize(*options)
20
+ @options = DEFAULT_DRIVER_OPTIONS
21
+ @options.merge! options[0] if options[0].is_a?(Hash)
22
+ @debug = @options[:debug]
23
+
24
+ @messages = SizedQueue.new(@options[:max_message_queue_size])
25
+ @services = {}
26
+ @threads = { message_router: launch_message_router }
27
+ end
28
+
29
+ def subscribe(*args, &block)
30
+ service_args = {
31
+ debug: @debug,
32
+ driver: self,
33
+ message_spec: @options[:default_message_spec],
34
+ name: nil,
35
+ publisher_spec: @options[:default_publisher_spec],
36
+ service: nil,
37
+ thread_count: @options[:default_thread_count],
38
+ }.merge args[0].is_a?(Hash) ? args[0] : { service: args[0] }
39
+
40
+ service_args[:service] = block.to_proc if block_given?
41
+ debug_message("#subscribe service: #{service_args[:service]}")
42
+ service = Service.new(service_args)
43
+ @services[service.to_s] = service
44
+ end
45
+
46
+ def publish(publisher=nil, arg)
47
+ if message = convert_to_message(arg)
48
+ message.publisher = publisher if publisher
49
+ @messages.push message
50
+ message
51
+ end
52
+ end
53
+
54
+ alias_method :<<, :publish
55
+
56
+ def unsubscribe(service)
57
+ if service.is_a? ShortBus::Service
58
+ unsubscribe service.to_s
59
+ elsif @services.has_key? service
60
+ @services[service].stop
61
+ @services.delete service
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ def convert_to_message(arg)
68
+ if arg.is_a? ShortBus::Message
69
+ arg
70
+ elsif arg.is_a? String
71
+ Message.new(arg)
72
+ elsif arg.is_a?(Array) && arg[0].is_a?(String)
73
+ Message.new(arg)
74
+ elsif arg.is_a?(Hash) && arg.has_key?(:message) && arg[:message]
75
+ publisher = arg.has_key?(:publisher) ? arg[:publisher] : nil
76
+ payload = arg.has_key?(:payload) ? arg[:payload] : nil
77
+ Message.new(message: arg[:message], payload: payload, publisher: publisher)
78
+ end
79
+ end
80
+
81
+ def launch_message_router
82
+ Thread.new do
83
+ loop do
84
+ message = @messages.shift
85
+ debug_message "route_message(#{message})"
86
+ @services.values.each { |service| service.check message }
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+
@@ -0,0 +1,85 @@
1
+ require 'pp'
2
+ require 'timeout'
3
+
4
+ # Queue, with a few mods
5
+ # - message (message.to_s) (string)
6
+ # - optional payload (object)
7
+ # - publisher (string or nil = anonymous)
8
+ #
9
+ module ShortBus
10
+ class Message < Queue
11
+ attr_accessor :publisher
12
+ attr_reader :payload
13
+
14
+ def initialize(*args)
15
+ @message, @payload, @publisher = nil
16
+ @semaphore = Mutex.new
17
+ if populate args
18
+ super()
19
+ else
20
+ raise ArgumentError.new "#Message: Invalid args #{args.pretty_inspect}"
21
+ end
22
+ end
23
+
24
+ def merge(*args)
25
+ arg_hash = process_args args
26
+ if arg_hash[:message]
27
+ Message.new(
28
+ message: arg_hash[:message] || @message,
29
+ payload: arg_hash.has_key?(:payload) ? arg_hash[:payload] : @payload,
30
+ )
31
+ end
32
+ end
33
+
34
+ def payload=(arg)
35
+ @semaphore.synchronize { @payload = arg }
36
+ end
37
+
38
+ def pop(time_out=nil)
39
+ if time_out.is_a? Numeric
40
+ begin
41
+ Timeout.timeout(time_out) { super() }
42
+ rescue Timeout::Error
43
+ end
44
+ else
45
+ super(time_out)
46
+ end
47
+ end
48
+
49
+ alias_method :shift, :pop
50
+ alias_method :deq, :pop
51
+
52
+ def to_s
53
+ @message
54
+ end
55
+
56
+ private
57
+
58
+ def populate(args)
59
+ arg_hash = process_args args
60
+ if arg_hash.has_key?(:message)
61
+ @payload = arg_hash[:payload] if arg_hash.has_key?(:payload)
62
+ @publisher = arg_hash[:publisher] if arg_hash.has_key?(:publisher)
63
+ @message = arg_hash[:message]
64
+ end
65
+ end
66
+
67
+ def process_args(args)
68
+ if args[0].is_a? Array
69
+ process_args args[0]
70
+ else
71
+ {}.tap do |me|
72
+ if args[0].is_a? String
73
+ me[:payload] = args[1] if args.length == 2
74
+ me[:payload] = args.slice(1..-1) if args.length > 2
75
+ me[:message] = args[0]
76
+ elsif args[0].is_a?(Hash) && args[0].has_key?(:message)
77
+ me[:payload] = args[0][:payload] if args[0].has_key?(:payload)
78
+ me[:publisher] = args[0][:publisher] if args[0].has_key?(:publisher)
79
+ me[:message] = args[0][:message]
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,60 @@
1
+ require 'pp'
2
+
3
+ module ShortBus
4
+ class Monitor
5
+
6
+ DEFAULT_MONITOR_OPTIONS = {
7
+ message_spec: nil,
8
+ name: 'ShortBus::Monitor',
9
+ suppress_payload: false,
10
+ suppress_publisher: false,
11
+ publisher_spec: nil,
12
+ thread_count: 1
13
+ }
14
+
15
+ def initialize(*args)
16
+ @options = DEFAULT_MONITOR_OPTIONS.merge(
17
+ { service: method(:monitor) }
18
+ )
19
+ @suppress_payload, @suppress_publisher = nil
20
+
21
+ if args[0].is_a?(Hash) && args[0].has_key?(:driver)
22
+ @options.merge! args[0]
23
+ @driver = @options[:driver]
24
+ @options.delete(:driver)
25
+ elsif args.is_a?(Array) && args.length == 1
26
+ @driver = args[0]
27
+ else
28
+ raise ArgumentError, 'No driver passed.'
29
+ end
30
+
31
+ @suppress_payload = @options.delete(:suppress_payload)
32
+ @suppress_publisher = @options.delete(:suppress_publisher)
33
+
34
+ start
35
+ end
36
+
37
+ def monitor(message)
38
+ puts "[#{@options[:name]}] message = #{message}"
39
+ printf(
40
+ " %s payload = %s\n",
41
+ @options[:name] ? ' ' * @options[:name].length : '',
42
+ message.payload.inspect
43
+ ) if message.payload && !@suppress_payload
44
+ printf(
45
+ " %spublisher = %s\n",
46
+ @options[:name] ? ' ' * @options[:name].length : '',
47
+ message.publisher ? message.publisher : '*ANONYMOUS*'
48
+ ) if !@suppress_publisher
49
+ nil
50
+ end
51
+
52
+ def start
53
+ @service = @driver.subscribe(@options)
54
+ end
55
+
56
+ def stop
57
+ @driver.unsubscribe @service
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,109 @@
1
+ #require 'observer'
2
+ require 'pp'
3
+ require 'set'
4
+ require 'openssl'
5
+
6
+ module ShortBus
7
+ class Service
8
+ include DebugMessage
9
+
10
+ attr_reader :name, :threads
11
+
12
+ def initialize(
13
+ debug: false,
14
+ driver: nil,
15
+ max_run_queue_size: 1_000_000,
16
+ message_spec: nil,
17
+ name: nil,
18
+ recursive: false,
19
+ publisher_spec: nil,
20
+ service: nil,
21
+ suppress_exception: false,
22
+ thread_count: 1
23
+ )
24
+ @debug = debug
25
+ @driver = driver
26
+ @message_spec = message_spec ? Spec.new(message_spec) : nil
27
+ @recursive = recursive
28
+ @publisher_spec = publisher_spec ? Spec.new(publisher_spec) : nil
29
+ @service = service
30
+ @thread_count = thread_count
31
+
32
+ @name = name || @service.to_s || OpenSSL::HMAC.new(rand.to_s, 'sha1').to_s
33
+ @run_queue = SizedQueue.new(max_run_queue_size)
34
+ @threads = []
35
+ start
36
+ end
37
+
38
+ def check(message, dry_run=false)
39
+ debug_message "[#{@name}]#check(#{message})#{' dry_run' if dry_run}#"
40
+ if(
41
+ (!@message_spec || @message_spec.match(message.to_s)) &&
42
+ (!@publisher_spec || @publisher_spec.match(message.publisher)) &&
43
+ (message.publisher != @name || @recursive)
44
+ )
45
+ @run_queue << message unless dry_run
46
+ end
47
+ end
48
+
49
+ # TODO: consider some mechanism to pass Exceptions up to the main thread,
50
+ # perhaps with a whitelist, optional logging, something clean.
51
+ def service_thread
52
+ Thread.new do
53
+ begin
54
+ run_service @run_queue.shift until Thread.current.key?(:stop)
55
+ rescue Exception => exc
56
+ puts "Service [#{@name}] => #{exc.inspect}" unless @suppress_exception
57
+ abort if exc.is_a? SystemExit
58
+ retry unless Thread.current.key?(:stop)
59
+ end
60
+ end
61
+ end
62
+
63
+ def start
64
+ @threads << service_thread while @threads.length < @thread_count
65
+ end
66
+
67
+ def stop(when_to_kill=nil)
68
+ @threads.each do |thread|
69
+ if when_to_kill.is_a? Numeric
70
+ begin
71
+ Timeout.timeout(when_to_kill) { stop }
72
+ rescue Timeout::Error
73
+ stop :now
74
+ end
75
+ elsif when_to_kill == :now
76
+ thread.kill
77
+ else
78
+ thread[:stop] = true
79
+ end
80
+ end
81
+ @threads.delete_if { |thread| @threads[index].join }
82
+ end
83
+
84
+ def stop!
85
+ stop :now
86
+ end
87
+
88
+ def to_s
89
+ @name
90
+ end
91
+
92
+ private
93
+
94
+ def run_service(message)
95
+ debug_message "[#{@name}]#run_service(#{message}) -> #{@service.class.name} ##{@service.arity}"
96
+ if @service.is_a?(Proc) || @service.is_a?(Method)
97
+ if @service.arity == 0
98
+ @driver.publish(@name, @service.call)
99
+ elsif [1, -1, -2].include? @service.arity
100
+ @driver.publish(@name, @service.call(message))
101
+ else
102
+ raise ArgumentError, "Service invalid arg count: #{@service.class.name}"
103
+ end
104
+ else
105
+ raise ArgumentError, "Unknown service type: #{@service.class.name}"
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,58 @@
1
+ require 'pp'
2
+ require 'set'
3
+
4
+ module ShortBus
5
+ class Spec
6
+ attr_reader :specs
7
+
8
+ def initialize(spec = nil)
9
+ @specs = process(spec)
10
+ end
11
+
12
+ def <<(spec)
13
+ @specs += process(spec)
14
+ end
15
+
16
+ def delete(spec)
17
+ @specs.delete spec
18
+ end
19
+
20
+ def match(item)
21
+ @specs.reduce(false) { |acc, spec| acc || match_single(spec, item) }
22
+ end
23
+
24
+ private
25
+
26
+ def match_single(spec, item)
27
+ if spec.is_a? String
28
+ spec == item
29
+ elsif spec.is_a? Regexp
30
+ spec.match item
31
+ end
32
+ end
33
+
34
+ def process(spec, to_set = true)
35
+ if spec.is_a? Array
36
+ process spec.flatten.to_set
37
+ elsif spec.is_a? Regexp
38
+ to_set ? [spec].to_set : spec
39
+ elsif spec.is_a? Set
40
+ spec.flatten
41
+ .map { |spec| process(spec, false) }
42
+ .to_set
43
+ elsif spec.is_a? String
44
+ to_set ? [string_to_regexp(spec)].to_set : string_to_regexp(spec)
45
+ elsif spec.is_a? NilClass
46
+ [/.*/].to_set
47
+ end
48
+ end
49
+
50
+ def string_to_regexp(spec)
51
+ if spec.include? '*'
52
+ /^#{spec.gsub(/\*+/, '*' => '[^:]*', '**' => '.*')}/
53
+ else
54
+ spec
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module ShortBus
2
+ VERSION = "0.1.0"
3
+ end
data/lib/short_bus.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'short_bus/debug_message'
2
+ require 'short_bus/driver'
3
+ require 'short_bus/spec'
4
+ require 'short_bus/message'
5
+ require 'short_bus/monitor'
6
+ require 'short_bus/service'
7
+ require "short_bus/version"
8
+
9
+ module ShortBus
10
+ # Your code goes here...
11
+ end
data/short_bus.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'short_bus/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "short_bus"
8
+ spec.version = ShortBus::VERSION
9
+ spec.authors = ["Rob Zwissler"]
10
+ spec.email = ["rob@zwissler.org"]
11
+
12
+ spec.summary = %q{Lightweight multi-threaded pub-sub message dispatcher for self-contained service-oriented Ruby apps.}
13
+
14
+ spec.description = %q{Small, lightweight, easy to implement multi-threaded publish/subscribe style message dispatcher for quickly creating self-contained service-oriented Ruby apps.}
15
+ spec.homepage = "https://github.com/robzr/short_bus"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.12"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: short_bus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rob Zwissler
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Small, lightweight, easy to implement multi-threaded publish/subscribe
56
+ style message dispatcher for quickly creating self-contained service-oriented Ruby
57
+ apps.
58
+ email:
59
+ - rob@zwissler.org
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - .gitignore
65
+ - .rspec
66
+ - .travis.yml
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - examples/misc_tests
74
+ - examples/tutorial_1_basic_intro.rb
75
+ - examples/tutorial_2_message_return_values.rb
76
+ - lib/short_bus.rb
77
+ - lib/short_bus/debug_message.rb
78
+ - lib/short_bus/driver.rb
79
+ - lib/short_bus/message.rb
80
+ - lib/short_bus/monitor.rb
81
+ - lib/short_bus/service.rb
82
+ - lib/short_bus/spec.rb
83
+ - lib/short_bus/version.rb
84
+ - short_bus.gemspec
85
+ homepage: https://github.com/robzr/short_bus
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.0.14.1
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Lightweight multi-threaded pub-sub message dispatcher for self-contained
109
+ service-oriented Ruby apps.
110
+ test_files: []