rocketman 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3a84e340fb77937facabe1dc31b0c95675e5f0d67587fba1c8e6f4ca8b60be0
4
- data.tar.gz: 1db1888d8d6d8d0ada0326b1d1b3d18614b2b5ec75e41b4c17f6d400480e2e5c
3
+ metadata.gz: 7add76c53ecb1c5996cc2c21e0141bcadeb4169d40bb9785c577286839eff7a7
4
+ data.tar.gz: e2b0ff4413172768ef6a331b76fa75113fe80cbb1aa63a9fc8e62bba915b4b8e
5
5
  SHA512:
6
- metadata.gz: 1f57be811c0093515e7362bb560007d3fe56f44081cad0b61ff1db973160c690be4230332666f6ab4c3e49269c52cca8673c6d0964325dc1e76e07422621685f
7
- data.tar.gz: 447f48d096cc2844dc1a2510231e93f34a0dffadcd9ba1aa09049ae5f4487ad64a9b4615b7edea09b626c0fc92361f661212d35e84ec945f4affeab1fa50f70f
6
+ metadata.gz: da1cd6a839cb80ae22eebb8ca1150bfacd555999a4ada7c348838647db7118d5e978641023459dce4c8ef2158d70043d84185d2e876153e2cea444b69616f266
7
+ data.tar.gz: 37b70df7e25868cfe7d3de36b38d49c116e75a699b7a6ee680491ab4c428f212f24dab728f570e1266228483daed1074d15876a37432a8a22c2ef0ef80939ee6
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rocketman (0.1.0)
4
+ rocketman (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -27,7 +27,7 @@ Or install it yourself as:
27
27
 
28
28
  ## Usage
29
29
 
30
- Rocketman exposes two module, `Rocketman::Producer` and `Rocketman::Consumer`. They do exactly as what their name implies. All you need to do is `include` them into your code.
30
+ Rocketman exposes two module, `Rocketman::Producer` and `Rocketman::Consumer`. They do exactly as what their name implies. All you need to do is `include Rocketman::Producer` and `extend Rocketman::Consumer` into your code.
31
31
 
32
32
  #### Producer
33
33
  Producer exposes one **instance** method to you: `:emit`. `:emit` takes in the event name and an optional payload and publishes it to the consumers. There's nothing more you need to do. The producer do not have to know who its consumers are.
@@ -42,12 +42,23 @@ class Producer
42
42
  end
43
43
  ```
44
44
 
45
+ Note that Producer emit events with threads that run in a thread pool. The default number of worker is 5, and the workers default to checking the job with a 3 seconds interval. You can tweak it to your liking like so:
46
+
47
+ ```ruby
48
+ # config/initializers/rocketman.rb
49
+
50
+ Rocketman.configure do |config|
51
+ config.worker_count = 10 # defaults to 5
52
+ config.latency = 1 # defaults to 3, unit is :seconds
53
+ end
54
+ ```
55
+
45
56
  #### Consumer
46
57
  Consumer exposes a **class** method, `:on_event`. `:on_event` takes in the event name, and also an additional block, which gets executed whenever a message is received. If an additional `payload` is emitted along with the event, you can get access to it in the form of block argument.
47
58
 
48
59
  ```ruby
49
60
  class Consumer
50
- include Rocketman::Consumer
61
+ extend Rocketman::Consumer
51
62
 
52
63
  on_event :hello do |payload|
53
64
  puts "I've received #{payload} here!"
@@ -1,85 +1,6 @@
1
- require 'set'
2
- require 'singleton'
3
- require 'pry'
4
-
5
- module Rocketman
6
- class Registry
7
- include Singleton
8
-
9
- def initialize
10
- @registry = {}
11
- end
12
-
13
- def register_event(event)
14
- if @registry[event]
15
- return @registry[event]
16
- else
17
- @registry[event] = {}
18
- end
19
- end
20
-
21
- def register_consumer(event, consumer, action)
22
- @registry[event][consumer] = action
23
- end
24
-
25
- def get_consumers_for(event)
26
- @registry[event]
27
- end
28
-
29
- def event_exists?(event)
30
- !@registry[event].nil?
31
- end
32
- end
33
-
34
- class Event
35
- def initialize(event, payload)
36
- @event = event
37
- @payload = payload
38
- @test = payload.fetch(:test, false)
39
- Rocketman::Registry.instance.register_event(event)
40
- end
41
-
42
- def notify_consumers
43
- consumers = Rocketman::Registry.instance.get_consumers_for(@event)
44
-
45
- threads = consumers.reduce([]) do |memo, (consumer, action)|
46
- memo << Thread.new { consumer.instance_exec(@payload, &action) }
47
- end
48
-
49
- threads.each { |t| t.join } if @test == true
50
- end
51
- end
52
-
53
- module Producer
54
- def self.included(base)
55
- base.include(InstanceMethods)
56
- end
57
-
58
- module InstanceMethods
59
- def emit(event, **payload)
60
- event = Rocketman::Event.new(event, payload)
61
- event.notify_consumers
62
- end
63
- end
64
- end
65
-
66
- module Consumer
67
- def self.included(base)
68
- base.extend(InstanceMethods)
69
- end
70
-
71
- module InstanceMethods
72
- def on_event(event, &action)
73
- consumer = self
74
- Rocketman::Registry.instance.register_event(event)
75
- register_consumer(event, consumer, action)
76
- end
77
-
78
- private
79
-
80
- def register_consumer(event, consumer, action)
81
- Rocketman::Registry.instance.register_consumer(event, consumer, action)
82
- end
83
- end
84
- end
85
- end
1
+ require 'rocketman/config.rb'
2
+ require 'rocketman/pool.rb'
3
+ require 'rocketman/registry.rb'
4
+ require 'rocketman/event.rb'
5
+ require 'rocketman/producer.rb'
6
+ require 'rocketman/consumer.rb'
@@ -0,0 +1,18 @@
1
+ module Rocketman
2
+ def self.configuration
3
+ @_configuration ||= Configuration.new
4
+ end
5
+
6
+ def self.configure
7
+ yield(configuration)
8
+ end
9
+
10
+ class Configuration
11
+ attr_accessor :worker_count, :latency
12
+
13
+ def initialize
14
+ @worker_count = 5
15
+ @latency = 3
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module Rocketman
2
+ module Consumer
3
+ def on_event(event, &action)
4
+ consumer = self
5
+ Rocketman::Registry.instance.register_event(event)
6
+ register_consumer(event, consumer, action)
7
+ end
8
+
9
+ private
10
+
11
+ def register_consumer(event, consumer, action)
12
+ Rocketman::Registry.instance.register_consumer(event, consumer, action)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module Rocketman
2
+ class Event
3
+ def initialize(event, payload)
4
+ @event = event
5
+ @payload = payload
6
+ @test = payload.fetch(:test, false)
7
+ Rocketman::Registry.instance.register_event(event)
8
+ end
9
+
10
+ def notify_consumers
11
+ consumers = Rocketman::Registry.instance.get_consumers_for(@event)
12
+
13
+ consumers.each do |consumer, action|
14
+ consumer.instance_exec(@payload, &action)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,38 @@
1
+ require 'singleton'
2
+
3
+ module Rocketman
4
+ class Pool
5
+ include Singleton
6
+
7
+ def initialize
8
+ worker_count = Rocketman.configuration.worker_count
9
+ latency = Rocketman.configuration.latency
10
+
11
+ @latency = latency
12
+ @jobs = Queue.new
13
+ @workers = []
14
+
15
+ worker_count.times do
16
+ @workers << spawn_worker
17
+ end
18
+
19
+ # spawn_supervisor # TODO: Write a supervisor to monitor workers health, and restart if necessary
20
+ end
21
+
22
+ def schedule(&job)
23
+ @jobs << job
24
+ end
25
+
26
+ private
27
+
28
+ def spawn_worker
29
+ Thread.new do
30
+ loop do
31
+ job = @jobs.pop
32
+ job.call
33
+ sleep @latency
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,8 @@
1
+ module Rocketman
2
+ module Producer
3
+ def emit(event, **payload)
4
+ event = Rocketman::Event.new(event, payload)
5
+ Rocketman::Pool.instance.schedule { event.notify_consumers }
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,31 @@
1
+ require 'singleton'
2
+
3
+ module Rocketman
4
+ class Registry
5
+ include Singleton
6
+
7
+ def initialize
8
+ @registry = {}
9
+ end
10
+
11
+ def register_event(event)
12
+ if @registry[event]
13
+ return @registry[event]
14
+ else
15
+ @registry[event] = {}
16
+ end
17
+ end
18
+
19
+ def register_consumer(event, consumer, action)
20
+ @registry[event][consumer] = action
21
+ end
22
+
23
+ def get_consumers_for(event)
24
+ @registry[event]
25
+ end
26
+
27
+ def event_exists?(event)
28
+ !@registry[event].nil?
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module Rocketman
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rocketman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edison Yap
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-10 00:00:00.000000000 Z
11
+ date: 2019-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -85,6 +85,12 @@ files:
85
85
  - bin/console
86
86
  - bin/setup
87
87
  - lib/rocketman.rb
88
+ - lib/rocketman/config.rb
89
+ - lib/rocketman/consumer.rb
90
+ - lib/rocketman/event.rb
91
+ - lib/rocketman/pool.rb
92
+ - lib/rocketman/producer.rb
93
+ - lib/rocketman/registry.rb
88
94
  - lib/rocketman/version.rb
89
95
  - rocketman.gemspec
90
96
  - rocketman.jpg