Class: Squongo::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/squongo/writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader, writer, parent_pid) ⇒ Writer

Returns a new instance of Writer.



4
5
6
7
8
# File 'lib/squongo/writer.rb', line 4

def initialize(reader, writer, parent_pid)
  @reader = reader
  @writer = writer
  @parent_pid = parent_pid
end

Instance Attribute Details

#readerObject (readonly)

Returns the value of attribute reader.



2
3
4
# File 'lib/squongo/writer.rb', line 2

def reader
  @reader
end

#writerObject (readonly)

Returns the value of attribute writer.



2
3
4
# File 'lib/squongo/writer.rb', line 2

def writer
  @writer
end

Instance Method Details

#insert(table, data) ⇒ Object



23
24
25
# File 'lib/squongo/writer.rb', line 23

def insert(table, data)
  Squongo.connection.db.execute "INSERT INTO #{table} (data, updated_at) VALUES(?, ?)", data.to_json, Squongo.timestamp
end

#monitor_parentObject



34
35
36
37
38
39
40
41
# File 'lib/squongo/writer.rb', line 34

def monitor_parent
  @monitor_thread = Thread.new do
    loop do
      exit unless should_live
      sleep 0.1
    end
  end
end

#should_liveObject



27
28
29
30
31
32
# File 'lib/squongo/writer.rb', line 27

def should_live
  Process.getpgid(@parent_pid)
  true
rescue Errno::ESRCH
  false
end

#startObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/squongo/writer.rb', line 10

def start
  monitor_parent

  while packet = @reader.gets
    model_information = Squongo.ipc_decode(packet)

    table = model_information['table']
    data  = model_information['data']

    insert(table, data)
  end
end