Class: Skein::Connected

Inherits:
Object
  • Object
show all
Defined in:
lib/skein/connected.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection: nil, context: nil) ⇒ Connected

Instance Methods =====================================================



11
12
13
14
15
16
17
18
19
20
# File 'lib/skein/connected.rb', line 11

def initialize(connection: nil, context: nil)
  @mutex = Mutex.new
  @shared_connection = !!connection

  @connection = connection || Skein::RabbitMQ.connect
  @channel = @connection.create_channel

  @context = context || Skein::Context.new
  @ident = @context.ident(self)
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



7
8
9
# File 'lib/skein/connected.rb', line 7

def channel
  @channel
end

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/skein/connected.rb', line 6

def connection
  @connection
end

#contextObject (readonly)

Properties ===========================================================



4
5
6
# File 'lib/skein/connected.rb', line 4

def context
  @context
end

#identObject (readonly)

Returns the value of attribute ident.



5
6
7
# File 'lib/skein/connected.rb', line 5

def ident
  @ident
end

Instance Method Details

#closeObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/skein/connected.rb', line 28

def close
  lock do
    begin
      @channel and @channel.close

    rescue => e
      if (defined?(MarchHare))
        case e
        when MarchHare::ChannelLevelException, MarchHare::ChannelAlreadyClosed
          # Ignored since we're finished with the channel anyway
        else
          raise e
        end
      end
    end

    @channel = nil

    unless (@shared_connection)
      @connection and @connection.close
      @connection = nil
    end
  end
end

#lockObject



22
23
24
25
26
# File 'lib/skein/connected.rb', line 22

def lock
  @mutex.synchronize do
    yield
  end
end