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