Ticket #12089: UpdateShutdownLock.patch.v0

File UpdateShutdownLock.patch.v0, 4.8 KB (added by Bill Meek <keemllib@…>, 5 years ago)
Line 
1diff --git a/mythtv/libs/libmythservicecontracts/services/mythServices.h b/mythtv/libs/libmythservicecontracts/services/mythServices.h
2index 373d444..d0d0018 100644
3--- a/mythtv/libs/libmythservicecontracts/services/mythServices.h
4+++ b/mythtv/libs/libmythservicecontracts/services/mythServices.h
5@@ -43,7 +43,7 @@
6 class SERVICE_PUBLIC MythServices : public Service  //, public QScriptable ???
7 {
8     Q_OBJECT
9-    Q_CLASSINFO( "version"    , "4.0" );
10+    Q_CLASSINFO( "version"    , "4.1" );
11     Q_CLASSINFO( "AddStorageGroupDir_Method",    "POST" )
12     Q_CLASSINFO( "RemoveStorageGroupDir_Method", "POST" )
13     Q_CLASSINFO( "PutSetting_Method",            "POST" )
14@@ -55,6 +55,7 @@ class SERVICE_PUBLIC MythServices : public Service  //, public QScriptable ???
15     Q_CLASSINFO( "CheckDatabase_Method",         "POST" )
16     Q_CLASSINFO( "ProfileSubmit_Method",         "POST" )
17     Q_CLASSINFO( "ProfileDelete_Method",         "POST" )
18+    Q_CLASSINFO( "UpdateShutdownLock_Method",    "POST" )
19 
20     public:
21 
22@@ -167,6 +168,8 @@ class SERVICE_PUBLIC MythServices : public Service  //, public QScriptable ???
23         virtual QString             ProfileUpdated      ( void ) = 0;
24 
25         virtual QString             ProfileText         ( void ) = 0;
26+
27+        virtual int                 UpdateShutdownLock  ( int   Increment ) = 0;
28 };
29 
30 #endif
31diff --git a/mythtv/programs/mythbackend/services/myth.cpp b/mythtv/programs/mythbackend/services/myth.cpp
32index 811e87c..9da02dd 100644
33--- a/mythtv/programs/mythbackend/services/myth.cpp
34+++ b/mythtv/programs/mythbackend/services/myth.cpp
35@@ -1055,3 +1055,77 @@ QString Myth::ProfileText()
36     return sProfileText;
37 }
38 
39+/////////////////////////////////////////////////////////////////////////////
40+// UpdateShutdownLock( int Increment )
41+// Input:  +/- value to add to the counter, typically 1 or -1, 0 is invalid.
42+// Output: -1 for all failures, otherwise the new value of the ShutdownLock.
43+/////////////////////////////////////////////////////////////////////////////
44+
45+int Myth::UpdateShutdownLock( int Increment )
46+{
47+    if ( Increment == 0 )
48+    {
49+        LOG(VB_GENERAL, LOG_ERR, "UpdateShutdownLock API, "
50+            "Error: Increment is missing or equals 0.");
51+        return -1;
52+    }
53+
54+    MSqlQuery query(MSqlQuery::InitCon());
55+
56+    int tries = 0;
57+    while (!query.exec("LOCK TABLE settings WRITE;") && tries < 5)
58+    {
59+        LOG(VB_GENERAL, LOG_INFO, "UpdateShutdownLock API, "
60+            "Waiting for LOCK on settings table");
61+        sleep(1);
62+        tries++;
63+    }
64+
65+    if (tries >= 5)
66+    {
67+        LOG(VB_GENERAL, LOG_ERR,
68+            "UpdateShutdownLock API, too long to LOCK settings");
69+        return -1;
70+    }
71+
72+    query.prepare("SELECT data FROM settings "
73+                  "WHERE value = 'MythShutdownLock' AND hostname IS NULL;");
74+    if (!query.exec() || !query.next())
75+    {
76+        MythDB::DBError("UpdateShutdownLock API, SELECT", query);
77+        return -1;
78+    }
79+
80+    int data = 1;
81+    if (query.size() < 1)
82+    {
83+        query.prepare("INSERT INTO settings (value, data) "
84+                      "VALUES ('MythShutdownLock', '1');");
85+        if (!query.exec())
86+        {
87+            MythDB::DBError("UpdateShutdownLock API, INSERT", query);
88+            return -1;
89+        }
90+    }
91+    else
92+    {
93+        data = query.value(0).toInt();
94+        data += Increment;
95+        if (data < 0)
96+            data = 0;
97+        query.prepare("UPDATE settings SET data = :DATA "
98+                      "WHERE value = 'MythShutdownLock' "
99+                      "AND hostname IS NULL;");
100+        query.bindValue(":DATA", QString("%1").arg(data));
101+        if (!query.exec())
102+        {
103+            MythDB::DBError("UpdateShutdownLock API, UPDATE", query);
104+            return -1;
105+        }
106+    }
107+
108+    if (!query.exec("UNLOCK TABLES;"))
109+        MythDB::DBError("UpdateShutdownLock API, UNLOCK settings", query);
110+
111+    return data;
112+}
113diff --git a/mythtv/programs/mythbackend/services/myth.h b/mythtv/programs/mythbackend/services/myth.h
114index cfd50aa..f889c71 100644
115--- a/mythtv/programs/mythbackend/services/myth.h
116+++ b/mythtv/programs/mythbackend/services/myth.h
117@@ -136,6 +136,8 @@ class Myth : public MythServices
118         QString             ProfileUpdated      ( void );
119 
120         QString             ProfileText         ( void );
121+
122+        int                 UpdateShutdownLock  ( int Increment );
123 };
124 
125 // --------------------------------------------------------------------------
126@@ -377,6 +379,13 @@ class ScriptableMyth : public QObject
127                 return m_obj.ProfileText();
128             )
129         }
130+
131+        int UpdateShutdownLock( int Increment )
132+        {
133+            SCRIPT_CATCH_EXCEPTION( false,
134+                return m_obj.UpdateShutdownLock( Increment );
135+            )
136+        }
137 };
138 
139 Q_SCRIPT_DECLARE_QMETAOBJECT_MYTHTV( ScriptableMyth, QObject*);