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 +4 -4
- data/lib/mumukit/nuntius/event_consumer.rb +14 -54
- data/lib/mumukit/nuntius/job_consumer.rb +30 -0
- data/lib/mumukit/nuntius/job_publisher.rb +11 -0
- data/lib/mumukit/nuntius/notification_mode/deaf.rb +3 -0
- data/lib/mumukit/nuntius/notification_mode/nuntius.rb +4 -0
- data/lib/mumukit/nuntius/task_consumer.rb +75 -0
- data/lib/mumukit/nuntius/version.rb +1 -1
- data/lib/mumukit/nuntius.rb +25 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fc18f07275f31c47b7c319636d863c478db44480df61bd8ccb756b28b8a4ed6
|
4
|
+
data.tar.gz: 062df9f35a58b132f6ee384469024716f99cdab2ee705fb25288bd9b5019d31b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
36
|
-
|
37
|
-
end
|
11
|
+
alias_method :handled_events, :handled_tasks
|
12
|
+
alias_method :handle_event!, :handle_tasks!
|
38
13
|
|
39
|
-
def
|
40
|
-
|
14
|
+
def builder
|
15
|
+
Mumukit::Nuntius::EventConsumer::Builder
|
41
16
|
end
|
42
17
|
|
43
|
-
def
|
44
|
-
|
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
|
51
|
-
|
22
|
+
def task_type
|
23
|
+
'event'
|
52
24
|
end
|
53
25
|
|
54
26
|
def handle_event!(properties, body)
|
55
|
-
|
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
|
71
|
-
Mumukit::Nuntius
|
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
|
@@ -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
|
data/lib/mumukit/nuntius.rb
CHANGED
@@ -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.
|
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:
|
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.
|
127
|
+
rubygems_version: 3.0.3
|
125
128
|
signing_key:
|
126
129
|
specification_version: 4
|
127
130
|
summary: Library for working with rabbit queues
|