/
backendhousekeeper.cpp
579 lines (502 loc) · 19.1 KB
/
backendhousekeeper.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// POSIX headers
#include <unistd.h>
#include <sys/types.h>
// ANSI C headers
#include <cstdlib>
// Qt headers
#include <QStringList>
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
// MythTV headers
#include "backendhousekeeper.h"
#include "mythdb.h"
#include "mythdirs.h"
#include "jobqueue.h"
#include "exitcodes.h"
#include "mythsystemlegacy.h"
#include "mythversion.h"
#include "mythcoreutil.h"
#include "programtypes.h"
#include "recordingtypes.h"
#include "mythcorecontext.h"
#include "mythdownloadmanager.h"
bool LogCleanerTask::DoRun(void)
{
int numdays = 14;
uint64_t maxrows = 10000 * numdays; // likely high enough to keep numdays
bool res = true;
MSqlQuery query(MSqlQuery::InitCon());
if (query.isConnected())
{
// Remove less-important logging after 1/2 * numdays days
QDateTime days = MythDate::current();
days = days.addDays(0 - (numdays / 2));
QString sql = "DELETE FROM logging "
" WHERE application NOT IN (:MYTHBACKEND, :MYTHFRONTEND) "
" AND msgtime < :DAYS ;";
query.prepare(sql);
query.bindValue(":MYTHBACKEND", MYTH_APPNAME_MYTHBACKEND);
query.bindValue(":MYTHFRONTEND", MYTH_APPNAME_MYTHFRONTEND);
query.bindValue(":DAYS", days);
LOG(VB_GENERAL, LOG_DEBUG,
QString("Deleting helper application database log entries "
"from before %1.") .arg(days.toString(Qt::ISODate)));
if (!query.exec())
{
MythDB::DBError("Delete helper application log entries", query);
res = false;
}
// Remove backend/frontend logging after numdays days
days = MythDate::current();
days = days.addDays(0 - numdays);
sql = "DELETE FROM logging WHERE msgtime < :DAYS ;";
query.prepare(sql);
query.bindValue(":DAYS", days);
LOG(VB_GENERAL, LOG_DEBUG,
QString("Deleting database log entries from before %1.")
.arg(days.toString(Qt::ISODate)));
if (!query.exec())
{
MythDB::DBError("Delete old log entries", query);
res = false;
}
sql = "SELECT COUNT(id) FROM logging;";
query.prepare(sql);
if (query.exec())
{
uint64_t totalrows = 0;
while (query.next())
{
totalrows = query.value(0).toLongLong();
LOG(VB_GENERAL, LOG_DEBUG,
QString("Database has %1 log entries.").arg(totalrows));
}
if (totalrows > maxrows)
{
sql = "DELETE FROM logging ORDER BY msgtime LIMIT :ROWS;";
query.prepare(sql);
quint64 extrarows = totalrows - maxrows;
query.bindValue(":ROWS", extrarows);
LOG(VB_GENERAL, LOG_DEBUG,
QString("Deleting oldest %1 database log entries.")
.arg(extrarows));
if (!query.exec())
{
MythDB::DBError("Delete excess log entries", query);
res = false;
}
}
}
else
{
MythDB::DBError("Query logging table size", query);
res = false;
}
}
return res;
}
bool CleanupTask::DoRun(void)
{
JobQueue::CleanupOldJobsInQueue();
CleanupOldRecordings();
CleanupInUsePrograms();
CleanupOrphanedLiveTV();
CleanupRecordedTables();
CleanupProgramListings();
return true;
}
void CleanupTask::CleanupOldRecordings(void)
{
MSqlQuery query(MSqlQuery::InitCon());
query.prepare("DELETE FROM inuseprograms "
"WHERE hostname = :HOSTNAME AND "
"( recusage = 'recorder' OR recusage LIKE 'Unknown %' );");
query.bindValue(":HOSTNAME", gCoreContext->GetHostName());
if (!query.exec())
MythDB::DBError("CleanupTask::CleanupOldRecordings", query);
}
void CleanupTask::CleanupInUsePrograms(void)
{
QDateTime fourHoursAgo = MythDate::current().addSecs(-4 * 60 * 60);
MSqlQuery query(MSqlQuery::InitCon());
query.prepare("DELETE FROM inuseprograms "
"WHERE lastupdatetime < :FOURHOURSAGO ;");
query.bindValue(":FOURHOURSAGO", fourHoursAgo);
if (!query.exec())
MythDB::DBError("CleanupTask::CleanupInUsePrograms", query);
}
void CleanupTask::CleanupOrphanedLiveTV(void)
{
QDateTime fourHoursAgo = MythDate::current().addSecs(-4 * 60 * 60);
MSqlQuery query(MSqlQuery::InitCon());
MSqlQuery deleteQuery(MSqlQuery::InitCon());
// Keep these tvchains, they may be in use.
query.prepare("SELECT DISTINCT chainid FROM tvchain "
"WHERE endtime > :FOURHOURSAGO ;");
query.bindValue(":FOURHOURSAGO", fourHoursAgo);
if (!query.exec() || !query.isActive())
{
MythDB::DBError("HouseKeeper Cleaning TVChain Table", query);
return;
}
QString msg, keepChains;
while (query.next())
if (keepChains.isEmpty())
keepChains = "'" + query.value(0).toString() + "'";
else
keepChains += ", '" + query.value(0).toString() + "'";
if (keepChains.isEmpty())
msg = "DELETE FROM tvchain WHERE endtime < now();";
else
{
msg = QString("DELETE FROM tvchain "
"WHERE chainid NOT IN ( %1 ) AND endtime < now();")
.arg(keepChains);
}
deleteQuery.prepare(msg);
if (!deleteQuery.exec())
MythDB::DBError("CleanupTask::CleanupOrphanedLiveTV", deleteQuery);
}
void CleanupTask::CleanupRecordedTables(void)
{
MSqlQuery query(MSqlQuery::InitCon());
MSqlQuery deleteQuery(MSqlQuery::InitCon());
int tableIndex = 0;
// tables[tableIndex][0] is the table name
// tables[tableIndex][1] is the name of the column on which the join is
// performed
QString tables[][2] = {
{ "recordedprogram", "progstart" },
{ "recordedrating", "progstart" },
{ "recordedcredits", "progstart" },
{ "recordedmarkup", "starttime" },
{ "recordedseek", "starttime" },
{ "", "" } }; // This blank entry must exist, do not remove.
QString table = tables[tableIndex][0];
QString column = tables[tableIndex][1];
// Because recordedseek can have millions of rows, we don't want to JOIN it
// with recorded. Instead, pull out DISTINCT chanid and starttime into a
// temporary table (resulting in tens, hundreds, or--at most--a few
// thousand rows) for the JOIN
QString querystr;
querystr = "CREATE TEMPORARY TABLE IF NOT EXISTS temprecordedcleanup ( "
"chanid int(10) unsigned NOT NULL default '0', "
"starttime datetime NOT NULL default '0000-00-00 00:00:00' "
");";
if (!query.exec(querystr))
{
MythDB::DBError("CleanupTask::CleanupRecordedTables"
"(creating temporary table)", query);
return;
}
while (!table.isEmpty())
{
query.prepare(QString("TRUNCATE TABLE temprecordedcleanup;"));
if (!query.exec() || !query.isActive())
{
MythDB::DBError("CleanupTask::CleanupRecordedTables"
"(truncating temporary table)", query);
return;
}
query.prepare(QString("INSERT INTO temprecordedcleanup "
"( chanid, starttime ) "
"SELECT DISTINCT chanid, starttime "
"FROM %1;")
.arg(table));
if (!query.exec() || !query.isActive())
{
MythDB::DBError("CleanupTask::CleanupRecordedTables"
"(cleaning recorded tables)", query);
return;
}
query.prepare(QString("SELECT DISTINCT p.chanid, p.starttime "
"FROM temprecordedcleanup p "
"LEFT JOIN recorded r "
"ON p.chanid = r.chanid "
"AND p.starttime = r.%1 "
"WHERE r.chanid IS NULL;").arg(column));
if (!query.exec() || !query.isActive())
{
MythDB::DBError("CleanupTask::CleanupRecordedTables"
"(cleaning recorded tables)", query);
return;
}
deleteQuery.prepare(QString("DELETE FROM %1 "
"WHERE chanid = :CHANID "
"AND starttime = :STARTTIME;")
.arg(table));
while (query.next())
{
deleteQuery.bindValue(":CHANID", query.value(0).toString());
deleteQuery.bindValue(":STARTTIME", query.value(1));
if (!deleteQuery.exec())
{
MythDB::DBError("CleanupTask::CleanupRecordedTables"
"(cleaning recorded tables)", deleteQuery);
return;
}
}
tableIndex++;
table = tables[tableIndex][0];
column = tables[tableIndex][1];
}
if (!query.exec("DROP TABLE temprecordedcleanup;"))
MythDB::DBError("CleanupTask::CleanupRecordedTables"
"(deleting temporary table)", query);
}
void CleanupTask::CleanupProgramListings(void)
{
MSqlQuery query(MSqlQuery::InitCon());
QString querystr;
// Keep as many days of listings data as we keep matching, non-recorded
// oldrecorded entries to allow for easier post-mortem analysis
int offset = gCoreContext->GetNumSetting( "CleanOldRecorded", 10);
// Also make sure to keep enough data so that we can flag the original
// airdate, for when that isn't included in guide data
int newEpiWindow = gCoreContext->GetNumSetting( "NewEpisodeWindow", 14);
if (newEpiWindow > offset)
offset = newEpiWindow;
query.prepare("DELETE FROM oldprogram WHERE airdate < "
"DATE_SUB(CURRENT_DATE, INTERVAL 320 DAY);");
if (!query.exec())
MythDB::DBError("HouseKeeper Cleaning Program Listings", query);
query.prepare("REPLACE INTO oldprogram (oldtitle,airdate) "
"SELECT title,starttime FROM program "
"WHERE starttime < NOW() AND manualid = 0 "
"GROUP BY title;");
if (!query.exec())
MythDB::DBError("HouseKeeper Cleaning Program Listings", query);
query.prepare("DELETE FROM program WHERE starttime <= "
"DATE_SUB(CURRENT_DATE, INTERVAL :OFFSET DAY);");
query.bindValue(":OFFSET", offset);
if (!query.exec())
MythDB::DBError("HouseKeeper Cleaning Program Listings", query);
query.prepare("DELETE FROM programrating WHERE starttime <= "
"DATE_SUB(CURRENT_DATE, INTERVAL :OFFSET DAY);");
query.bindValue(":OFFSET", offset);
if (!query.exec())
MythDB::DBError("HouseKeeper Cleaning Program Listings", query);
query.prepare("DELETE FROM programgenres WHERE starttime <= "
"DATE_SUB(CURRENT_DATE, INTERVAL :OFFSET DAY);");
query.bindValue(":OFFSET", offset);
if (!query.exec())
MythDB::DBError("HouseKeeper Cleaning Program Listings", query);
query.prepare("DELETE FROM credits WHERE starttime <= "
"DATE_SUB(CURRENT_DATE, INTERVAL :OFFSET DAY);");
query.bindValue(":OFFSET", offset);
if (!query.exec())
MythDB::DBError("HouseKeeper Cleaning Program Listings", query);
query.prepare("DELETE FROM record WHERE (type = :SINGLE "
"OR type = :OVERRIDE OR type = :DONTRECORD) "
"AND enddate < CURDATE();");
query.bindValue(":SINGLE", kSingleRecord);
query.bindValue(":OVERRIDE", kOverrideRecord);
query.bindValue(":DONTRECORD", kDontRecord);
if (!query.exec())
MythDB::DBError("HouseKeeper Cleaning Program Listings", query);
MSqlQuery findq(MSqlQuery::InitCon());
findq.prepare("SELECT record.recordid FROM record "
"LEFT JOIN oldfind ON oldfind.recordid = record.recordid "
"WHERE type = :FINDONE AND oldfind.findid IS NOT NULL;");
findq.bindValue(":FINDONE", kOneRecord);
if (findq.exec())
{
query.prepare("DELETE FROM record WHERE recordid = :RECORDID;");
while (findq.next())
{
query.bindValue(":RECORDID", findq.value(0).toInt());
if (!query.exec())
MythDB::DBError("HouseKeeper Cleaning Program Listings", query);
}
}
query.prepare("DELETE FROM oldfind WHERE findid < TO_DAYS(NOW()) - 14;");
if (!query.exec())
MythDB::DBError("HouseKeeper Cleaning Program Listings", query);
query.prepare("DELETE FROM oldrecorded WHERE "
"recstatus <> :RECORDED AND duplicate = 0 AND "
"endtime < DATE_SUB(CURRENT_DATE, INTERVAL :CLEAN DAY);");
query.bindValue(":RECORDED", rsRecorded);
query.bindValue(":CLEAN", offset);
if (!query.exec())
MythDB::DBError("HouseKeeper Cleaning Program Listings", query);
}
bool ThemeUpdateTask::DoRun(void)
{
QString MythVersion = MYTH_SOURCE_PATH;
// FIXME: For now, treat git master the same as svn trunk
if (MythVersion == "master")
MythVersion = "trunk";
if (MythVersion != "trunk")
{
MythVersion = MYTH_BINARY_VERSION; // Example: 0.25.20101017-1
MythVersion.replace(QRegExp("\\.[0-9]{8,}.*"), "");
}
QString remoteThemesDir = GetConfDir();
remoteThemesDir.append("/tmp/remotethemes");
QDir dir(remoteThemesDir);
if (!dir.exists() && !dir.mkpath(remoteThemesDir))
{
LOG(VB_GENERAL, LOG_ERR,
QString("HouseKeeper: Error creating %1"
"directory for remote themes info cache.")
.arg(remoteThemesDir));
return false;
}
QString remoteThemesFile = remoteThemesDir;
remoteThemesFile.append("/themes.zip");
QString url = QString("%1/%2/themes.zip")
.arg(gCoreContext->GetSetting("ThemeRepositoryURL",
"http://themes.mythtv.org/themes/repository")).arg(MythVersion);
bool result = GetMythDownloadManager()->download(url, remoteThemesFile);
if (!result)
{
LOG(VB_GENERAL, LOG_ERR,
QString("HouseKeeper: Error downloading %1"
"remote themes info package.").arg(url));
return false;
}
if (!extractZIP(remoteThemesFile, remoteThemesDir))
{
LOG(VB_GENERAL, LOG_ERR,
QString("HouseKeeper: Error extracting %1"
"remote themes info package.").arg(remoteThemesFile));
QFile::remove(remoteThemesFile);
return false;
}
return true;
}
bool ArtworkTask::DoRun(void)
{
QString command = GetInstallPrefix() + "/bin/mythmetadatalookup";
QStringList args;
args << "--refresh-all-artwork";
args << logPropagateArgs;
LOG(VB_GENERAL, LOG_INFO, QString("Performing Artwork Refresh: %1 %2")
.arg(command).arg(args.join(" ")));
MythSystemLegacy artupd(command, args, kMSRunShell | kMSAutoCleanup);
artupd.Run();
artupd.Wait();
LOG(VB_GENERAL, LOG_INFO, QString("Artwork Refresh Complete"));
return true;
}
bool JobQueueRecoverTask::DoRun(void)
{
JobQueue::RecoverOldJobsInQueue();
return true;
}
MythFillDatabaseTask::MythFillDatabaseTask(void) :
DailyHouseKeeperTask("MythFillDB"), m_msMFD(NULL)
{
SetHourWindowFromDB();
}
void MythFillDatabaseTask::SetHourWindowFromDB(void)
{
// we need to set the time window from database settings, so we cannot
// initialize these values in. grab them and set them afterwards
int min = gCoreContext->GetNumSetting("MythFillMinHour", -1);
int max = gCoreContext->GetNumSetting("MythFillMaxHour", 23);
if (min == -1)
{
min = 0;
max = 23;
}
else
{
// make sure they fall within the range of 0-23
min %= 24;
max %= 24;
}
DailyHouseKeeperTask::SetHourWindow(min, max);
}
bool MythFillDatabaseTask::UseSuggestedTime(void)
{
if (!gCoreContext->GetNumSetting("MythFillGrabberSuggestsTime", 1))
// this feature is disabled, so don't bother with a deeper check
return false;
MSqlQuery result(MSqlQuery::InitCon());
if (result.isConnected())
{
// check to see if we have any of a list of supported grabbers in use
// TODO: this is really cludgy. there has to be a better way to test
result.prepare("SELECT COUNT(*) FROM videosource"
" WHERE xmltvgrabber IN"
" ( 'datadirect',"
" 'technovera',"
" 'schedulesdirect1' );");
if ((result.exec()) &&
(result.next()) &&
(result.value(0).toInt() > 0))
return true;
}
return false;
}
bool MythFillDatabaseTask::DoCheckRun(QDateTime now)
{
if (!gCoreContext->GetNumSetting("MythFillEnabled", 1))
{
// we don't want to run this manually, so abort early
LOG(VB_GENERAL, LOG_DEBUG, "MythFillDatabase is disabled. Cannot run.");
return false;
}
// if (m_running)
// // we're still running from the previous pass, so abort early
// return false;
if (UseSuggestedTime())
{
QDateTime nextRun = MythDate::fromString(
gCoreContext->GetSetting("MythFillSuggestedRunTime",
"1970-01-01T00:00:00"));
LOG(VB_GENERAL, LOG_DEBUG,
QString("MythFillDatabase scheduled to run at %1.")
.arg(nextRun.toString()));
if (nextRun > now)
// not yet time
return false;
if (InWindow(now))
// we're inside our permitted window
return true;
return false;
}
else
{
// just let DailyHouseKeeperTask handle things
LOG(VB_GENERAL, LOG_DEBUG, "Performing daily run check.");
return DailyHouseKeeperTask::DoCheckRun(now);
}
}
bool MythFillDatabaseTask::DoRun(void)
{
if (m_msMFD)
{
// this should never be defined, but terminate it anyway
if (m_msMFD->GetStatus() == GENERIC_EXIT_RUNNING)
m_msMFD->Term(true);
delete m_msMFD;
m_msMFD = NULL;
}
QString mfpath = gCoreContext->GetSetting("MythFillDatabasePath",
"mythfilldatabase");
QString mfarg = gCoreContext->GetSetting("MythFillDatabaseArgs", "");
uint opts = kMSRunShell | kMSAutoCleanup;
if (mfpath == "mythfilldatabase")
opts |= kMSPropagateLogs;
QString cmd = QString("%1 %2").arg(mfpath).arg(mfarg);
m_msMFD = new MythSystemLegacy(cmd, opts);
m_msMFD->Run();
uint result = m_msMFD->Wait();
m_msMFD = NULL;
if (result != GENERIC_EXIT_OK)
{
LOG(VB_GENERAL, LOG_ERR, QString("MythFillDatabase command '%1' failed")
.arg(cmd));
return false;
}
return true;
}
void MythFillDatabaseTask::Terminate(void)
{
if (m_msMFD && (m_msMFD->GetStatus() == GENERIC_EXIT_RUNNING))
// just kill it, the runner thread will handle any necessary cleanup
m_msMFD->Term(true);
}