mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Limit the amount of time to wait for BonJour to shutdown
This commit is contained in:
parent
7548a52e3d
commit
96746583a2
@ -11,8 +11,9 @@ from threading import Event
|
|||||||
|
|
||||||
class BonJour(object): # {{{
|
class BonJour(object): # {{{
|
||||||
|
|
||||||
def __init__(self, name='Books in calibre', service_type='_calibre._tcp', path='/opds', add_hostname=True):
|
def __init__(self, name='Books in calibre', service_type='_calibre._tcp', path='/opds', add_hostname=True, wait_for_stop=True):
|
||||||
self.service_name = name
|
self.service_name = name
|
||||||
|
self.wait_for_stop = wait_for_stop
|
||||||
self.service_type = service_type
|
self.service_type = service_type
|
||||||
self.add_hostname = add_hostname
|
self.add_hostname = add_hostname
|
||||||
self.path = path
|
self.path = path
|
||||||
@ -42,6 +43,6 @@ class BonJour(object): # {{{
|
|||||||
|
|
||||||
self.shutdown.wait()
|
self.shutdown.wait()
|
||||||
for s in mdns_services:
|
for s in mdns_services:
|
||||||
unpublish(*s, add_hostname=self.add_hostname)
|
unpublish(*s, add_hostname=self.add_hostname, wait_for_stop=self.wait_for_stop)
|
||||||
self.stopped.set()
|
self.stopped.set()
|
||||||
# }}}
|
# }}}
|
||||||
|
@ -66,7 +66,7 @@ class Server(object):
|
|||||||
self.handler = Handler(library_broker, opts, notify_changes=notify_changes)
|
self.handler = Handler(library_broker, opts, notify_changes=notify_changes)
|
||||||
plugins = self.plugins = []
|
plugins = self.plugins = []
|
||||||
if opts.use_bonjour:
|
if opts.use_bonjour:
|
||||||
plugins.append(BonJour())
|
plugins.append(BonJour(wait_for_stop=max(0, opts.shutdown_timeout - 0.2)))
|
||||||
self.opts = opts
|
self.opts = opts
|
||||||
self.log, self.access_log = log, access_log
|
self.log, self.access_log = log, access_log
|
||||||
self.handler.set_log(self.log)
|
self.handler.set_log(self.log)
|
||||||
|
@ -75,7 +75,7 @@ class Server(object):
|
|||||||
self.handler.router.ctx.search_the_net_urls = json.load(f)
|
self.handler.router.ctx.search_the_net_urls = json.load(f)
|
||||||
plugins = []
|
plugins = []
|
||||||
if opts.use_bonjour:
|
if opts.use_bonjour:
|
||||||
plugins.append(BonJour())
|
plugins.append(BonJour(wait_for_stop=max(0, opts.shutdown_timeout - 0.2)))
|
||||||
self.loop = ServerLoop(
|
self.loop = ServerLoop(
|
||||||
create_http_handler(self.handler.dispatch),
|
create_http_handler(self.handler.dispatch),
|
||||||
opts=opts,
|
opts=opts,
|
||||||
|
@ -119,7 +119,7 @@ class LoopTest(BaseTest):
|
|||||||
from zeroconf import Zeroconf
|
from zeroconf import Zeroconf
|
||||||
else:
|
else:
|
||||||
from calibre.utils.Zeroconf import Zeroconf
|
from calibre.utils.Zeroconf import Zeroconf
|
||||||
b = BonJour()
|
b = BonJour(wait_for_stop=False)
|
||||||
with TestServer(lambda data:(data.path[0] + data.read()), plugins=(b,), shutdown_timeout=5) as server:
|
with TestServer(lambda data:(data.path[0] + data.read()), plugins=(b,), shutdown_timeout=5) as server:
|
||||||
self.assertTrue(b.started.wait(5), 'BonJour not started')
|
self.assertTrue(b.started.wait(5), 'BonJour not started')
|
||||||
self.ae(b.advertised_port, server.address[1])
|
self.ae(b.advertised_port, server.address[1])
|
||||||
|
@ -191,7 +191,7 @@ def publish(desc, service_type, port, properties=None, add_hostname=True, use_ip
|
|||||||
return service
|
return service
|
||||||
|
|
||||||
|
|
||||||
def unpublish(desc, service_type, port, properties=None, add_hostname=True):
|
def unpublish(desc, service_type, port, properties=None, add_hostname=True, wait_for_stop=True):
|
||||||
'''
|
'''
|
||||||
Unpublish a service.
|
Unpublish a service.
|
||||||
|
|
||||||
@ -201,13 +201,19 @@ def unpublish(desc, service_type, port, properties=None, add_hostname=True):
|
|||||||
service = create_service(desc, service_type, port, properties, add_hostname)
|
service = create_service(desc, service_type, port, properties, add_hostname)
|
||||||
server.unregister_service(service)
|
server.unregister_service(service)
|
||||||
if len(server.services) == 0:
|
if len(server.services) == 0:
|
||||||
stop_server()
|
stop_server(wait_for_stop=wait_for_stop)
|
||||||
|
|
||||||
|
|
||||||
def stop_server():
|
def stop_server(wait_for_stop=True):
|
||||||
global _server
|
global _server
|
||||||
if _server is not None:
|
srv = _server
|
||||||
try:
|
|
||||||
_server.close()
|
|
||||||
finally:
|
|
||||||
_server = None
|
_server = None
|
||||||
|
if srv is not None:
|
||||||
|
t = Thread(target=srv.close)
|
||||||
|
t.daemon = True
|
||||||
|
t.start()
|
||||||
|
if wait_for_stop:
|
||||||
|
if wait_for_stop is True:
|
||||||
|
t.join()
|
||||||
|
else:
|
||||||
|
t.join(wait_for_stop)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user