From: Calin Crisan Date: Sun, 31 May 2015 12:44:18 +0000 (+0300) Subject: added MJPG_CLIENT_IDLE_TIMEOUT setting X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=f24ab5f940d86c8e858c38e14d2ce9c822bd65ea;p=motioneye-debian added MJPG_CLIENT_IDLE_TIMEOUT setting --- diff --git a/motioneye.py b/motioneye.py index 28dbf8a..13a4791 100755 --- a/motioneye.py +++ b/motioneye.py @@ -65,6 +65,7 @@ def _configure_settings(): set_default_setting('THUMBNAILER_INTERVAL', 60) set_default_setting('REMOTE_REQUEST_TIMEOUT', 10) set_default_setting('MJPG_CLIENT_TIMEOUT', 10) + set_default_setting('MJPG_CLIENT_IDLE_TIMEOUT', 10) set_default_setting('PICTURE_CACHE_SIZE', 8) set_default_setting('PICTURE_CACHE_LIFETIME', 60) set_default_setting('SMB_SHARES', False) @@ -341,7 +342,7 @@ def _start_motion(): if ioloop._stopped: return - if not motionctl.running() and motionctl.started() and config.has_local_enabled_cameras(): + if not motionctl.running() and motionctl.started() and config.get_enabled_local_motion_cameras(): try: logging.error('motion not running, starting it') motionctl.start() diff --git a/settings_default.py b/settings_default.py index 0c14b80..3dc11d6 100644 --- a/settings_default.py +++ b/settings_default.py @@ -47,12 +47,15 @@ CLEANUP_INTERVAL = 43200 # interval in seconds at which the thumbnail mechanism runs (set to 0 to disable) THUMBNAILER_INTERVAL = 60 -# timeout in seconds to wait for responses when contacting a remote server +# timeout in seconds when waiting for response from a remote motionEye server REMOTE_REQUEST_TIMEOUT = 10 -# timeout in seconds to wait for an access to a mjpg client before removing it +# timeout in seconds when waiting for mjpg data from the motion daemon MJPG_CLIENT_TIMEOUT = 10 +# timeout in seconds after which an idle mjpg client is removed (set to 0 to disable) +MJPG_CLIENT_IDLE_TIMEOUT = 10 + # the maximal number of entries per camera in the current pictures cache PICTURE_CACHE_SIZE = 8 diff --git a/src/config.py b/src/config.py index af8d1f2..bbfc868 100644 --- a/src/config.py +++ b/src/config.py @@ -205,13 +205,13 @@ def get_camera_ids(): return filtered_camera_ids -def has_local_enabled_cameras(): +def get_enabled_local_motion_cameras(): if not get_main().get('@enabled'): - return False + return [] camera_ids = get_camera_ids() cameras = [get_camera(camera_id) for camera_id in camera_ids] - return bool([c for c in cameras if c.get('@enabled') and utils.local_motion_camera(c)]) + return [c for c in cameras if c.get('@enabled') and utils.local_motion_camera(c)] def get_network_shares(): diff --git a/src/mjpgclient.py b/src/mjpgclient.py index b103bd3..d576d81 100644 --- a/src/mjpgclient.py +++ b/src/mjpgclient.py @@ -72,8 +72,8 @@ class MjpgClient(iostream.IOStream): logging.error('connection problem detected for mjpg client for camera %(camera_id)s on port %(port)s' % { 'port': self._port, 'camera_id': self._camera_id}) - motionctl.stop() # this will close all the mjpg clients - motionctl.start() + motionctl.stop(invalidate=True) # this will close all the mjpg clients + motionctl.start(deferred=True) MjpgClient.last_erroneous_close_time = now @@ -231,8 +231,8 @@ def _garbage_collector(): logging.error('mjpg client timed out receiving data for camera %(camera_id)s on port %(port)s' % { 'camera_id': camera_id, 'port': port}) - motionctl.stop() # this will close all the mjpg clients - motionctl.start() + motionctl.stop(invalidate=True) # this will close all the mjpg clients + motionctl.start(deferred=True) break @@ -244,9 +244,9 @@ def _garbage_collector(): delta = now - last_access delta = delta.days * 86400 + delta.seconds - if delta > settings.MJPG_CLIENT_TIMEOUT: - logging.debug('mjpg client for camera %(camera_id)s on port %(port)s timed out' % { - 'camera_id': camera_id, 'port': port}) + if settings.MJPG_CLIENT_IDLE_TIMEOUT and delta > settings.MJPG_CLIENT_IDLE_TIMEOUT: + logging.debug('mjpg client for camera %(camera_id)s on port %(port)s has been idle for %(timeout)s seconds, removing it' % { + 'camera_id': camera_id, 'port': port, 'timeout': settings.MJPG_CLIENT_IDLE_TIMEOUT}) client.close() @@ -286,9 +286,16 @@ def get_jpg(camera_id): return MjpgClient.last_jpgs.get(camera_id) -def close_all(): +def close_all(invalidate=False): for client in MjpgClient.clients.values(): client.close() + + if invalidate: + MjpgClient.clients = {} + MjpgClient.last_jpgs = {} + MjpgClient.last_jpg_moment = {} + MjpgClient.last_access = {} + MjpgClient.last_erroneous_close_time = 0 # schedule the garbage collector diff --git a/src/motionctl.py b/src/motionctl.py index 1783fda..df27ae6 100644 --- a/src/motionctl.py +++ b/src/motionctl.py @@ -24,8 +24,10 @@ import subprocess import time from tornado.httpclient import HTTPClient, AsyncHTTPClient, HTTPRequest +from tornado.ioloop import IOLoop import config +import mjpgclient import powerctl import settings import utils @@ -80,12 +82,16 @@ def _disable_initial_motion_detection(): set_motion_detection(camera_id, False) -def start(): +def start(deferred=False): + if deferred: + return IOLoop.instance().add_callback(start, deferred=False) + global _started _started = True - if running() or not config.has_local_enabled_cameras(): + enabled_local_motion_cameras = config.get_enabled_local_motion_cameras() + if running() or not enabled_local_motion_cameras: return logging.debug('starting motion') @@ -131,11 +137,15 @@ def start(): f.write(str(pid) + '\n') _disable_initial_motion_detection() + + # if mjpg client idle timeout is disabled, create mjpg clients for all cameras by default + if not settings.MJPG_CLIENT_IDLE_TIMEOUT: + logging.debug('creating default mjpg clients for local cameras') + for camera in enabled_local_motion_cameras: + mjpgclient.get_jpg(camera['@id']) -def stop(): - import mjpgclient - +def stop(invalidate=False): global _started _started = False @@ -145,7 +155,7 @@ def stop(): logging.debug('stopping motion') - mjpgclient.close_all() + mjpgclient.close_all(invalidate=invalidate) pid = _get_pid() if pid is not None: