10 #include "mythconfig.h"
19 #include <QCoreApplication>
46 #include "commandlineparser.h"
59 #define LOC QString("MythBackend: ")
60 #define LOC_WARN QString("MythBackend, Warning: ")
61 #define LOC_ERR QString("MythBackend, Error: ")
77 if (!query.
exec(
"UPDATE recorded SET basename = CONCAT(chanid, '_', "
78 "DATE_FORMAT(starttime, '%Y%m%d%H%i00'), '_', "
79 "DATE_FORMAT(endtime, '%Y%m%d%H%i00'), '.nuv') "
80 "WHERE basename = '';"))
86 if (!query.
exec(
"UPDATE channel SET callsign=chanid "
87 "WHERE callsign IS NULL OR callsign='';"))
90 if (query.
exec(
"SELECT MIN(chanid) FROM channel;"))
93 int min_chanid = query.
value(0).toInt();
94 if (!query.
exec(QString(
"UPDATE record SET chanid = %1 "
95 "WHERE chanid IS NULL;").arg(min_chanid)))
102 records_without_station.
prepare(
"SELECT record.chanid,"
103 " channel.callsign FROM record LEFT JOIN channel"
104 " ON record.chanid = channel.chanid WHERE record.station='';");
105 if (records_without_station.
exec() && records_without_station.
next())
108 update_record.
prepare(
"UPDATE record SET station = :CALLSIGN"
109 " WHERE chanid = :CHANID;");
113 records_without_station.
value(1));
115 records_without_station.
value(0));
116 if (!update_record.
exec())
120 }
while (records_without_station.
next());
125 "SELECT cardid, hostname "
133 vector<uint> cardids;
134 vector<QString> hosts;
138 QString host = query.
value(1).toString();
139 QString cidmsg = QString(
"Card %1").arg(cardid);
143 LOG(VB_GENERAL, LOG_ERR, cidmsg +
144 " does not have a hostname defined.\n"
145 "Please run setup and confirm all of the capture cards.\n");
149 cardids.push_back(cardid);
150 hosts.push_back(host);
153 for (
uint i = 0; i < cardids.size(); i++)
155 if (hosts[i] == localhostname)
156 new TVRec(cardids[i]);
159 for (
uint i = 0; i < cardids.size(); i++)
161 uint cardid = cardids[i];
162 QString host = hosts[i];
163 QString cidmsg = QString(
"Card %1").arg(cardid);
167 if (host == localhostname)
170 if (tv && tv->
Init())
177 LOG(VB_GENERAL, LOG_ERR,
"Problem with capture cards. " +
178 cidmsg +
" failed init");
188 if (host == localhostname)
191 if (tv && tv->
Init())
198 LOG(VB_GENERAL, LOG_ERR,
"Problem with capture cards" +
199 cidmsg +
"failed init");
213 LOG(VB_GENERAL, LOG_WARNING,
LOC +
214 "No valid capture cards are defined in the database.");
269 unlink(
pidfile.toLatin1().constData());
280 if (cmdline.
toBool(
"event"))
281 eventString = cmdline.
toString(
"event");
282 else if (cmdline.
toBool(
"systemevent"))
283 eventString =
"SYSTEM_EVENT " +
287 if (!eventString.isEmpty())
292 return GENERIC_EXIT_OK;
294 return GENERIC_EXIT_NO_MYTHCONTEXT;
297 if (cmdline.
toBool(
"setverbose"))
301 QString message =
"SET_VERBOSE ";
302 message += cmdline.
toString(
"setverbose");
305 LOG(VB_GENERAL, LOG_INFO,
306 QString(
"Sent '%1' message").arg(message));
307 return GENERIC_EXIT_OK;
311 LOG(VB_GENERAL, LOG_ERR,
312 "Unable to connect to backend, verbose mask unchanged ");
313 return GENERIC_EXIT_CONNECT_ERROR;
317 if (cmdline.
toBool(
"setloglevel"))
321 QString message =
"SET_LOG_LEVEL ";
322 message += cmdline.
toString(
"setloglevel");
325 LOG(VB_GENERAL, LOG_INFO,
326 QString(
"Sent '%1' message").arg(message));
327 return GENERIC_EXIT_OK;
331 LOG(VB_GENERAL, LOG_ERR,
332 "Unable to connect to backend, log level unchanged ");
333 return GENERIC_EXIT_CONNECT_ERROR;
337 if (cmdline.
toBool(
"clearcache"))
342 LOG(VB_GENERAL, LOG_INFO,
"Sent CLEAR_SETTINGS_CACHE message");
343 return GENERIC_EXIT_OK;
347 LOG(VB_GENERAL, LOG_ERR,
"Unable to connect to backend, settings "
348 "cache will not be cleared.");
349 return GENERIC_EXIT_CONNECT_ERROR;
353 if (cmdline.
toBool(
"printsched") ||
354 cmdline.
toBool(
"testsched"))
357 if (!cmdline.
toBool(
"testsched") &&
360 cout <<
"Retrieving Schedule from Master backend.\n";
365 cout <<
"Calculating Schedule from database.\n" <<
366 "Inputs, Card IDs, and Conflict info may be invalid "
367 "if you have multiple tuners.\n";
378 return GENERIC_EXIT_OK;
381 if (cmdline.
toBool(
"resched"))
386 LOG(VB_GENERAL, LOG_INFO,
"Connected to master for reschedule");
388 "MythBackendCommand");
392 LOG(VB_GENERAL, LOG_ERR,
"Cannot connect to master for reschedule");
394 return (ok) ? GENERIC_EXIT_OK : GENERIC_EXIT_CONNECT_ERROR;
397 if (cmdline.
toBool(
"scanvideos"))
403 LOG(VB_GENERAL, LOG_INFO,
"Requested video scan");
407 LOG(VB_GENERAL, LOG_ERR,
"Cannot connect to master for video scan");
409 return (ok) ? GENERIC_EXIT_OK : GENERIC_EXIT_CONNECT_ERROR;
412 if (cmdline.
toBool(
"printexpire"))
416 return GENERIC_EXIT_OK;
420 return GENERIC_EXIT_OK;
422 using namespace MythTZ;
433 LOG(VB_GENERAL, LOG_ERR,
"Master backend is incompatible with "
434 "this backend.\nCannot become a slave.");
435 return GENERIC_EXIT_CONNECT_ERROR;
438 QStringList tempMonitorDone(
"DONE");
440 QStringList tempMonitorAnnounce(
"ANN Monitor tzcheck 0");
442 if (tempMonitorAnnounce.empty() ||
443 tempMonitorAnnounce[0] ==
"ERROR")
445 tempMonitorConnection->
DecrRef();
446 tempMonitorConnection = NULL;
447 if (tempMonitorAnnounce.empty())
449 LOG(VB_GENERAL, LOG_ERR,
LOC +
450 "Failed to open event socket, timeout");
454 LOG(VB_GENERAL, LOG_ERR,
LOC +
455 "Failed to open event socket" +
456 ((tempMonitorAnnounce.size() >= 2) ?
457 QString(
", error was %1").arg(tempMonitorAnnounce[1]) :
458 QString(
", remote error")));
462 QStringList timeCheck;
463 if (tempMonitorConnection)
465 timeCheck.push_back(
"QUERY_TIME_ZONE");
468 if (timeCheck.size() < 3)
470 return GENERIC_EXIT_SOCKET_ERROR;
476 int timediff = abs(our_time.secsTo(master_time));
480 LOG(VB_GENERAL, LOG_ERR,
481 QString(
"Current time on the master backend differs by "
482 "%1 seconds from time on this system. Exiting.")
484 tempMonitorConnection->
DecrRef();
485 return GENERIC_EXIT_INVALID_TIME;
490 LOG(VB_GENERAL, LOG_WARNING,
491 QString(
"Time difference between the master "
492 "backend and this system is %1 seconds.")
496 if (tempMonitorConnection)
497 tempMonitorConnection->
DecrRef();
499 return GENERIC_EXIT_OK;
505 if (cmdline.
toBool(
"nohousekeeper"))
507 LOG(VB_GENERAL, LOG_WARNING,
LOC +
508 "****** The Housekeeper has been DISABLED with "
509 "the --nohousekeeper option ******");
511 if (cmdline.
toBool(
"nosched"))
513 LOG(VB_GENERAL, LOG_WARNING,
LOC +
514 "********** The Scheduler has been DISABLED with "
515 "the --nosched option **********");
517 if (cmdline.
toBool(
"noautoexpire"))
519 LOG(VB_GENERAL, LOG_WARNING,
LOC +
520 "********* Auto-Expire has been DISABLED with "
521 "the --noautoexpire option ********");
523 if (cmdline.
toBool(
"nojobqueue"))
525 LOG(VB_GENERAL, LOG_WARNING,
LOC +
526 "********* The JobQueue has been DISABLED with "
527 "the --nojobqueue option *********");
535 LOG(VB_GENERAL, LOG_ERR,
536 "MySQL time zone support is missing. "
537 "Please install it and try again. "
538 "See 'mysql_tzinfo_to_sql' for assistance.");
539 return GENERIC_EXIT_DB_NOTIMEZONE;
546 LOG(VB_GENERAL, LOG_ERR,
"Couldn't upgrade database to new schema");
547 return GENERIC_EXIT_DB_OUTOFDATE;
555 if (ret != GENERIC_EXIT_OK)
563 cerr <<
"No setting found for this machine's BackendServerIP.\n"
564 <<
"Please run setup on this machine and modify the first page\n"
565 <<
"of the general settings.\n";
566 return GENERIC_EXIT_SETUP_ERROR;
573 LOG(VB_GENERAL, LOG_NOTICE,
LOC +
"Starting up as the master server.");
577 LOG(VB_GENERAL, LOG_NOTICE,
LOC +
"Running as a slave backend.");
582 bool fatal_error =
false;
583 bool runsched =
setupTVs(ismaster, fatal_error);
586 delete sysEventHandler;
587 return GENERIC_EXIT_SETUP_ERROR;
600 if (cmdline.
toBool(
"nosched"))
604 if (!cmdline.
toBool(
"noautoexpire"))
613 if (!cmdline.
toBool(
"nohousekeeper"))
628 #ifdef CONFIG_BINDINGS_PYTHON
636 if (!cmdline.
toBool(
"nojobqueue"))
659 LOG(VB_GENERAL, LOG_INFO,
"Main::Registering HttpStatus Extension");
669 if (exitCode != GENERIC_EXIT_OK)
671 LOG(VB_GENERAL, LOG_CRIT,
672 "Backend exiting, MainServer initialization error.");
677 if (httpStatus && mainServer)
687 exitCode = qApp->exec();
694 qApp->processEvents();
697 LOG(VB_GENERAL, LOG_NOTICE,
"MythBackend exiting");
699 delete sysEventHandler;