# -*- coding: utf-8 -*-

import os, time
import redis
from subprocess import Popen

def start_q_server():
	f=file("redis.log","a+")
	exe_name=os.path.split(__file__)[0]+"/redis-server"
	if not os.name=='posix': exe_name+=".exe"
	print "start", exe_name
	Popen(exe_name, stdout=f)
	time.sleep(2)

def queqe_server(q=None):
    return q or redis.Redis(host='localhost', port=6379, db=0)
    """try:
        q.ping()
    except redis.exceptions.InvalidResponse:
        print "InvalidResponse while queqe_server"
    except redis.exceptions.ConnectionError: 
        print "ConnectionError while queqe_server"
    return q"""

def check_and_start_queqe_server(q=None):
	q=q or redis.Redis(host='localhost', port=6379, db=0)
	try:
		q.ping()
	except redis.exceptions.ConnectionError: 
		start_q_server()
	except redis.exceptions.InvalidResponse:
		pass
	return q
    
