]> www.vanbest.org Git - motioneye-debian/commitdiff
motion is no longer restarted upon mount error
authorCalin Crisan <ccrisan@gmail.com>
Sat, 28 Jun 2014 13:13:23 +0000 (16:13 +0300)
committerCalin Crisan <ccrisan@gmail.com>
Sat, 28 Jun 2014 13:36:12 +0000 (16:36 +0300)
motioneye.py
src/handlers.py
src/motionctl.py
src/smbctl.py

index 94906a2f8c46a833624aa881f221276eb4ee2b81..ed6032f99a5a252de634f59894248599d0315890 100755 (executable)
@@ -279,16 +279,17 @@ def _start_motion():
     import config
     import motionctl
 
+    ioloop = tornado.ioloop.IOLoop.instance()
+    
     # add a motion running checker
     def checker():
-        ioloop = tornado.ioloop.IOLoop.instance()
         if ioloop._stopped:
             return
             
-        if not motionctl.running() and config.has_enabled_cameras():
+        if not motionctl.running() and motionctl.started() and config.has_enabled_cameras():
             try:
+                logging.error('motion not running, starting it')
                 motionctl.start()
-                logging.info('motion started')
             
             except Exception as e:
                 logging.error('failed to start motion: %(msg)s' % {
@@ -296,7 +297,10 @@ def _start_motion():
 
         ioloop.add_timeout(datetime.timedelta(seconds=settings.MOTION_CHECK_INTERVAL), checker)
     
-    checker()
+    if config.has_enabled_cameras():
+        motionctl.start()
+        
+    ioloop.add_timeout(datetime.timedelta(seconds=settings.MOTION_CHECK_INTERVAL), checker)
 
 
 def _start_cleanup():
@@ -321,11 +325,14 @@ if __name__ == '__main__':
     
     _configure_signals()
     _configure_logging()
-    
     if settings.SMB_SHARES:
-        smbctl.update_mounts()
+        stop, start = smbctl.update_mounts()
+        if start:
+            _start_motion()
 
-    _start_motion()
+    else:
+        _start_motion()
+        
     _start_cleanup()
     
     if settings.THUMBNAILER_INTERVAL:
index fea56a47aa6ed35dd0e058dcadec2b3de5330a7a..d391ee5beb5b3df193fe608f5c6b9b1a4c3535f3 100644 (file)
@@ -293,12 +293,19 @@ class ConfigHandler(BaseHandler):
         
         def finish():
             if restart[0]:
+                logging.debug('motion needs to be restarted')
+                
+                motionctl.stop()
+                
                 if settings.SMB_SHARES:
                     logging.debug('updating SMB mounts')
-                    smbctl.update_mounts()
+                    stop, start = smbctl.update_mounts()  # @UnusedVariable
 
-                logging.debug('motion needs to be restarted')
-                motionctl.restart()
+                    if start:
+                        motionctl.start()
+                
+                else:
+                    motionctl.start()
 
             self.finish({'reload': reload, 'error': error[0]})
         
@@ -500,10 +507,16 @@ class ConfigHandler(BaseHandler):
         camera_config['@id'] = camera_id
 
         if proto == 'v4l2':
+            motionctl.stop()
+            
             if settings.SMB_SHARES:
-                smbctl.update_mounts()
+                stop, start = smbctl.update_mounts()  # @UnusedVariable
 
-            motionctl.restart()
+                if start:
+                    motionctl.start()
+            
+            else:
+                motionctl.start()
             
             ui_config = config.camera_dict_to_ui(camera_config)
             
@@ -529,7 +542,9 @@ class ConfigHandler(BaseHandler):
         config.rem_camera(camera_id)
         
         if local:
-            motionctl.restart()
+            motionctl.stop()
+            if config.has_enabled_cameras():
+                motionctl.start()
             
         self.finish_json()
 
index 004e8557ba2e6e2f66b7a59bf0012f6d191fcb9c..176a76ba7927cf74583fce1110ee6b9439e6e6e7 100644 (file)
@@ -16,6 +16,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 
 import errno
+import logging
 import os.path
 import re
 import signal
@@ -26,6 +27,9 @@ import config
 import settings
 
 
+_started = False
+
+
 def find_motion():
     try:
         binary = subprocess.check_output('which motion', shell=True).strip()
@@ -46,8 +50,14 @@ def find_motion():
 
 
 def start():
+    global _started
+    
+    _started = True
+    
     if running():
-        raise Exception('motion is already running')
+        return
+    
+    logging.debug('starting motion')
  
     program = find_motion()
     if not program:
@@ -84,8 +94,14 @@ def start():
 def stop():
     import mjpgclient
     
+    global _started
+    
+    _started = False
+    
     if not running():
-        raise Exception('motion is not running')
+        return
+    
+    logging.debug('stopping motion')
 
     mjpgclient.close_all()
     
@@ -121,7 +137,6 @@ def running():
     if pid is None:
         return False
     
-    
     try:
         os.waitpid(pid, os.WNOHANG)
         os.kill(pid, 0)
@@ -136,12 +151,8 @@ def running():
     return False
 
 
-def restart():
-    if running():
-        stop()
-    
-    if config.has_enabled_cameras():
-        start()
+def started():
+    return _started
 
 
 def _get_pid():
index 64ebcdd816cb01582dc12953aabf98b8b188105a..43a978745862a1fd5330cbb33c9ac4df8638e2d7 100644 (file)
@@ -22,11 +22,12 @@ import re
 import subprocess
 import time
 
+from tornado import ioloop
+
 import config
+import motionctl
 import settings
 
-from tornado import ioloop
-
 
 def find_mount_cifs():
     try:
@@ -127,7 +128,7 @@ def mount(server, share, username, password):
     except subprocess.CalledProcessError:
         logging.error('failed to mount smb share "//%s/%s" at "%s"' % (server, share, mount_point))
         
-        return False
+        return None
     
     # test to see if mount point is writable
     try:
@@ -139,7 +140,7 @@ def mount(server, share, username, password):
     except:
         logging.error('directory at "%s" is not writable' % mount_point)
         
-        return False
+        return None
     
     return mount_point
 
@@ -157,7 +158,7 @@ def umount(server, share, username):
         return False
     
     try:
-        os.remove(mount_point)
+        os.rmdir(mount_point)
     
     except Exception as e:
         logging.error('failed to remove smb mount point "%s": %s' % (mount_point, e))
@@ -173,18 +174,25 @@ def update_mounts():
     mounts = list_mounts()
     mounts = dict(((m['server'], m['share'], m['username'] or ''), False) for m in mounts)
     
+    should_stop = False # indicates that motion should be stopped immediately
+    should_start = True # indicates that motion can be started afterwards
     for network_share in network_shares:
         key = (network_share['server'], network_share['share'], network_share['username'] or '')
         if key in mounts: # found
             mounts[key] = True
         
         else: # needs to be mounted
-            mount(network_share['server'], network_share['share'], network_share['username'], network_share['password'])
+            should_stop = True
+            if not mount(network_share['server'], network_share['share'], network_share['username'], network_share['password']):
+                should_start = False
     
     # unmount the no longer necessary mounts
     for (network_share['server'], network_share['share'], network_share['username']), required in mounts.items():
         if not required:
             umount(network_share['server'], network_share['share'], network_share['username'])
+            should_stop = True
+    
+    return (should_stop, should_start)
 
 
 def umount_all():
@@ -195,8 +203,13 @@ def umount_all():
 def _check_mounts():
     logging.debug('checking SMB mounts...')
     
-    update_mounts()
+    stop, start = update_mounts()
+    if stop:
+        motionctl.stop()
 
+    if start:
+        motionctl.start()
+        
     io_loop = ioloop.IOLoop.instance()
     io_loop.add_timeout(datetime.timedelta(seconds=settings.MOUNT_CHECK_INTERVAL), _check_mounts)