ruby_rabbitmq_janus 2.1.1 → 2.2.0.pre.42

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.md +3 -4
  3. data/config/default.yml +7 -6
  4. data/lib/generators/ruby_rabbitmq_janus/templates/migration.rb +1 -0
  5. data/lib/rrj/errors/error.rb +16 -4
  6. data/lib/rrj/errors/janus/janus.rb +0 -1
  7. data/lib/rrj/errors/janus/processus/keepalive.rb +5 -17
  8. data/lib/rrj/errors/janus/processus/keepalive/initializer.rb +61 -0
  9. data/lib/rrj/errors/janus/processus/keepalive/thread.rb +16 -0
  10. data/lib/rrj/errors/janus/processus/keepalive/timer.rb +16 -0
  11. data/lib/rrj/errors/rabbit/connect.rb +12 -12
  12. data/lib/rrj/errors/tools/gem/cluster.rb +7 -7
  13. data/lib/rrj/errors/tools/gem/config.rb +0 -14
  14. data/lib/rrj/errors/tools/gem/option.rb +7 -0
  15. data/lib/rrj/info.rb +1 -1
  16. data/lib/rrj/init.rb +0 -11
  17. data/lib/rrj/janus/processus/concurrency.rb +8 -5
  18. data/lib/rrj/janus/processus/event.rb +8 -5
  19. data/lib/rrj/janus/processus/keepalive/keepalive_initializer.rb +74 -0
  20. data/lib/rrj/janus/processus/keepalive/keepalive_message.rb +52 -0
  21. data/lib/rrj/janus/processus/keepalive/keepalive_thread.rb +71 -0
  22. data/lib/rrj/janus/processus/keepalive/keepalive_timer.rb +64 -0
  23. data/lib/rrj/janus/responses/response.rb +8 -1
  24. data/lib/rrj/janus/transactions/transaction.rb +1 -1
  25. data/lib/rrj/models/active_record.rb +6 -2
  26. data/lib/rrj/models/concerns/janus_instance_callbacks.rb +69 -0
  27. data/lib/rrj/models/concerns/{janus_instance_concern.rb → janus_instance_methods.rb} +7 -10
  28. data/lib/rrj/models/concerns/janus_instance_validations.rb +20 -0
  29. data/lib/rrj/models/mongoid.rb +7 -2
  30. data/lib/rrj/rabbit/connect.rb +14 -12
  31. data/lib/rrj/rabbit/publish/keepalive.rb +33 -0
  32. data/lib/rrj/rabbit/publish/publisher.rb +1 -0
  33. data/lib/rrj/tools/gem/cluster.rb +15 -25
  34. data/lib/rrj/tools/gem/config.rb +1 -8
  35. data/lib/rrj/tools/gem/option.rb +16 -9
  36. data/spec/rrj/tools/gem/rrj_cluster_spec.rb +2 -6
  37. data/spec/rrj/tools/gem/rrj_config_spec.rb +1 -6
  38. data/spec/support/schemas/config/config.json +13 -5
  39. metadata +30 -7
  40. data/lib/rrj/janus/processus/keepalive.rb +0 -79
@@ -7,13 +7,18 @@ module RubyRabbitmqJanus
7
7
  # Store instance information for MongoID database
8
8
  class JanusInstance
9
9
  include Mongoid::Document
10
- include RubyRabbitmqJanus::Models::JanusInstanceConcern
10
+ include RubyRabbitmqJanus::Models::JanusInstanceCallbacks
11
+ include RubyRabbitmqJanus::Models::JanusInstanceMethods
12
+ include RubyRabbitmqJanus::Models::JanusInstanceValidations
11
13
 
12
14
  field :instance, type: Integer
13
15
  field :session, type: Integer
14
16
  field :enable, type: Boolean
17
+ field :thread, type: Integer
15
18
 
16
- set_callback(:destroy, :before) { destroy_before_action }
19
+ set_callback(:create, :after) { callback_create_after }
20
+ set_callback(:update, :after) { callback_update_after }
21
+ set_callback(:destroy, :after) { callback_destroy_after }
17
22
  end
18
23
  end
19
24
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # :reek:TooManyStatements
4
+
3
5
  module RubyRabbitmqJanus
4
6
  module Rabbit
5
7
  # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
@@ -9,8 +11,8 @@ module RubyRabbitmqJanus
9
11
  # Initialize connection to server RabbitMQ
10
12
  def initialize
11
13
  @rabbit = Bunny.new(read_options_server.merge!(option_log_rabbit))
12
- rescue
13
- raise Errors::Rabbit::Connect::Initialize
14
+ rescue => error
15
+ raise Errors::Rabbit::Connect::Initialize, error
14
16
  end
15
17
 
16
18
  # Create an transaction with rabbitmq and close after response is received
@@ -18,37 +20,37 @@ module RubyRabbitmqJanus
18
20
  response = transaction_long { yield }
19
21
  close
20
22
  response
21
- rescue
22
- raise Errors::Rabbit::Connect::TransactionShort
23
+ rescue => error
24
+ raise Errors::Rabbit::Connect::TransactionShort, error
23
25
  end
24
26
 
25
27
  # Create an transaction with rabbitmq and not close
26
28
  def transaction_long
27
29
  start
28
30
  yield
29
- rescue
30
- raise Errors::Rabbit::Connect::TransactionLong
31
+ rescue => error
32
+ raise Errors::Rabbit::Connect::TransactionLong, error
31
33
  end
32
34
 
33
35
  # Openning a connection with Rabbitmq
34
36
  def start
35
37
  @rabbit.start
36
- rescue
37
- raise Errors::Rabbit::Start
38
+ rescue => error
39
+ raise Errors::Rabbit::Connect::Start, error
38
40
  end
39
41
 
40
42
  # Close connection to server RabbitMQ
41
43
  def close
42
44
  @rabbit.close
43
- rescue
44
- raise Errors::Rabbit::Close
45
+ rescue => error
46
+ raise Errors::Rabbit::Connect::Close, error
45
47
  end
46
48
 
47
49
  # Create an channel
48
50
  def channel
49
51
  @rabbit.create_channel
50
- rescue
51
- raise Errors::Rabbit::Channel
52
+ rescue => error
53
+ raise Errors::Rabbit::Connect::Channel, error
52
54
  end
53
55
 
54
56
  private
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyRabbitmqJanus
4
+ module Rabbit
5
+ module Publisher
6
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
7
+ #
8
+ # # Publish message for keepalive thread
9
+ #
10
+ # The name to queue it's created automatically by Bunny GEM
11
+ #
12
+ # @see KeepaliveThread
13
+ class PublishKeepalive < Publisher
14
+ def initialize(exchange)
15
+ @reply = exchange.queue('', exclusive: true)
16
+ super(exchange)
17
+ subscribe_to_queue
18
+ end
19
+
20
+ def publish(request)
21
+ super(request)
22
+ return_response
23
+ rescue
24
+ raise Errors::Rabbit::PublishExclusive::Publish
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :reply
30
+ end
31
+ end
32
+ end
33
+ end
@@ -70,4 +70,5 @@ end
70
70
 
71
71
  require 'rrj/rabbit/publish/admin'
72
72
  require 'rrj/rabbit/publish/exclusive'
73
+ require 'rrj/rabbit/publish/keepalive'
73
74
  require 'rrj/rabbit/publish/non_exclusive'
@@ -8,27 +8,31 @@ module RubyRabbitmqJanus
8
8
  class Cluster
9
9
  include Singleton
10
10
 
11
- attr_reader :number, :enable, :sessions
11
+ attr_reader :sessions
12
12
 
13
- # Initalize object for managing each instance to Janus
13
+ # Initialize object for managing each instance to Janus
14
14
  def initialize
15
15
  @current_instance = nil
16
- config = Config.instance
17
- @enable = config.cluster
18
- @number = config.number_of_instance
19
16
  @sessions = []
20
17
  rescue
21
18
  raise Errors::Tools::Cluster::Initializer
22
19
  end
23
20
 
24
- # Create all session. One by instance
25
- def create_sessions
26
- (1..@number).each do |janus_instance|
27
- @current_instance = janus_instance
28
- @sessions.push create_session_instance
21
+ # Create session (just one Janus Instance)
22
+ def create_session
23
+ @current_instance = 1
24
+ Models::JanusInstance.create(instance: @current_instance)
25
+ rescue
26
+ raise Errors::Tools::Cluster::CreateSession
27
+ end
28
+
29
+ # Restart a thread keepalive for an instance
30
+ def restart_session
31
+ Models::JanusInstance.enabled.each do |ji|
32
+ ji.send(:create_a_session_in_janus_instance)
29
33
  end
30
34
  rescue
31
- raise Errors::Tools::Cluster::CreateSessions
35
+ raise Errors::Tools::Cluster::RestartInstance
32
36
  end
33
37
 
34
38
  # Specify a name to queue
@@ -46,20 +50,6 @@ module RubyRabbitmqJanus
46
50
  rescue
47
51
  raise Errors::Tools::Cluster::QueueAdminTo
48
52
  end
49
-
50
- private
51
-
52
- # Create a thread for manage a session/keepalive with Janus
53
- def initialize_session
54
- Janus::Concurrencies::Keepalive.new(@current_instance).session
55
- end
56
-
57
- # create a new instance in database
58
- def create_session_instance
59
- Models::JanusInstance.create(instance: @current_instance,
60
- session: initialize_session,
61
- enable: true)
62
- end
63
53
  end
64
54
  end
65
55
  end
@@ -109,18 +109,11 @@ module RubyRabbitmqJanus
109
109
 
110
110
  # @return [Boolean] Read option file for a janus cluster section
111
111
  def cluster
112
- @options['janus']['cluster']['enabled'].to_s.eql?('true') ? true : false
112
+ @options['gem']['cluster']['enabled'].to_s.match?('true') ? true : false
113
113
  rescue
114
114
  raise Errors::Tools::Config::Cluster
115
115
  end
116
116
 
117
- # Count number of Janus instances
118
- def number_of_instance
119
- @options['janus']['cluster']['count'].to_i || 1
120
- rescue
121
- raise Errors::Tools::Config::NumberOfInstance
122
- end
123
-
124
117
  private
125
118
 
126
119
  def load_configuration
@@ -1,11 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rrj/models/concerns/janus_instance_concern'
4
- if defined?(Mongoid)
5
- require 'rrj/models/mongoid'
6
- else
7
- require 'rrj/models/active_record'
8
- end
3
+ require 'rrj/models/concerns/janus_instance_callbacks'
4
+ require 'rrj/models/concerns/janus_instance_methods'
5
+ require 'rrj/models/concerns/janus_instance_validations'
6
+ require "rrj/models/#{defined?(Mongoid) ? 'mongoid' : 'active_record'}"
9
7
 
10
8
  # :reek:FeatureEnvy
11
9
 
@@ -15,14 +13,14 @@ module RubyRabbitmqJanus
15
13
 
16
14
  # # Utility for manage option to this gem.
17
15
  #
18
- # This class start all singleton, Log, Config, Request and Keepalice
19
- # instance. It's alos used for testing session/handle used in request.
16
+ # This class start all singleton, Log, Config, Request and Keepalive
17
+ # instance. It's also used for testing session/handle used in request.
20
18
  class Option
21
19
  def initialize
22
20
  Log.instance
23
21
  Config.instance
24
22
  Requests.instance
25
- Cluster.instance.create_sessions
23
+ cluster_mode
26
24
  rescue => error
27
25
  raise Errors::Tools::Option::Initialize, error
28
26
  end
@@ -56,6 +54,15 @@ module RubyRabbitmqJanus
56
54
  rescue
57
55
  raise Errors::Tools::Option::UseCurrentHandle, options
58
56
  end
57
+
58
+ private
59
+
60
+ def cluster_mode
61
+ method = Config.instance.cluster ? :restart_session : :create_session
62
+ Cluster.instance.send(method)
63
+ rescue
64
+ raise Errors::Tools::Option::ClusterMode
65
+ end
59
66
  end
60
67
  end
61
68
  end
@@ -3,10 +3,6 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe RubyRabbitmqJanus::Tools::Cluster, type: :tools, name: :cluster do
6
- it { expect(RubyRabbitmqJanus::Tools::Cluster.instance.enable).to eql(false) }
7
-
8
- it { expect(RubyRabbitmqJanus::Tools::Cluster.instance.number).to eql(1) }
9
-
10
6
  it do
11
7
  expect(RubyRabbitmqJanus::Tools::Cluster.instance.queue_to(42)).to \
12
8
  eql('to-janus-42')
@@ -19,8 +15,8 @@ describe RubyRabbitmqJanus::Tools::Cluster, type: :tools, name: :cluster do
19
15
 
20
16
  context 'when created sessions with active record' do
21
17
  it do
22
- expect(RubyRabbitmqJanus::Tools::Cluster.instance.create_sessions).to \
23
- be_a(Range)
18
+ expect(RubyRabbitmqJanus::Tools::Cluster.instance.create_session).to \
19
+ be_a(Integer)
24
20
  end
25
21
  end
26
22
  end
@@ -41,16 +41,11 @@ describe RubyRabbitmqJanus::Tools::Config, type: :tools, name: :config do
41
41
  it_behaves_like 'type and default value', String, ''
42
42
  end
43
43
 
44
- context 'janus/cluster/enabled' do
44
+ context 'gem/cluster/enabled' do
45
45
  let(:method) { @cfg.cluster }
46
46
  it_behaves_like 'type and default value', FalseClass, false
47
47
  end
48
48
 
49
- context 'janus/cluster/count' do
50
- let(:method) { @cfg.number_of_instance }
51
- it_behaves_like 'type and default value', Integer, 1
52
- end
53
-
54
49
  context 'queues/standard/from' do
55
50
  let(:method) { @cfg.queue_from }
56
51
  it_behaves_like 'type and default value', String, 'from-janus'
@@ -55,13 +55,11 @@
55
55
  "type": "hash",
56
56
  "properties": {
57
57
  "plugins": { "type": "hash" },
58
- "session": { "type": "hash" },
59
- "cluster": { "type": "hash" }
58
+ "session": { "type": "hash" }
60
59
  },
61
60
  "required": [
62
61
  "session",
63
- "plugins",
64
- "cluster"
62
+ "plugins"
65
63
  ]
66
64
  },
67
65
  "gem": {
@@ -75,10 +73,20 @@
75
73
  "required": [
76
74
  "level"
77
75
  ]
76
+ },
77
+ "cluster": {
78
+ "type": "hash",
79
+ "properties": {
80
+ "enabled": { "type": "boolean" }
81
+ },
82
+ "required": [
83
+ "enabled"
84
+ ]
78
85
  }
79
86
  },
80
87
  "required": [
81
- "log"
88
+ "log",
89
+ "cluster"
82
90
  ]
83
91
  }
84
92
  },
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_rabbitmq_janus
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.0.pre.42
5
5
  platform: ruby
6
6
  authors:
7
7
  - VAILLANT Jeremy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-14 00:00:00.000000000 Z
11
+ date: 2017-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -318,6 +318,20 @@ dependencies:
318
318
  - - "~>"
319
319
  - !ruby/object:Gem::Version
320
320
  version: 0.0.1
321
+ - !ruby/object:Gem::Dependency
322
+ name: timers
323
+ requirement: !ruby/object:Gem::Requirement
324
+ requirements:
325
+ - - "~>"
326
+ - !ruby/object:Gem::Version
327
+ version: '4.1'
328
+ type: :runtime
329
+ prerelease: false
330
+ version_requirements: !ruby/object:Gem::Requirement
331
+ requirements:
332
+ - - "~>"
333
+ - !ruby/object:Gem::Version
334
+ version: '4.1'
321
335
  description: |2
322
336
  This gem is used to communicate to a server Janus through RabbitMQ software
323
337
  (Message-oriented middleware). It waiting a messages to Rails API who send to
@@ -378,6 +392,9 @@ files:
378
392
  - lib/rrj/errors/janus/processus/concurency.rb
379
393
  - lib/rrj/errors/janus/processus/event.rb
380
394
  - lib/rrj/errors/janus/processus/keepalive.rb
395
+ - lib/rrj/errors/janus/processus/keepalive/initializer.rb
396
+ - lib/rrj/errors/janus/processus/keepalive/thread.rb
397
+ - lib/rrj/errors/janus/processus/keepalive/timer.rb
381
398
  - lib/rrj/errors/janus/responses/admin.rb
382
399
  - lib/rrj/errors/janus/responses/event.rb
383
400
  - lib/rrj/errors/janus/responses/response.rb
@@ -411,7 +428,10 @@ files:
411
428
  - lib/rrj/janus/messages/standard.rb
412
429
  - lib/rrj/janus/processus/concurrency.rb
413
430
  - lib/rrj/janus/processus/event.rb
414
- - lib/rrj/janus/processus/keepalive.rb
431
+ - lib/rrj/janus/processus/keepalive/keepalive_initializer.rb
432
+ - lib/rrj/janus/processus/keepalive/keepalive_message.rb
433
+ - lib/rrj/janus/processus/keepalive/keepalive_thread.rb
434
+ - lib/rrj/janus/processus/keepalive/keepalive_timer.rb
415
435
  - lib/rrj/janus/responses/admin.rb
416
436
  - lib/rrj/janus/responses/event.rb
417
437
  - lib/rrj/janus/responses/response.rb
@@ -421,13 +441,16 @@ files:
421
441
  - lib/rrj/janus/transactions/session.rb
422
442
  - lib/rrj/janus/transactions/transaction.rb
423
443
  - lib/rrj/models/active_record.rb
424
- - lib/rrj/models/concerns/janus_instance_concern.rb
444
+ - lib/rrj/models/concerns/janus_instance_callbacks.rb
445
+ - lib/rrj/models/concerns/janus_instance_methods.rb
446
+ - lib/rrj/models/concerns/janus_instance_validations.rb
425
447
  - lib/rrj/models/mongoid.rb
426
448
  - lib/rrj/rabbit/connect.rb
427
449
  - lib/rrj/rabbit/propertie.rb
428
450
  - lib/rrj/rabbit/publish/admin.rb
429
451
  - lib/rrj/rabbit/publish/base_publisher.rb
430
452
  - lib/rrj/rabbit/publish/exclusive.rb
453
+ - lib/rrj/rabbit/publish/keepalive.rb
431
454
  - lib/rrj/rabbit/publish/listener.rb
432
455
  - lib/rrj/rabbit/publish/non_exclusive.rb
433
456
  - lib/rrj/rabbit/publish/publisher.rb
@@ -549,12 +572,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
549
572
  version: '0'
550
573
  required_rubygems_version: !ruby/object:Gem::Requirement
551
574
  requirements:
552
- - - ">="
575
+ - - ">"
553
576
  - !ruby/object:Gem::Version
554
- version: '0'
577
+ version: 1.3.1
555
578
  requirements: []
556
579
  rubyforge_project:
557
- rubygems_version: 2.4.8
580
+ rubygems_version: 2.6.11
558
581
  signing_key:
559
582
  specification_version: 4
560
583
  summary: Ruby RabbitMQ Janus
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyRabbitmqJanus
4
- module Janus
5
- module Concurrencies
6
- # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
7
- #
8
- # # Manage keepalive message
9
- #
10
- # Create a thread for sending a message with type keepalive to session
11
- # created by this instanciate gem
12
- #
13
- # @see https://ruby-doc.org/stdlib-2.4.0/libdoc/singleton/rdoc/Singleton.html
14
- class Keepalive < Concurrency
15
- # Initialize a singleton object for sending keepalive to janus
16
- def initialize(instance)
17
- @pub = @session = nil
18
- @instance = instance
19
- super()
20
- rescue
21
- raise Errors::Janus::Keepalive::Initializer
22
- end
23
-
24
- # Give a session Integer created when this gem is intanciate.
25
- # Is waiting a thread return a response to message created sending.
26
- #
27
- # @example Ask session
28
- # Keepalive.session
29
- # => 852803383803249
30
- #
31
- # @return [Fixnum] Identifier to session created by Janus
32
- def session
33
- lock.synchronize do
34
- condition.wait(lock)
35
- @session
36
- end
37
- rescue
38
- raise Errors::Janus::Keepalive::Session
39
- end
40
-
41
- private
42
-
43
- def transaction_running
44
- @session = find_session
45
- lock.synchronize { condition.signal }
46
- loop { loop_session(Tools::Config.instance.ttl) }
47
- end
48
-
49
- def loop_session(time_to_live)
50
- sleep time_to_live
51
- @pub.publish(message_keepalive)
52
- Tools::Log.instance.info "Keepalive for #{@session}"
53
- end
54
-
55
- def create_session
56
- @pub = Rabbit::Publisher::PublishExclusive.new(rabbit.channel, '')
57
- msg = Janus::Messages::Standard.new('base::create', param_instance)
58
- @pub.publish(msg)
59
- end
60
-
61
- def message_keepalive
62
- Janus::Messages::Standard.new('base::keepalive', param_session)
63
- end
64
-
65
- def find_session
66
- Janus::Responses::Standard.new(create_session).session
67
- end
68
-
69
- def param_instance
70
- { 'instance' => @instance }
71
- end
72
-
73
- def param_session
74
- { 'session_id' => @session }.merge(param_instance)
75
- end
76
- end
77
- end
78
- end
79
- end