From b97e90ff739fa2f7d27ed5c62ae8436c6ce2bb8d Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Thu, 12 Dec 2013 14:26:55 -0600
Subject: LinHES-system: change myth_mtc.py to run in cron hourly to run more
 reliably after system is down or is busy.

---
 abs/core/LinHES-system/PKGBUILD              | 17 +++++++------
 abs/core/LinHES-system/lh_system_backup_job  | 10 +++-----
 abs/core/LinHES-system/lh_system_host_update |  9 +++----
 abs/core/LinHES-system/myth_mtc.cron         | 23 +++++++++++++++++
 abs/core/LinHES-system/myth_mtc.py           | 37 ++++++++++++++++++----------
 abs/core/LinHES-system/myth_mtc.sh           | 14 -----------
 abs/core/LinHES-system/myth_status.py        |  4 +--
 abs/core/LinHES-system/system.install        | 12 +++------
 8 files changed, 68 insertions(+), 58 deletions(-)
 create mode 100755 abs/core/LinHES-system/myth_mtc.cron
 delete mode 100755 abs/core/LinHES-system/myth_mtc.sh

diff --git a/abs/core/LinHES-system/PKGBUILD b/abs/core/LinHES-system/PKGBUILD
index e03cb7d..b0b8ba7 100755
--- a/abs/core/LinHES-system/PKGBUILD
+++ b/abs/core/LinHES-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=LinHES-system
 pkgver=8.1
-pkgrel=9
+pkgrel=10
 arch=('i686' 'x86_64')
 install=system.install
 pkgdesc="Everything that makes LinHES an automated system"
@@ -11,7 +11,7 @@ depends=('linhes-sounds' 'xdotool' 'tilda' 'keylaunch'
         'ethtool' 'gnu-netcat' 'normalize' 'ttf-overlock'
         'handbrake-cli' 'mkvtoolnix' 'mplayer')
 backup=('etc/modprobe.d/alsa-base.conf')
-binfiles="LinHES-start optimize_mythdb.py myth_mtc.py myth_mtc.sh
+binfiles="LinHES-start optimize_mythdb.py myth_mtc.py
  load-modules-mythvantage.sh unclutter-toggle.sh tvterm.sh
  mythfrontend-start set_windowmanager.sh myth_status.py myth_status.sh
  install_supplemental_service.sh get_airplay_key importfiles.sh
@@ -28,7 +28,7 @@ binfiles="LinHES-start optimize_mythdb.py myth_mtc.py myth_mtc.sh
  mythwelcome-config.py mythwelcome-set-alarm.sh mythwelcome-test-wakeup.sh"
 
 source=(LinHES-session LinHES-profile.sh $binfiles
-    alsa-base cacheclean.cron xfs_defrag.cron
+    alsa-base cacheclean.cron myth_mtc.cron xfs_defrag.cron
     readme_is_xml readme_light add_storage.readme
     system-sudo.rules msg.cfg)
 
@@ -62,6 +62,7 @@ package() {
     #install -m755 -D $srcdir/smolt.cron $pkgdir/etc/cron.weekly/smolt.cron
     install -m755 -D $srcdir/cacheclean.cron $pkgdir/etc/cron.weekly/cacheclean
     install -m755 -D $srcdir/xfs_defrag.cron $pkgdir/etc/cron.weekly/xfs_defrag
+    install -m755 -D $srcdir/myth_mtc.cron $pkgdir/etc/cron.hourly/myth_mtc
 
     #sudo rules
     mkdir -p $pkgdir/etc/sudoers.d/
@@ -78,22 +79,21 @@ md5sums=('7f5624a2d965a4cd3ed6a37ebee13fd9'
          'de32a1c50101265dd7f6ca5037f7a26a'
          '301884fb60521627fffd1160b2cf5181'
          '76b2637cac0452b3acdbeeb4e8a5474b'
-         '79a63270794ef7de244af10dad51330a'
-         '6f5b757524d905b5d2420519a88edc93'
+         '22807bd1e37d2a07bc0bd3f2a9fd2bb4'
          'dc3eef2a624754e16805d72bbe488b67'
          '617af86b901538817ebdcaf646248dc5'
          '542e670e78d117657f93141e9689f54d'
          '8da6a7f1703a002f84e66629e847d8a6'
          'bb72ab230c7a71706285bd0f31a4fb1f'
-         '42981e30e016c5f00b841b8490a3a489'
+         'f8683caddf74dca1ea5cc3db4d748764'
          '962a3e9eaba2d1466251b7ab0956705d'
          '1758aed160de64abfafb28a3a8f3390e'
          '33fbebbd546672cedd3c5e7350ab414e'
          'c773d8caacba8fbd4968e8afe5137bc6'
          '3edef50a49a47694bf8add39cc160add'
-         'aa6010065f5ec2afe1d2bee4cf81dec7'
+         'b376c30f95892b8682fad84f81685f75'
          'bc69a520add58ede9b060c73e67ace13'
-         '26fdd26e945f0c187f9fdcf98a7a5bef'
+         '2e5e1b3187f75154366bffe806ff8ae9'
          '47e093e8cfe4b5b96602358e1f540832'
          '4d6a6e88b519caf917ebe6c85c32c45b'
          '2c005d95312018bef80092136f80f254'
@@ -129,6 +129,7 @@ md5sums=('7f5624a2d965a4cd3ed6a37ebee13fd9'
          '410795ef9039e4c6c0484e706ecfd567'
          'eb879fee9603a05d5420d4ce8ed9e450'
          '02c810c2f47b7c4495fdacaf54189473'
+         'fae4162988de9525ba3f9341e17f9c52'
          '474d5c6883fcfffae4f199aceb3b1356'
          '36bdfa8d877cea20ca3870faf08e08d5'
          '59d0602bac7e06f11abba1894acc8bb0'
diff --git a/abs/core/LinHES-system/lh_system_backup_job b/abs/core/LinHES-system/lh_system_backup_job
index 7d05be5..3ac9de0 100644
--- a/abs/core/LinHES-system/lh_system_backup_job
+++ b/abs/core/LinHES-system/lh_system_backup_job
@@ -33,8 +33,7 @@ function backup_status_check(){
 
 function backup(){
 
-    echo "#######################################"
-    echo "Starting  backup	"
+    echo "Starting Backup"
     mkdir -p $BACKUPDIR/$DATE
 
     #backup database
@@ -117,7 +116,6 @@ function backup(){
     echo
     echo "Created file:"
     echo "    $BACKUPDIR/backup.$DATE.tgz"
-    echo "########################################"
 }
 
 function update_backup_status(){
@@ -131,7 +129,7 @@ function update_backup_status(){
         COMPLETE_MSG="Last backup FAILED `date '+%D %-I:%M %p'`"
     fi
     echo "Updating menu with:"
-    echo "  $COMPLETE_MSG"
+    echo "    $COMPLETE_MSG"
     xmlfile="/usr/share/mythtv/themes/defaultmenu/mythbackup.xml"
 
     grep  -q "<description>" $xmlfile >/dev/null
@@ -198,10 +196,10 @@ function remote_backup(){
                 echo "    Copying system backups to $SECBACKUP"
                 rsync -au --delete $BACKUPDIR $SECBACKUP
             else
-                echo "*    $SECBACKUPDISK isn't mounted."
+                echo "    $SECBACKUPDISK isn't mounted."
             fi
         else
-            echo "*    Link $SECBACKUPLINK doesn't exist."
+            echo "    Link $SECBACKUPLINK doesn't exist."
         fi
     fi
 }
diff --git a/abs/core/LinHES-system/lh_system_host_update b/abs/core/LinHES-system/lh_system_host_update
index 56a22e5..ca6aeff 100644
--- a/abs/core/LinHES-system/lh_system_host_update
+++ b/abs/core/LinHES-system/lh_system_host_update
@@ -2,8 +2,8 @@
 # Process that call this script
 # func  update 
 # supplemental web
-echo "#####################################################"
-echo "Starting update"
+
+echo "Starting Update"
 MYTH_RUN_STATUS="1"
 . /etc/profile
 
@@ -17,7 +17,7 @@ fi
 
 case $Hostupdateplan in
 	"Myth_only")
-		echo "Updating myth packages"
+		echo "Updating only myth packages"
 		postfix=`cat $INSTALL_DIR/usr/local/share/mythtv/.releasetype`
 		for mythl in mythphone mytharchive mythbrowser mythnews mythgame mythflix mythweather mythcontrols mythgallery mythmovietime mythmusic mythsmolt mythvideo mythweb mythtv
 		do
@@ -52,6 +52,3 @@ case $Hostupdateplan in
         echo "Unknown  $Hostupdateplan, skipping updates"
         ;;
 esac
-
-
-echo "##################################################################################"
diff --git a/abs/core/LinHES-system/myth_mtc.cron b/abs/core/LinHES-system/myth_mtc.cron
new file mode 100755
index 0000000..4f63dec
--- /dev/null
+++ b/abs/core/LinHES-system/myth_mtc.cron
@@ -0,0 +1,23 @@
+#!/bin/bash
+MYTH_RUN_STATUS=1
+. /etc/profile
+
+date=`date +%Y-%m-%d`
+timestamp=`date +'%Y-%m-%d %H:%M'`
+logFile="/var/log/$date/myth_mtc.log"
+
+if [ ! -f $logFile ]; then
+    touch $logFile
+fi
+
+#check logfile for Finished and if not run myth_mtc.py
+if ! grep -q "Finished" $logFile
+then
+    MYTHCONFDIR=/usr/share/mythtv unbuffer myth_mtc.py >> $logFile 2>&1
+    if [ $? = 0 ]
+    then
+        echo "Finished $timestamp" >> $logFile
+    else
+        echo "Time Exceeded $timestamp" >> $logFile
+    fi
+fi
diff --git a/abs/core/LinHES-system/myth_mtc.py b/abs/core/LinHES-system/myth_mtc.py
index 2c1fd14..7847313 100755
--- a/abs/core/LinHES-system/myth_mtc.py
+++ b/abs/core/LinHES-system/myth_mtc.py
@@ -5,6 +5,7 @@ import re
 import socket
 import os
 import datetime,time
+import shlex
 
 try:
     from MythTV import MythBE
@@ -34,7 +35,7 @@ def optimize():
     for row in result:
         ctable=row[0]
         for op in ops:
-            print op,ctable
+            print "    %s %s" %(op,ctable)
             cmd= "%s  table %s" %(op,ctable)
             cursor.execute(cmd)
 
@@ -128,10 +129,18 @@ def mfd_check():
 
     return True
 
+def bail_if_another_is_running():
+    cmd = shlex.split("pgrep -u {} -f {}".format(os.getuid(), __file__))
+    pids = subprocess.check_output(cmd).strip().split('\n')
+    if len(pids) > 1:
+        pids.remove("{}".format(os.getpid()))
+        print "Exiting! Found {} is already running (pids): {}".format(
+            __file__, " ".join(pids))
+        raise SystemExit(1)
 
 
 def idle_check():
-    print "%s  Checking idle" %(get_timestamp())
+    print "\n%s Checking Idle" %(get_timestamp())
     if  (   upcoming_check() and schemalock_check() and job_check()  and in_use() and  mfd_check()  ):
         idle=True
         print "\n%s Myth is idle" %(get_timestamp())
@@ -142,35 +151,37 @@ def idle_check():
 
 def run_stuff():
     if idle_check():
-        print "\n%sRunning optimize" %(get_timestamp())
+        print "\n#######################################"
+        print "\n%s Running Optimize" %(get_timestamp())
         optimize()
 
-	print "\n%s Running backup" %(get_timestamp())
+        print "\n#######################################"
+        print "\n%s Running Backup" %(get_timestamp())
         os.system('/usr/LH/bin/lh_system_backup_job')
 
-	print "\n%s Running system update" %(get_timestamp())
-	os.system('/usr/LH/bin/lh_system_host_update')
+        print "\n#######################################"
+        print "\n%s Running System Update" %(get_timestamp())
+        os.system('/usr/LH/bin/lh_system_host_update')
+
+        print "\n#######################################"
         continue_loop=False
     else:
         continue_loop=True
     return continue_loop
 
 #---------------------------------
+bail_if_another_is_running()
 starttime=time.time()
 ctin=True
 while ctin:
     ctin=run_stuff()
-    print "\n"
     if  ctin:
-        print "%s Waiting 10 minutes before trying again" %(get_timestamp())
+        print "\n%s Waiting 10 minutes before trying again." %(get_timestamp())
         time.sleep(600)
         
     current_time=time.time()
-    if (current_time - starttime) > 10800 :
+    if (current_time - starttime) > 3000 :
         ctin = False
-        print "time exceeded (3 hours)"
+        print "\n%s Time Exceeded 50 minutes. Quitting.)" %(get_timestamp())
         exit(1)
 
-
-
-
diff --git a/abs/core/LinHES-system/myth_mtc.sh b/abs/core/LinHES-system/myth_mtc.sh
deleted file mode 100755
index 7899daa..0000000
--- a/abs/core/LinHES-system/myth_mtc.sh
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/bash
-MYTH_RUN_STATUS=1
-. /etc/profile
-
-date=`date +%Y-%m-%d`
-logFile="/var/log/$date/myth_mtc.log"
-
-MYTHCONFDIR=/usr/share/mythtv myth_mtc.py > $logFile 2>&1
-if [ $? = 0 ]
-then
-    echo "Finished $date" >> $logFile
-else
-    echo "Time Exceede $date" >> $logFile
-fi
diff --git a/abs/core/LinHES-system/myth_status.py b/abs/core/LinHES-system/myth_status.py
index df8c84a..40dd3b3 100644
--- a/abs/core/LinHES-system/myth_status.py
+++ b/abs/core/LinHES-system/myth_status.py
@@ -92,7 +92,7 @@ def print_alerts():
             out_alert += out_line
                 
         print "System Alerts:"
-        print "---------------"
+        print "--------------"
         if len(out_alert) > 0:
             print out_alert 
             print "    Go to http://%s and click Health & Maintenance for more information." %gethostname()
@@ -239,7 +239,7 @@ class tuner_recording_status:
     def print_conflict_list(self):
         print ""
         print "Recording Conflicts:"
-        print "----------------------"
+        print "--------------------"
         if len(self.get_conflict_list()) > 0:
             for i in self.get_conflict_list():
                 print "    %s -  %-50s " %(i[0],i[1])
diff --git a/abs/core/LinHES-system/system.install b/abs/core/LinHES-system/system.install
index ddad9e3..880ea02 100644
--- a/abs/core/LinHES-system/system.install
+++ b/abs/core/LinHES-system/system.install
@@ -8,17 +8,11 @@ pre_install() {
 
 # arg 1:  the new package version
 post_install() {
-    MVDIR=/usr/LH/bin
-    #add myth_mtc to cron list
-    if [ ! -e /etc/cron.daily/myth_mtc ]
-    then
-        ln -s  $MVDIR/myth_mtc.sh  /etc/cron.daily/myth_mtc
+    #remove daily myth_mtc link
+    if [ -h /etc/cron.daily/myth_mtc ]; then
+        rm -f /etc/cron.daily/myth_mtc
     fi
 
-    # Add db entry for misc status in MythWeb
-    # not needed in R8 because it's in the DB, may be removed
-    #/usr/LH/bin/misc_status_config.py
-
     sv restart msg_daemon
 }
 
-- 
cgit v0.12