Ticket #12089: UpdateShutdownLock.patch.v1

File UpdateShutdownLock.patch.v1, 4.9 KB (added by Bill Meek <keemllib@…>, 5 years ago)

Unlock settings on error, thanks to ikke-t on the MAF issues list

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..8804306 100644
33--- a/mythtv/programs/mythbackend/services/myth.cpp
34+++ b/mythtv/programs/mythbackend/services/myth.cpp
35@@ -1055,3 +1055,78 @@ QString Myth::ProfileText()
36     return sProfileText;
37 }
38 
39+/////////////////////////////////////////////////////////////////////////////
40+// UpdateShutdownLock: (logic stolen from mythshutdown --lock)
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+    bool err = false;
48+    int data = 1;
49+
50+    if (!Increment)
51+    {
52+        LOG(VB_GENERAL, LOG_ERR, "Error: Increment is missing or equals 0.");
53+        return -1;
54+    }
55+
56+    MSqlQuery query(MSqlQuery::InitCon());
57+
58+    int tries = 0;
59+    while (!query.exec("LOCK TABLE settings WRITE;") && tries < 5)
60+    {
61+        LOG(VB_GENERAL, LOG_INFO, "Waiting for LOCK on settings table");
62+        sleep(1);
63+        tries++;
64+    }
65+
66+    if (tries >= 5)
67+    {
68+        LOG(VB_GENERAL, LOG_ERR, "Took 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+        err = true;
78+    }
79+    else
80+    {
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+                err = true;
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+                err = true;
105+            }
106+        }
107+    }
108+
109+    if (!query.exec("UNLOCK TABLES;"))
110+        MythDB::DBError("UpdateShutdownLock API, UNLOCK settings", query);
111+
112+    return (err ? -1 : data);
113+}
114diff --git a/mythtv/programs/mythbackend/services/myth.h b/mythtv/programs/mythbackend/services/myth.h
115index cfd50aa..f889c71 100644
116--- a/mythtv/programs/mythbackend/services/myth.h
117+++ b/mythtv/programs/mythbackend/services/myth.h
118@@ -136,6 +136,8 @@ class Myth : public MythServices
119         QString             ProfileUpdated      ( void );
120 
121         QString             ProfileText         ( void );
122+
123+        int                 UpdateShutdownLock  ( int Increment );
124 };
125 
126 // --------------------------------------------------------------------------
127@@ -377,6 +379,13 @@ class ScriptableMyth : public QObject
128                 return m_obj.ProfileText();
129             )
130         }
131+
132+        int UpdateShutdownLock( int Increment )
133+        {
134+            SCRIPT_CATCH_EXCEPTION( false,
135+                return m_obj.UpdateShutdownLock( Increment );
136+            )
137+        }
138 };
139 
140 Q_SCRIPT_DECLARE_QMETAOBJECT_MYTHTV( ScriptableMyth, QObject*);