Skip to content

Instantly share code, notes, and snippets.

@koyachi
Created December 22, 2009 08:00
Show Gist options
  • Save koyachi/261595 to your computer and use it in GitHub Desktop.
Save koyachi/261595 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
$:.unshift File.dirname(__FILE__) + '/../../lib/'
require 'rubygems'
require 'q4m'
database = "test_q4m"
table = "example_json_queue_monitor"
sql = "CREATE TABLE IF NOT EXISTS #{table} (v TEXT NOT NULL) ENGINE=QUEUE;"
begin
dbh = DBI.connect "DBI:Mysql:database=#{database}", "root", ""
dbh.do sql
rescue
p "ERROR: #{$!}"
end
$:.unshift File.dirname(__FILE__) + '/../../lib/'
require 'rubygems'
require 'q4m'
require 'json'
q = Q4M.connect :connect_info => ['DBI:Mysql:database=test_q4m', 'root', '']
table = 'example_json_queue_monitor'
puts '[client] write message:'
input = gets
data = {:message => input, :time => Time.now}
q.insert(table, :v => data.to_json)
# 適当にjsonでしまっとくサンプル
$:.unshift File.dirname(__FILE__) + '/../../lib/'
require 'rubygems'
require 'q4m'
require 'json'
Signal.trap(:INT) do
Q4M.disconnect
p 'END'
exit
end
q = Q4M.connect :connect_info => ['DBI:Mysql:database=test_q4m', 'root', '']
table = 'example_json_queue_monitor'
while q.next(table, 5)
v = q.fetch(table)[0]
data = JSON.parse(v)
p "[DATA] time = #{data['time']}, message = #{data['message']}"
p "...waiting..."
sleep 1
end
Q4M.disconnect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment