]> www.vanbest.org Git - motioneye-debian/commitdiff
working schedule checker is now implemented using coroutines and
authorCalin Crisan <ccrisan@gmail.com>
Sun, 18 Oct 2015 13:43:25 +0000 (16:43 +0300)
committerCalin Crisan <ccrisan@gmail.com>
Sun, 18 Oct 2015 13:43:25 +0000 (16:43 +0300)
AsyncHTTPClient, to prevent memory leaks by using a regular HTTPClient

motioneye/motionctl.py
motioneye/wsswitch.py

index bd79cbc9d867c3dc3be2429e5ddb2424ba4ca4aa..3385e905b1b87b2f23e43c1bc235e06b7e7a5fd4 100644 (file)
@@ -23,7 +23,8 @@ import signal
 import subprocess
 import time
 
-from tornado.httpclient import HTTPClient, AsyncHTTPClient, HTTPRequest
+from tornado import gen
+from tornado.httpclient import AsyncHTTPClient, HTTPRequest
 from tornado.ioloop import IOLoop
 
 import config
@@ -212,34 +213,36 @@ def started():
     return _started
 
 
+@gen.coroutine
 def get_motion_detection(camera_id):
     thread_id = camera_id_to_thread_id(camera_id)
     if thread_id is None:
-        return logging.error('could not find thread id for camera with id %s' % camera_id)
-    
+        logging.error('could not find thread id for camera with id %s' % camera_id)
+        return
+
     url = 'http://127.0.0.1:7999/%(id)s/detection/status' % {'id': thread_id}
     
     request = HTTPRequest(url, connect_timeout=5, request_timeout=5)
-    http_client = HTTPClient()
+    http_client = AsyncHTTPClient()
     try:
-        response = http_client.fetch(request)
+        response = yield http_client.fetch(request)
         if response.error:
-            raise response.error 
-    
+            raise response.error
+
     except Exception as e:
         logging.error('failed to get motion detection status for camera with id %(id)s: %(msg)s' % {
                 'id': camera_id,
                 'msg': unicode(e)})
+        return
 
-        return None
-    
     enabled = bool(response.body.lower().count('active'))
     
     logging.debug('motion detection is %(what)s for camera with id %(id)s' % {
             'what': ['disabled', 'enabled'][enabled],
             'id': camera_id})
-    
-    return enabled
+
+    raise gen.Return(enabled)
 
 
 def set_motion_detection(camera_id, enabled):
index be65c2749ce8b6598156c7e08c13a831dc8d7b4a..59490d0f66228cbb18ac193418c1744ddabe4d7a 100644 (file)
@@ -17,7 +17,8 @@
 
 import datetime
 import logging
-import tornado
+
+from tornado import ioloop, gen
 
 import config
 import motionctl
@@ -25,8 +26,8 @@ import utils
 
 
 def start():
-    ioloop = tornado.ioloop.IOLoop.instance()
-    ioloop.add_timeout(datetime.timedelta(seconds=10), _check_ws)
+    io_loop = ioloop.IOLoop.instance()
+    io_loop.add_timeout(datetime.timedelta(seconds=1), _check_ws)
 
 
 def _during_working_schedule(now, working_schedule):
@@ -69,14 +70,15 @@ def _during_working_schedule(now, working_schedule):
     return True
 
 
+@gen.coroutine
 def _check_ws():
     # schedule the next call
-    ioloop = tornado.ioloop.IOLoop.instance()
-    ioloop.add_timeout(datetime.timedelta(seconds=10), _check_ws)
+    io_loop = ioloop.IOLoop.instance()
+    io_loop.add_timeout(datetime.timedelta(seconds=10), _check_ws)
 
     if not motionctl.running():
         return
-
+    
     now = datetime.datetime.now()
     for camera_id in config.get_camera_ids():
         camera_config = config.get_camera(camera_id)
@@ -96,7 +98,7 @@ def _check_ws():
         now_during = _during_working_schedule(now, working_schedule)
         must_be_enabled = (now_during and working_schedule_type == 'during') or (not now_during and working_schedule_type == 'outside')
         
-        currently_enabled = motionctl.get_motion_detection(camera_id)
+        currently_enabled = yield motionctl.get_motion_detection(camera_id)
         if currently_enabled is None: # could not detect current status
             logging.warn('skipping motion detection status update for camera with id %(id)s' % {'id': camera_id})
             continue