Class: Squongo::Writer
- Inherits:
-
Object
- Object
- Squongo::Writer
- Defined in:
- lib/squongo/writer.rb
Instance Attribute Summary collapse
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
-
#writer ⇒ Object
readonly
Returns the value of attribute writer.
Instance Method Summary collapse
-
#initialize(reader, writer, parent_pid) ⇒ Writer
constructor
A new instance of Writer.
- #insert(table, data) ⇒ Object
- #monitor_parent ⇒ Object
- #should_live ⇒ Object
- #start ⇒ Object
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
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
2 3 4 |
# File 'lib/squongo/writer.rb', line 2 def reader @reader end |
#writer ⇒ Object (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. end |
#monitor_parent ⇒ Object
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_live ⇒ Object
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 |
#start ⇒ Object
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 |