mumukit-nuntius 6.5.0 → 6.6.0

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: 5b51e0917adc22fad9e3173aaf9fd3dd4e5bfd9b5ad887c85ffc549c6625eb0a
4
- data.tar.gz: 5b342931d64d9c9892fd58c699cee86096d01038381ea8ab52b16f742ff74d23
3
+ metadata.gz: 4fc18f07275f31c47b7c319636d863c478db44480df61bd8ccb756b28b8a4ed6
4
+ data.tar.gz: 062df9f35a58b132f6ee384469024716f99cdab2ee705fb25288bd9b5019d31b
5
5
  SHA512:
6
- metadata.gz: f7d416c63759d6759d9c2448e5d164c2329e15fbfd344e30a8854b142f0fe1c70e4ca063a5da40c99e4c4c42abfc799167f4e900d401ff510bfb8161d00108ab
7
- data.tar.gz: a4d7c7507130cf7a76ae0f15f75b3e0b3de018e38b2dab59913e9bddbef21fa96e93f0d11226bf717edcd147ce96f1afffa74f41b531d334cfab5a9b5d230578
6
+ metadata.gz: e832561715b9600fc68fb20f1d0f81f246a7ef9a5255184d67827b91b028b306a33303cb9c74ab41b19c4695581c5e87d03caa2c6c69e421bc31b12a33918dcf
7
+ data.tar.gz: 58d7e01c8438e97a2d647f6f0263192fb7adc3ca8bfc11a4e08ddba02085838fe16a39fba46b1b4507616f1ae31dd407ba6db3cab06641ffac04b1296d767310
@@ -1,74 +1,34 @@
1
1
  module Mumukit::Nuntius::EventConsumer
2
- class Builder
3
- def initialize
4
- @handlers = {}
5
- end
6
-
7
- def event(key, &block)
8
- @handlers[key] = with_database_reconnection &block
9
- end
10
-
11
- def build
12
- @handlers.with_indifferent_access
13
- end
14
-
15
- private
16
2
 
17
- def with_database_reconnection(&block)
18
- return block unless defined? ActiveRecord
19
- proc do |*args|
20
- ActiveRecord::Base.connection_pool.with_connection do
21
- block.call(*args)
22
- end
23
- end
24
- end
3
+ extend Mumukit::Nuntius::TaskConsumer
25
4
 
5
+ class Builder < Mumukit::Nuntius::TaskConsumer::Builder
6
+ alias_method :event, :task
26
7
  end
27
8
 
28
9
  class << self
29
- @@handlers = {}
30
-
31
- def handle(&block)
32
- register_handlers! Builder.new.tap { |it| it.instance_eval(&block) }.build
33
- end
34
10
 
35
- def handled_events
36
- @@handlers.keys
37
- end
11
+ alias_method :handled_events, :handled_tasks
12
+ alias_method :handle_event!, :handle_tasks!
38
13
 
39
- def register_handlers!(handlers)
40
- @@handlers = handlers
14
+ def builder
15
+ Mumukit::Nuntius::EventConsumer::Builder
41
16
  end
42
17
 
43
- def start!
44
- queue_name = "#{Mumukit::Nuntius.config.app_name}-events"
45
- Mumukit::Nuntius::Consumer.start queue_name, 'events' do |_delivery_info, properties, body|
46
- handle_event!(properties, body)
47
- end
18
+ def tasks_type
19
+ task_type.pluralize
48
20
  end
49
21
 
50
- def handles?(event)
51
- @@handlers.include? event
22
+ def task_type
23
+ 'event'
52
24
  end
53
25
 
54
26
  def handle_event!(properties, body)
55
- return if body[:data][:sender] == Mumukit::Nuntius.config.app_name
56
- event = properties[:type]
57
- if handles? event
58
- @@handlers[event].call body[:data].except(:sender)
59
- else
60
- log_unknown_event event
61
- end
62
- rescue => e
63
- log_exception(event, e, body[:data])
64
- end
65
-
66
- def log_unknown_event(event)
67
- Mumukit::Nuntius::Logger.info "Unhandled event: #{event} does not exists."
27
+ handle_tasks! properties, body
68
28
  end
69
29
 
70
- def log_exception(event, e, body)
71
- Mumukit::Nuntius::Logger.error "Failed to proccess #{event}, error was: #{e}, body was: #{body}"
30
+ def skip_process?(body)
31
+ body[:data][:sender] == Mumukit::Nuntius.config.app_name
72
32
  end
73
33
  end
74
34
  end
@@ -0,0 +1,30 @@
1
+ module Mumukit::Nuntius::JobConsumer
2
+
3
+ extend Mumukit::Nuntius::TaskConsumer
4
+
5
+ class Builder < Mumukit::Nuntius::TaskConsumer::Builder
6
+ alias_method :job, :task
7
+ end
8
+
9
+ class << self
10
+
11
+ alias_method :handled_jobs, :handled_tasks
12
+ alias_method :handle_job!, :handle_tasks!
13
+
14
+ def builder
15
+ Mumukit::Nuntius::JobConsumer::Builder
16
+ end
17
+
18
+ def tasks_type
19
+ task_type.pluralize
20
+ end
21
+
22
+ def task_type
23
+ 'job'
24
+ end
25
+
26
+ def skip_process?(body)
27
+ body[:data][:sender].try { |sender| sender != Mumukit::Nuntius.config.app_name }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ module Mumukit::Nuntius::JobPublisher
2
+
3
+ class << self
4
+
5
+ def publish(job, payload)
6
+ payload.merge!(sender: Mumukit::Nuntius.config.app_name)
7
+ Mumukit::Nuntius::Publisher.publish "jobs", {data: payload}, {type: job}
8
+ end
9
+
10
+ end
11
+ end
@@ -3,6 +3,9 @@ module Mumukit::Nuntius::NotificationMode
3
3
  def notify!(*)
4
4
  end
5
5
 
6
+ def notify_job!(*)
7
+ end
8
+
6
9
  def notify_event!(*)
7
10
  end
8
11
 
@@ -4,6 +4,10 @@ module Mumukit::Nuntius::NotificationMode
4
4
  Mumukit::Nuntius::Publisher.publish queue_name, event
5
5
  end
6
6
 
7
+ def notify_job!(type, data)
8
+ Mumukit::Nuntius::JobPublisher.publish type, data
9
+ end
10
+
7
11
  def notify_event!(type, data, **options)
8
12
  Mumukit::Nuntius::EventPublisher.publish type, data, **options
9
13
  end
@@ -0,0 +1,75 @@
1
+ module Mumukit::Nuntius::TaskConsumer
2
+
3
+ class Builder
4
+ def initialize
5
+ @handlers = {}
6
+ end
7
+
8
+ def task(key, &block)
9
+ @handlers[key] = with_database_reconnection &block
10
+ end
11
+
12
+ def build
13
+ @handlers.with_indifferent_access
14
+ end
15
+
16
+ private
17
+
18
+ def with_database_reconnection(&block)
19
+ return block unless defined? ActiveRecord
20
+ proc do |*args|
21
+ ActiveRecord::Base.connection_pool.with_connection do
22
+ block.call(*args)
23
+ end
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ def handlers
30
+ @@handlers ||= {}
31
+ end
32
+
33
+ def handle(&block)
34
+ register_handlers! builder.new.tap { |it| it.instance_eval(&block) }.build
35
+ end
36
+
37
+ def handled_tasks
38
+ handlers.keys
39
+ end
40
+
41
+ def register_handlers!(handlers)
42
+ @@handlers = handlers
43
+ end
44
+
45
+ def start!
46
+ queue_name = "#{Mumukit::Nuntius.config.app_name}-#{tasks_type}"
47
+ Mumukit::Nuntius::Consumer.start queue_name, tasks_type do |_delivery_info, properties, body|
48
+ handle_tasks!(properties, body)
49
+ end
50
+ end
51
+
52
+ def handles?(task)
53
+ handlers.include? task
54
+ end
55
+
56
+ def handle_tasks!(properties, body)
57
+ return if skip_process? body
58
+ task = properties[:type]
59
+ if handles? task
60
+ handlers[task].call body[:data].except(:sender)
61
+ else
62
+ log_unknown_task task
63
+ end
64
+ rescue => e
65
+ log_exception(task, e, body[:data])
66
+ end
67
+
68
+ def log_unknown_task(task)
69
+ Mumukit::Nuntius::Logger.info "Unhandled #{task_type}: #{task} does not exists."
70
+ end
71
+
72
+ def log_exception(task, e, body)
73
+ Mumukit::Nuntius::Logger.error "Failed to proccess #{task}, error was: #{e}, body was: #{body}"
74
+ end
75
+ end
@@ -1,5 +1,5 @@
1
1
  module Mumukit
2
2
  module Nuntius
3
- VERSION = '6.5.0'
3
+ VERSION = '6.6.0'
4
4
  end
5
5
  end
@@ -47,6 +47,28 @@ module Mumukit
47
47
  notification_mode.notify_event! type, event, **options
48
48
  end
49
49
 
50
+ # Notifies a job of the given type.
51
+ #
52
+ # Jobs are very similar to normal messages, with the following differences:
53
+ #
54
+ # * they are published to a single queue, named `jobs`
55
+ # * job data is augmented with the sender app name,
56
+ # so that consumers can consume only jobs sent from themselves.
57
+ #
58
+ # This makes jobs lighter and easier to send. You should send application
59
+ # jobs to here if:
60
+ #
61
+ # * you expect to send a lot of those jobs
62
+ # * you expect those jobs processing to be slow
63
+ #
64
+ # Events are consumed using the Mumukit::Nuntius::JobConsumer module
65
+ #
66
+ # @param [String|Symbol] type the type of job.
67
+ # @param [Hash] job a json-like hash with the job data
68
+ def self.notify_job!(type, job)
69
+ notification_mode.notify_job! type, job
70
+ end
71
+
50
72
  def self.establish_connection
51
73
  notification_mode.establish_connection
52
74
  end
@@ -68,6 +90,9 @@ require 'mumukit/nuntius/connection'
68
90
  require 'mumukit/nuntius/channel'
69
91
  require 'mumukit/nuntius/publisher'
70
92
  require 'mumukit/nuntius/consumer'
93
+ require 'mumukit/nuntius/task_consumer'
71
94
  require 'mumukit/nuntius/event_consumer'
72
95
  require 'mumukit/nuntius/event_publisher'
96
+ require 'mumukit/nuntius/job_consumer'
97
+ require 'mumukit/nuntius/job_publisher'
73
98
  require 'mumukit/nuntius/notification_mode'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mumukit-nuntius
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.5.0
4
+ version: 6.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Agustin Pina
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-11-17 00:00:00.000000000 Z
12
+ date: 2021-11-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -96,10 +96,13 @@ files:
96
96
  - lib/mumukit/nuntius/consumer.rb
97
97
  - lib/mumukit/nuntius/event_consumer.rb
98
98
  - lib/mumukit/nuntius/event_publisher.rb
99
+ - lib/mumukit/nuntius/job_consumer.rb
100
+ - lib/mumukit/nuntius/job_publisher.rb
99
101
  - lib/mumukit/nuntius/notification_mode.rb
100
102
  - lib/mumukit/nuntius/notification_mode/deaf.rb
101
103
  - lib/mumukit/nuntius/notification_mode/nuntius.rb
102
104
  - lib/mumukit/nuntius/publisher.rb
105
+ - lib/mumukit/nuntius/task_consumer.rb
103
106
  - lib/mumukit/nuntius/version.rb
104
107
  homepage: http://github.com/mumuki/mumukit-nuntius
105
108
  licenses:
@@ -121,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
124
  - !ruby/object:Gem::Version
122
125
  version: '0'
123
126
  requirements: []
124
- rubygems_version: 3.0.8
127
+ rubygems_version: 3.0.3
125
128
  signing_key:
126
129
  specification_version: 4
127
130
  summary: Library for working with rabbit queues