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 using namespace std;
40 
41 JobQueue *jobqueue = nullptr;
42 QString pidfile;
43 QString logfile;
44 
45 static void cleanup(void)
46 {
47  delete gContext;
48  gContext = nullptr;
49 
50  if (pidfile.size())
51  {
52  unlink(pidfile.toLatin1().constData());
53  pidfile.clear();
54  }
55 
57 }
58 
59 int main(int argc, char *argv[])
60 {
62  if (!cmdline.Parse(argc, argv))
63  {
66  }
67 
68  if (cmdline.toBool("showhelp"))
69  {
71  return GENERIC_EXIT_OK;
72  }
73 
74  if (cmdline.toBool("showversion"))
75  {
77  return GENERIC_EXIT_OK;
78  }
79 
80  QCoreApplication a(argc, argv);
81  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHJOBQUEUE);
82 
83  int retval = cmdline.Daemonize();
84  if (retval != GENERIC_EXIT_OK)
85  return retval;
86 
87  bool daemonize = cmdline.toBool("daemon");
88  QString mask("general");
89  if ((retval = cmdline.ConfigureLogging(mask, daemonize)) != GENERIC_EXIT_OK)
90  return retval;
91 
92  CleanupGuard callCleanup(cleanup);
93 
94 #ifndef _WIN32
95  QList<int> signallist;
96  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
97  << SIGILL;
98 #if ! CONFIG_DARWIN
99  signallist << SIGRTMIN;
100 #endif
101  SignalHandler::Init(signallist);
102  SignalHandler::SetHandler(SIGHUP, logSigHup);
103 #endif
104 
106  if (!gContext->Init(false))
107  {
108  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to init MythContext, exiting.");
110  }
111 
113 
115  {
116  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to connect to master server");
118  }
119 
120  jobqueue = new JobQueue(false);
121 
123 
125 #ifdef __linux__
126  #ifdef CONFIG_BINDINGS_PYTHON
128  #endif
129 #endif
130  housekeeping->Start();
131 
132  int exitCode = QCoreApplication::exec();
133 
134  delete sysEventHandler;
135 
136  return exitCode ? exitCode : GENERIC_EXIT_OK;
137 }
138 
139 /* vim: set expandtab tabstop=4 shiftwidth=4: */
int Daemonize(void)
Fork application into background, and detatch from terminal.
Startup context for MythTV.
Definition: mythcontext.h:42
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
#define GENERIC_EXIT_CONNECT_ERROR
Can't connect to master backend.
Definition: exitcodes.h:20
void RegisterTask(HouseKeeperTask *task)
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
void PrintHelp(void) const
Print command line option help.
HouseKeeper * housekeeping
bool ConnectToMasterServer(bool blockingClient=true, bool openEventSocket=true)
MythSystemEventHandler * sysEventHandler
static void Init(QList< int > &signallist, QObject *parent=nullptr)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:63
int main(int argc, char *argv[])
void ApplySettingsOverride(void)
Apply all overrides to the global context.
static void cleanup(void)
Manages registered HouseKeeperTasks and queues tasks for operation.
Definition: housekeeper.h:148
static void Done(void)
static void SetHandler(int signum, SigHandlerFunc handler)
MythCommFlagCommandLineParser cmdline
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
#define SIGHUP
Definition: compat.h:213
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:16
bool Init(const bool gui=true, const bool promptForBackend=false, const bool disableAutoDiscovery=false, const bool ignoreDB=false)
Handles incoming MythSystemEvent messages.
#define MYTH_APPNAME_MYTHJOBQUEUE
int ConfigureLogging(QString mask="general", unsigned int progress=0)
Read in logging options and initialize the logging interface.
#define GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:13
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
void PrintVersion(void) const
Print application version information.
void Start(void)