コード例 #1
0
ファイル: server.py プロジェクト: graham/jitsu
    def start(self):
        self.running = 1
        self.needs_restart = 0
    
        while self.running:
            print('Starting Server...')
            start_time = int(time.time())    
        
            server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        
            last_dynamic_code_check = time.time()
        
            pid = os.fork()
        
            if pid == 0:
                print('    Loading...')
                import jitsu
                import jitsu.server
                import jitsu.server.module
            
                from jitsu.server.handler import RequestHandler
                from jitsu.server.jsocket import JitsuHTTPServer
            
            
                httpd = JitsuHTTPServer(server_socket, ('127.0.0.1', 8000), RequestHandler)

                reads = []
                reads.append(sys.stdin)
                reads.append(httpd)
            
                try:
                    print('    Pre-load complete, loading modules', server_socket.getsockname())
                
                    module_list = []
                    httpd.module_list = module_list
                    httpd.job_list = []
                
                    for i in jitsu.server.module.get_all_modules():
                        m = jitsu.server.module.JitsuModule()
                        m.load_module(i)
                        module_list.append(m)
                    
                    if jitsu.server.master_route_object.search('/'):
                        #pass no problem.
                        pass
                    else:
                        # load the default module.
                        m = jitsu.server.module.JitsuModule()
                        m.load_module(['jitsu.apps.default'])
                        module_list.append(m)
            
                    jitsu.server.master_route_object.sort()
                
                    while not self.needs_restart:
                        tmp = select.select(reads, [], [], 0.25)
                    
                        for i in tmp[0]:
                            if i in (httpd, ):
                                x = threading.Thread(target=i.handle_request)
                                x.start()
                            elif i == sys.stdin:
                                tmp = sys.stdin.readline().strip()
                                if tmp == 'restart' or tmp == 'r':
                                    print('restarting...')
                                    self.needs_restart = 1
                                elif tmp == 'pause' or tmp == 'p':
                                    print('pausing for 5 seconds')
                                    for i in range(1, 6):
                                        print(i)
                                        time.sleep(1)
                               
                         
                        if httpd.job_list:
                            x = httpd.job_list.pop()
                            thread = threading.Thread(target=run_job, args=(x,))
                            thread.start()

                except KeyboardInterrupt:
                    print()
                    print("    Shutting Down modules...")                    
                    print("    Shutting Down core...")
                    end_time = int(time.time())
                    print("Server uptime: %.2f minutes" % ((end_time - start_time) / 60.0))
                    self.running = 0
                
                sys.exit(0)
            else:
                try:
                    result = os.waitpid(int(pid), 0)
                    print('result', result)
                    if result[1] == 256:
                        self.running = 0
                except KeyboardInterrupt, ki:
                    self.running = 0
                    print('')
                
            print('Shutdown Complete.')
コード例 #2
0
ファイル: server.py プロジェクト: graham/jitsu
def start():
    import http
    running = 1
    needs_restart = 0
    import queue
    
    while running:
        print('Starting Server...')
        start_time = int(time.time())    
        
        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        
        last_dynamic_code_check = time.time()
        
        pid = os.fork()
        
        if pid == 0:
            print('    Loading...')
            import jitsu
            import jitsu.server.module
            import jitsu.server.jobber
            
            from jitsu.server.handler import RequestHandler
            from jitsu.server.jsocket import JitsuHTTPServer
            
            httpd = JitsuHTTPServer(server_socket, ('127.0.0.1', 8000), RequestHandler)
            
            job_queue_parent, job_queue_child = multiprocessing.Pipe()
            httpd.parent_job_queue = job_queue_child
            
            job_process = multiprocessing.Process(target=jitsu.server.jobber.job_runner,
                                        args=(job_queue_parent,) )
            job_process.start()
            
            reads = []
            reads.append(sys.stdin)
            reads.append(httpd)
            
            try:
                print('    Pre-load complete, loading modules', server_socket.getsockname())
                
                modules = jitsu.server.module.get_all_modules()
                module_list = []
                
                for i in modules:
                    command_pipe_parent, command_pipe_child = multiprocessing.Pipe()
                    o = jitsu.server.module.JitsuModule()
                    o.load_module(i, command_pipe_child, None)
                    p = multiprocessing.Process(target=o.run, 
                                                args=(command_pipe_child, None))
                    p.start()
                    module_list.append( ((command_pipe_parent, command_pipe_child), p, o) )
                
                print('    Module Process creation complete.')
                httpd.module_list = module_list
                
                while not needs_restart:
                    tmp = select.select(reads, [], [], 0.25)
                    
                    for i in tmp[0]:
                        if i in (httpd, ):
                            i.handle_request()
                        elif i == sys.stdin:
                            tmp = sys.stdin.readline().strip()
                            if tmp == 'restart' or tmp == 'r':
                                print('restarting...')
                                needs_restart = 1
                            elif tmp == 'pause' or tmp == 'p':
                                print('pausing for 5 seconds')
                                for i in range(1, 6):
                                    print(i)
                                    time.sleep(1)
                                    
                    #while not parent_job_queue.empty():
                    #    job_queue.send(parent_job_queue.recv())
                                                
                for command_pipe, process in module_list:
                    process.terminate()
            except KeyboardInterrupt:
                print()
                print("    Shutting Down modules...")
                for command_pipe, process, obj in module_list:
                    process.terminate()
                    
                print("    Shutting Down core...")
                end_time = int(time.time())
                print("Server uptime: %.2f minutes" % ((end_time - start_time) / 60.0))
                running = 0
                
            job_process.terminate()
            sys.exit(0)
        else:
            try:
                result = os.waitpid(int(pid), 0)
                print('result', result)
                if result[1] == 256:
                    running = 0
            except KeyboardInterrupt, ki:
                running = 0
                print('')
                
        print('Shutdown Complete.')