Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

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.config.client.ConfigClient import ConfigClient
from shop2020.thriftpy.model.v1.dtr import  DtrService
from shop2020.model.v1.dtr.impl.DtrServiceHandler import DtrServiceHandler

class DtrServer(Daemon):
    
    def __init__(self, logfile='/var/log/services/dtr.log', pidfile='/tmp/dtr-server.pid'):
        Daemon.__init__(self, pidfile, stdout=logfile, stderr=logfile)

    def run(self):

        config_client = ConfigClient()
        host_name = config_client.get_property('dtr_service_server_host')
        port = config_client.get_property('dtr_service_server_port')
        
        handler = DtrServiceHandler()
        processor = DtrService.Processor(handler)
        transport = TSocket.TServerSocket(port=port)
        tfactory = TTransport.TFramedTransportFactory()
        pfactory = TBinaryProtocolFactory()
        server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
        print "Starting DtrService 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 = DtrServer(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)