#!/usr/bin/python
#encoding=utf-8
import beanstalkc
import yaml
import json
import MySQLdb
#load config
handle = open('config.yaml')
conf = yaml.load(handle)
#get data from beanstalkd
def get_beanstalk_data(conf):
beanstalk = beanstalkc.Connection(host=conf['beans']['host'],port=conf['beans']['port'])
beanstalk.use('test')
beanstalk.watch('test')
job = beanstalk.reserve()
data = json.loads(job.body)
job.delete()
return data
#insert data to mysql
def insert_to_mysql(conf,data):
try:
conn = MySQLdb.connect(host=conf['mysql']['host'], user=conf['mysql']['user'],passwd=conf['mysql']['pass'], port=conf['mysql']['port'], db=conf['mysql']['db'])
cursor = conn.cursor()
sql = "insert into stat_plugin_log (id, md, cd, st) values(%s, %s, %s, %s)"
param = (data['id'], data['md'], data['cd'], data['st'])
cursor.execute(sql, param)
cursor.close()
conn.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
#go
while True:
data = get_beanstalk_data(conf)
insert_to_mysql(conf,data)