MythTV  master
mythtv/programs/mythjobqueue/main.cpp
Go to the documentation of this file.
1 #include <cmath>
2 #include <csignal>
3 #include <cstdio>
4 #include <cstdlib>
5 #include <ctime>
6 #include <fcntl.h>
7 #include <fstream>
8 #include <iostream>
9 #include <string>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13 
14 #include <QCoreApplication>
15 #include <QString>
16 #include <QRegExp>
17 #include <QFileInfo>
18 #include <QDir>
19 
20 #include "exitcodes.h"
21 #include "mythcontext.h"
22 #include "jobqueue.h"
23 #include "mythdbcon.h"
24 #include "mythversion.h"
25 #include "commandlineparser.h"
26 #include "compat.h"
27 #include "mythsystemevent.h"
28 #include "loggingserver.h"
29 #include "mythlogging.h"
30 #include "signalhandling.h"
31 #include "housekeeper.h"
32 #include "hardwareprofile.h"
33 #include "cleanupguard.h"
34 
35 #define LOC QString("MythJobQueue: ")
36 #define LOC_WARN QString("MythJobQueue, Warning: ")
37 #define LOC_ERR QString("MythJobQueue, Error: ")
38 
39 JobQueue *jobqueue = nullptr;
40 QString pidfile;
41 QString logfile;
42 
43 static void cleanup(void)
44 {
45  delete gContext;
46  gContext = nullptr;
47 
48  if (!pidfile.isEmpty())
49  {
50  unlink(pidfile.toLatin1().constData());
51  pidfile.clear();
52  }
53 
55 }
56 
57 int main(int argc, char *argv[])
58 {
60  if (!cmdline.Parse(argc, argv))
61  {
64  }
65 
66  if (cmdline.toBool("showhelp"))
67  {
69  return GENERIC_EXIT_OK;
70  }
71 
72  if (cmdline.toBool("showversion"))
73  {
75  return GENERIC_EXIT_OK;
76  }
77 
78  QCoreApplication a(argc, argv);
79  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHJOBQUEUE);
80 
81  int retval = cmdline.Daemonize();
82  if (retval != GENERIC_EXIT_OK)
83  return retval;
84 
85  bool daemonize = cmdline.toBool("daemon");
86  QString mask("general");
87  if ((retval = cmdline.ConfigureLogging(mask, daemonize)) != GENERIC_EXIT_OK)
88  return retval;
89 
90  CleanupGuard callCleanup(cleanup);
91 
92 #ifndef _WIN32
93  QList<int> signallist;
94  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
95  << SIGILL;
96 #if ! CONFIG_DARWIN
97  signallist << SIGRTMIN;
98 #endif
99  SignalHandler::Init(signallist);
100  SignalHandler::SetHandler(SIGHUP, logSigHup);
101 #endif
102 
104  if (!gContext->Init(false))
105  {
106  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to init MythContext, exiting.");
108  }
109 
111 
113  {
114  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to connect to master server");
116  }
117 
118  jobqueue = new JobQueue(false);
119 
121 
122  auto *housekeeping = new HouseKeeper();
123 #ifdef __linux__
124  #ifdef CONFIG_BINDINGS_PYTHON
126  #endif
127 #endif
128  housekeeping->Start();
129 
130  int exitCode = QCoreApplication::exec();
131 
132  delete sysEventHandler;
133 
134  return exitCode ? exitCode : GENERIC_EXIT_OK;
135 }
136 
137 /* vim: set expandtab tabstop=4 shiftwidth=4: */
commandlineparser.h
hardwareprofile.h
HouseKeeper
Manages registered HouseKeeperTasks and queues tasks for operation.
Definition: housekeeper.h:149
GENERIC_EXIT_OK
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
sysEventHandler
MythSystemEventHandler * sysEventHandler
Definition: backendcontext.cpp:15
pidfile
QString pidfile
Definition: mythtv/programs/mythjobqueue/main.cpp:40
housekeeper.h
MythCoreContext::ConnectToMasterServer
bool ConnectToMasterServer(bool blockingClient=true, bool openEventSocket=true)
Definition: mythcorecontext.cpp:377
MythContext
Startup context for MythTV.
Definition: mythcontext.h:44
mythdbcon.h
logfile
QString logfile
Definition: mythtv/programs/mythjobqueue/main.cpp:41
MythJobQueueCommandLineParser
Definition: mythjobqueue/commandlineparser.h:7
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
mythsystemevent.h
HouseKeeper::RegisterTask
void RegisterTask(HouseKeeperTask *task)
Definition: housekeeper.cpp:638
GENERIC_EXIT_INVALID_CMDLINE
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
mythversion.h
MythCommandLineParser::Parse
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
Definition: mythcommandlineparser.cpp:1435
mythlogging.h
signalhandling.h
CleanupGuard
Definition: cleanupguard.h:7
compat.h
MYTH_APPNAME_MYTHJOBQUEUE
#define MYTH_APPNAME_MYTHJOBQUEUE
Definition: mythcorecontext.h:20
SIGHUP
#define SIGHUP
Definition: compat.h:215
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1263
HardwareProfileTask
Definition: hardwareprofile.h:53
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1279
jobqueue.h
MythCommandLineParser::Daemonize
int Daemonize(void) const
Fork application into background, and detatch from terminal.
Definition: mythcommandlineparser.cpp:2760
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
MythCommandLineParser::ApplySettingsOverride
void ApplySettingsOverride(void)
Apply all overrides to the global context.
Definition: mythcommandlineparser.cpp:2654
housekeeping
HouseKeeper * housekeeping
Definition: backendcontext.cpp:10
MYTH_BINARY_VERSION
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:15
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythtv/programs/mythcommflag/main.cpp:76
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:1941
cleanupguard.h
cleanup
static void cleanup(void)
Definition: mythtv/programs/mythjobqueue/main.cpp:43
mythcontext.h
GENERIC_EXIT_NO_MYTHCONTEXT
#define GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:13
SignalHandler::SetHandler
static void SetHandler(int signum, SigHandlerFunc handler)
Definition: signalhandling.cpp:138
JobQueue
Definition: jobqueue.h:123
MythCommandLineParser::ConfigureLogging
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
Definition: mythcommandlineparser.cpp:2585
LOC
#define LOC
Definition: mythtv/programs/mythjobqueue/main.cpp:35
GENERIC_EXIT_CONNECT_ERROR
#define GENERIC_EXIT_CONNECT_ERROR
Can't connect to master backend.
Definition: exitcodes.h:20
exitcodes.h
main
int main(int argc, char *argv[])
Definition: mythtv/programs/mythjobqueue/main.cpp:57
loggingserver.h
MythSystemEventHandler
Handles incoming MythSystemEvent messages.
Definition: mythsystemevent.h:25
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:64
HouseKeeper::Start
void Start(void)
Definition: housekeeper.cpp:672
MythContext::Init
bool Init(bool gui=true, bool promptForBackend=false, bool disableAutoDiscovery=false, bool ignoreDB=false)
Definition: mythcontext.cpp:1564
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:131
jobqueue
JobQueue * jobqueue
Definition: mythtv/programs/mythjobqueue/main.cpp:39
SignalHandler::Init
static void Init(QList< int > &signallist, QObject *parent=nullptr)
Definition: signalhandling.cpp:124