Subversion Repositories SmartDukaan

Rev

Rev 3915 | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 06-Apr-2010

@author: ashish
@author: Chandranshu
'''
import optparse
import sys
import os

if __name__ == '__main__' and __package__ is None:
    sys.path.insert(0, os.getcwd())

from thrift.transport import TSocket, TTransport
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
from thrift.server import TServer

from shop2020.utils.daemon import Daemon
from shop2020.model.v1.order.impl.OrderServiceHandler import OrderServiceHandler
from shop2020.thriftpy.model.v1.order import TransactionService
from shop2020.config.client.ConfigClient import ConfigClient

class TransactionServer(Daemon):
    
    def __init__(self, logfile='/var/log/services/order.log', pidfile='/tmp/transaction-server.pid'):
        Daemon.__init__(self, pidfile, stdout=logfile, stderr=logfile)
        
    def run(self):
    
        config_client = ConfigClient()
        host_name = config_client.get_property('transaction_service_server_host')
        port = config_client.get_property('transaction_service_server_port')
        dbname = config_client.get_property('transaction_service_dbname')
        db_hostname = config_client.get_property('transaction_service_db_hostname')
            
        handler = OrderServiceHandler(dbname, db_hostname)
        processor = TransactionService.Processor(handler)
        transport = TSocket.TServerSocket(port=port)
        tfactory = TTransport.TFramedTransportFactory()
        pfactory = TBinaryProtocolFactory()
        server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
        print "Starting Transaction Service at, port " + str(port) + " host " + host_name
        sys.stdout.flush()
        server.serve()
        print "Server functioning"

if __name__ == "__main__":
    parser = optparse.OptionParser()
    parser.add_option("-l", "--logfile", dest="logfile",
                      type="string",
                      help="Log all output to LOG_FILE",
                      )
    parser.add_option("-i", "--pidfile", dest="pidfile",
                      type="string",
                      help="Write the PID to pidfile")
    (options, args) = parser.parse_args()
    daemon = TransactionServer(options.logfile, options.pidfile)
    if len(args) == 0:
        daemon.run()
    elif len(args) == 1:
        if 'start' == args[0]:
            daemon.start()
        elif 'stop' == args[0]:
            daemon.stop()
        elif 'restart' == args[0]:
            daemon.restart()
        else:
            print "Unknown command"
            sys.exit(2)
        sys.exit(0)
    else:
        print "usage: %s start|stop|restart" % sys.argv[0]
        sys.exit(2)