Sha256: 88133bbac4e493db84708238dc113b3c49fa50f22f5f7ce0e77b7723fd6a4963

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module RubyRabbitmqJanus
  module Janus
    module Concurrencies
      # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
      # Manage sending keepalive message
      class Keepalive < Concurrency
        include Singleton

        # Initalize a keepalive message
        def initialize
          @publish = @response = nil
          super
        end

        # Return an number session created
        def session
          wait { @response.session }
        end

        private

        # Star an session with janus and waiting an signal for saving session_id
        def transaction_running
          @response = Janus::Response.new(create_session)
          signal
          session_keepalive(ttl)
        end

        # Create an loop for sending a keepalive message
        def session_keepalive(time_to_live)
          loop do
            sleep time_to_live
            @publish.send_a_message(Janus::Message.new('base::keepalive',
                                                       'session_id' => running_session))
          end
        rescue => message
          Tools::Log.instance.debug "Error keepalive : #{message}"
        end

        # Define a Time To Live between each request sending to janus
        def ttl
          Tools::Config.instance.options['gem']['session']['keepalive'].to_i
        rescue => error
          Tools::Log.instance.debug "TTL Not loading - #{error}"
        end

        # Create an message and publish for create session in Janus
        def create_session
          @publish = Rabbit::Publisher::PublishExclusive.new(rabbit.channel, '')
          @publish.send_a_message(Janus::Message.new('base::create'))
        end

        # Return session_id
        def running_session
          @response.session
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby_rabbitmq_janus-1.0.5 lib/rrj/janus/processus/keepalive.rb
ruby_rabbitmq_janus-1.0.4 lib/rrj/janus/processus/keepalive.rb