Skip to content

Commit

Permalink
fix xmlrpc server not stopped bug, check xmlrpc server stop
Browse files Browse the repository at this point in the history
  • Loading branch information
binux committed Mar 6, 2016
1 parent 0e5b363 commit 77196e8
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 10 deletions.
7 changes: 4 additions & 3 deletions pyspider/fetcher/tornado_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ def quit(self):
self._running = False
self._quit = True
self.ioloop.add_callback(self.ioloop.stop)
if hasattr(self, 'xmlrpc_ioloop'):
if hasattr(self, 'xmlrpc_server'):
self.xmlrpc_ioloop.add_callback(self.xmlrpc_server.stop)
self.xmlrpc_ioloop.add_callback(self.xmlrpc_ioloop.stop)

def size(self):
Expand Down Expand Up @@ -578,8 +579,8 @@ def dump_counter(_time, _type):

container = tornado.wsgi.WSGIContainer(application)
self.xmlrpc_ioloop = tornado.ioloop.IOLoop()
http_server = tornado.httpserver.HTTPServer(container, io_loop=self.xmlrpc_ioloop)
http_server.listen(port=port, address=bind)
self.xmlrpc_server = tornado.httpserver.HTTPServer(container, io_loop=self.xmlrpc_ioloop)
self.xmlrpc_server.listen(port=port, address=bind)
self.xmlrpc_ioloop.start()

def on_fetch(self, type, task):
Expand Down
10 changes: 10 additions & 0 deletions pyspider/libs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
import hashlib
import datetime
import socket
import base64

import six
Expand Down Expand Up @@ -409,3 +410,12 @@ def python_console(namespace=None):
namespace.update(caller.f_locals)

return get_python_console(namespace=namespace).interact()


def check_port_open(port, addr='127.0.0.1'):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((addr, port))
if result == 0:
return True
else:
return False
13 changes: 7 additions & 6 deletions pyspider/scheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,9 @@ def quit(self):
'''Set quit signal'''
self._quit = True
# stop xmlrpc server
if hasattr(self, 'ioloop'):
self.ioloop.add_callback(self.ioloop.stop)
if hasattr(self, 'xmlrpc_server'):
self.xmlrpc_ioloop.add_callback(self.xmlrpc_server.stop)
self.xmlrpc_ioloop.add_callback(self.xmlrpc_ioloop.stop)

def run_once(self):
'''comsume queues and feed tasks to fetcher, once'''
Expand Down Expand Up @@ -578,10 +579,10 @@ def get_active_tasks(project=None, limit=100):
import tornado.httpserver

container = tornado.wsgi.WSGIContainer(application)
self.ioloop = tornado.ioloop.IOLoop()
http_server = tornado.httpserver.HTTPServer(container, io_loop=self.ioloop)
http_server.listen(port=port, address=bind)
self.ioloop.start()
self.xmlrpc_ioloop = tornado.ioloop.IOLoop()
self.xmlrpc_server = tornado.httpserver.HTTPServer(container, io_loop=self.xmlrpc_ioloop)
self.xmlrpc_server.listen(port=port, address=bind)
self.xmlrpc_ioloop.start()

def on_request(self, task):
if self.INQUEUE_LIMIT and len(self.task_queue[task['project']]) >= self.INQUEUE_LIMIT:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ def tearDownClass(self):
self.phantomjs.wait()
self.rpc._quit()
self.thread.join()

assert not utils.check_port_open(5000)
assert not utils.check_port_open(23333)
assert not utils.check_port_open(24444)
assert not utils.check_port_open(25555)
assert not utils.check_port_open(14887)

time.sleep(1)

def test_10_http_get(self):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def tearDownClass(self):
self.httpbin_thread.terminate()
self.httpbin_thread.join()

assert not utils.check_port_open(5000)
assert not utils.check_port_open(23333)
assert not utils.check_port_open(24444)
assert not utils.check_port_open(25555)
assert not utils.check_port_open(14887)

shutil.rmtree('./data/tests', ignore_errors=True)

def test_10_cli(self):
Expand Down Expand Up @@ -323,6 +329,11 @@ def tearDownClass(self):
self.scheduler_thread.join()
time.sleep(1)

assert not utils.check_port_open(5000)
assert not utils.check_port_open(23333)
assert not utils.check_port_open(24444)
assert not utils.check_port_open(25555)

shutil.rmtree('./data/tests', ignore_errors=True)

def test_10_send_message(self):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
logging.config.fileConfig("pyspider/logging.conf")

from pyspider.scheduler.task_queue import TaskQueue
from pyspider.libs import utils


class TestTaskQueue(unittest.TestCase):
Expand Down Expand Up @@ -157,6 +158,11 @@ def tearDownClass(self):
shutil.rmtree('./data/tests', ignore_errors=True)
time.sleep(1)

assert not utils.check_port_open(5000)
assert not utils.check_port_open(self.scheduler_xmlrpc_port)
assert not utils.check_port_open(24444)
assert not utils.check_port_open(25555)

def test_10_new_task_ignore(self):
self.newtask_queue.put({
'taskid': 'taskid',
Expand Down
8 changes: 7 additions & 1 deletion tests/test_webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def setUpClass(self):
self.threads = []

ctx = run.scheduler.make_context('scheduler', [], self.ctx)
scheduler = run.scheduler.invoke(ctx)
self.scheduler = scheduler = run.scheduler.invoke(ctx)
self.threads.append(run_in_thread(scheduler.xmlrpc_run))
self.threads.append(run_in_thread(scheduler.run))

Expand Down Expand Up @@ -81,6 +81,12 @@ def tearDownClass(self):
self.httpbin_thread.terminate()
self.httpbin_thread.join()

assert not utils.check_port_open(5000)
assert not utils.check_port_open(23333)
assert not utils.check_port_open(24444)
assert not utils.check_port_open(25555)
assert not utils.check_port_open(14887)

shutil.rmtree('./data/tests', ignore_errors=True)

def test_10_index_page(self):
Expand Down

0 comments on commit 77196e8

Please sign in to comment.