summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-system/myth_mtc.py
blob: db6b42a23ac85a2e72f52a0d17b98c9f122e19f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python2
import sys, subprocess
import optparse
import re
import socket
import os
import datetime,time
import shlex
sys.dont_write_bytecode = True
import idle

try:
    from MythTV import MythBE
    mythtv = MythBE()
except:
    mythtv = None

#print mythtv.db.getSetting( 'Theme', socket.gethostname())

def get_timestamp():
    now = datetime.datetime.now()
#date = "%s-%s-%s" %(now.year, now.month, now.day)
    date = (now.strftime('%Y-%m-%d %H:%M')) 
    return date

def optimize():
    try:
        cursor = mythtv.db.cursor()
        cursor.execute("SHOW tables")
        result = cursor.fetchall()
    except:
        print "    Problem getting tables from database"
	return
    ops=["REPAIR","OPTIMIZE","ANALYZE"]
    for row in result:
        ctable=row[0]
        for op in ops:
            print "    %s %s" %(op,ctable)
            cmd= "%s  table %s" %(op,ctable)
            cursor.execute(cmd)

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 run_stuff():
    print "\n%s" %get_timestamp()
    if idle.main():
        print "\n#######################################"
        print "\n%s Running Optimize" %(get_timestamp())
        optimize()

        print "\n#######################################"
        print "\n%s Running Backup" %(get_timestamp())
        os.system('/usr/LH/bin/lh_system_backup_job')

        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()
    if  ctin:
        print "\n%s Waiting 10 minutes before trying again." %(get_timestamp())
        time.sleep(600)
        
    current_time=time.time()
    if (current_time - starttime) > 3000 :
        ctin = False
        print "\n%s Time Exceeded 50 minutes. Quitting.)" %(get_timestamp())
        exit(1)