Class: Izanami::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/izanami/channel.rb

Overview

Open a communication channel between workers to handle the output of a command.

A channel does two things: it publish and reads the data sent by a worker to a Redis pub/sub channel and stores that data when the channel is closed. That way, a process can read the output of a command when this is been executed or when is finished.

Defined Under Namespace

Classes: Input

Constant Summary collapse

EOC =

End Of Channel

'--EOC--'
SEPARATOR =
"\n"

Instance Method Summary collapse

Constructor Details

#initialize(mapper) ⇒ Channel

Returns a new instance of Channel.

Parameters:

  • mapper (Izanami::Mapper::Command)

    the instance which talks to redis.



64
65
66
# File 'lib/izanami/channel.rb', line 64

def initialize(mapper)
  @mapper = mapper
end

Instance Method Details

#read(id) {|String| ... } ⇒ Object

Opens the channel for reading.

Parameters:

  • id (String)

    the command id.

Yields:

  • (String)

    each of the lines read from the channel.



93
94
95
96
97
98
99
100
101
102
# File 'lib/izanami/channel.rb', line 93

def read(id, &block)
  command = @mapper.find(id) || {}
  output  = command['output']

  if output
    read_string(output, &block)
  else
    read_buffer(id, &block)
  end
end

#write(id) {|Izanami::Channel::Input| ... } ⇒ Izanami::Channel::Input

Note:

if a block is given, the input is closed when the block

Opens the channel to write.

finish. If not, the client of the channel must close it.

Parameters:

  • id (String)

    the command id.

Yields:

Returns:



78
79
80
81
82
83
84
85
86
# File 'lib/izanami/channel.rb', line 78

def write(id)
  input = Input.new(@mapper, id)
  if block_given?
    yield input
    input.close
  end

  input
end