chore: Improve logging related to Windows daemon start/stop/install/uninstall
This commit is contained in:
parent
19c41e2ac5
commit
a99d408be1
5 changed files with 30 additions and 10 deletions
|
|
@ -69,8 +69,10 @@ int main(int argc, char **argv)
|
||||||
daemon.moveToThread(&daemonThread);
|
daemon.moveToThread(&daemonThread);
|
||||||
|
|
||||||
QObject::connect(&daemonThread, &QThread::started, [&daemon, &daemonThread]() {
|
QObject::connect(&daemonThread, &QThread::started, [&daemon, &daemonThread]() {
|
||||||
|
LOG_DEBUG("daemon thread started");
|
||||||
daemon.run();
|
daemon.run();
|
||||||
daemonThread.quit();
|
daemonThread.quit();
|
||||||
|
LOG_DEBUG("daemon thread finished");
|
||||||
});
|
});
|
||||||
QObject::connect(&daemonThread, &QThread::finished, &app, &QCoreApplication::quit);
|
QObject::connect(&daemonThread, &QThread::finished, &app, &QCoreApplication::quit);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "arch/win32/ArchDaemonWindows.h"
|
#include "arch/win32/ArchDaemonWindows.h"
|
||||||
|
|
||||||
#include "arch/Arch.h"
|
#include "arch/Arch.h"
|
||||||
#include "arch/win32/ArchMiscWindows.h"
|
#include "arch/win32/ArchMiscWindows.h"
|
||||||
#include "arch/win32/XArchWindows.h"
|
#include "arch/win32/XArchWindows.h"
|
||||||
|
|
@ -59,6 +60,8 @@ void ArchDaemonWindows::installDaemon(
|
||||||
const char *name, const char *description, const char *pathname, const char *commandLine, const char *dependencies
|
const char *name, const char *description, const char *pathname, const char *commandLine, const char *dependencies
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
LOG_DEBUG("installing windows service: %s", name);
|
||||||
|
|
||||||
// open service manager
|
// open service manager
|
||||||
SC_HANDLE mgr = OpenSCManager(NULL, NULL, GENERIC_WRITE);
|
SC_HANDLE mgr = OpenSCManager(NULL, NULL, GENERIC_WRITE);
|
||||||
if (mgr == NULL) {
|
if (mgr == NULL) {
|
||||||
|
|
@ -125,6 +128,8 @@ void ArchDaemonWindows::installDaemon(
|
||||||
|
|
||||||
void ArchDaemonWindows::uninstallDaemon(const char *name)
|
void ArchDaemonWindows::uninstallDaemon(const char *name)
|
||||||
{
|
{
|
||||||
|
LOG_DEBUG("uninstalling windows service: %s", name);
|
||||||
|
|
||||||
// remove parameters for this service. ignore failures.
|
// remove parameters for this service. ignore failures.
|
||||||
HKEY key = openNTServicesKey();
|
HKEY key = openNTServicesKey();
|
||||||
key = ArchMiscWindows::openKey(key, name);
|
key = ArchMiscWindows::openKey(key, name);
|
||||||
|
|
|
||||||
|
|
@ -80,17 +80,17 @@ DaemonApp::~DaemonApp() = default;
|
||||||
void DaemonApp::run()
|
void DaemonApp::run()
|
||||||
{
|
{
|
||||||
if (m_foreground) {
|
if (m_foreground) {
|
||||||
LOG_INFO("starting daemon in foreground");
|
LOG_DEBUG("starting daemon in foreground");
|
||||||
|
|
||||||
// run process in foreground instead of daemonizing.
|
// run process in foreground instead of daemonizing.
|
||||||
// useful for debugging.
|
// useful for debugging.
|
||||||
mainLoop(m_foreground);
|
mainLoop(m_foreground);
|
||||||
} else {
|
} else {
|
||||||
#if SYSAPI_WIN32
|
#if SYSAPI_WIN32
|
||||||
LOG_INFO("daemonizing windows service");
|
LOG_DEBUG("daemonizing windows service");
|
||||||
ARCH->daemonize(kAppName, winMainLoopStatic);
|
ARCH->daemonize(kAppName, winMainLoopStatic);
|
||||||
#elif SYSAPI_UNIX
|
#elif SYSAPI_UNIX
|
||||||
LOG_INFO("daemonizing unix service");
|
LOG_DEBUG("daemonizing unix service");
|
||||||
ARCH->daemonize(kAppName, unixMainLoopStatic);
|
ARCH->daemonize(kAppName, unixMainLoopStatic);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
@ -253,9 +253,12 @@ void DaemonApp::mainLoop(bool foreground)
|
||||||
m_watchdog->startAsync();
|
m_watchdog->startAsync();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
LOG_INFO("daemon is running");
|
||||||
m_events->loop();
|
m_events->loop();
|
||||||
|
LOG_INFO("daemon is stopping");
|
||||||
|
|
||||||
#if SYSAPI_WIN32
|
#if SYSAPI_WIN32
|
||||||
|
LOG_DEBUG("stopping process watchdog");
|
||||||
m_watchdog->stop();
|
m_watchdog->stop();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -127,12 +127,16 @@ void MSWindowsProcess::shutdown(HANDLE handle, DWORD pid, int timeout)
|
||||||
SetEvent(hCloseEvent);
|
SetEvent(hCloseEvent);
|
||||||
CloseHandle(hCloseEvent);
|
CloseHandle(hCloseEvent);
|
||||||
} else {
|
} else {
|
||||||
LOG((CLOG_WARN "could not send close event to process"));
|
LOG_WARN("could not send close event to process");
|
||||||
throw XArch(new XArchEvalWindows);
|
throw XArch(new XArchEvalWindows);
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD exitCode;
|
DWORD exitCode;
|
||||||
GetExitCodeProcess(handle, &exitCode);
|
if (!GetExitCodeProcess(handle, &exitCode)) {
|
||||||
|
LOG_ERR("failed to get process exit code for process %d", pid);
|
||||||
|
throw XArch(new XArchEvalWindows);
|
||||||
|
}
|
||||||
|
|
||||||
if (exitCode != STILL_ACTIVE) {
|
if (exitCode != STILL_ACTIVE) {
|
||||||
LOG_DEBUG("process %d is already shutdown", pid);
|
LOG_DEBUG("process %d is already shutdown", pid);
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -93,10 +93,14 @@ void MSWindowsWatchdog::stop()
|
||||||
{
|
{
|
||||||
m_monitoring = false;
|
m_monitoring = false;
|
||||||
|
|
||||||
m_thread->wait(5);
|
if (!m_thread->wait(5)) {
|
||||||
|
LOG((CLOG_WARN "could not stop main thread"));
|
||||||
|
}
|
||||||
delete m_thread;
|
delete m_thread;
|
||||||
|
|
||||||
m_outputThread->wait(5);
|
if (!m_outputThread->wait(5)) {
|
||||||
|
LOG((CLOG_WARN "could not stop output thread"));
|
||||||
|
}
|
||||||
delete m_outputThread;
|
delete m_outputThread;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -161,7 +165,7 @@ MSWindowsWatchdog::getUserToken(LPSECURITY_ATTRIBUTES security, bool elevatedTok
|
||||||
|
|
||||||
void MSWindowsWatchdog::mainLoop(void *)
|
void MSWindowsWatchdog::mainLoop(void *)
|
||||||
{
|
{
|
||||||
LOG_DEBUG("starting main loop");
|
LOG_DEBUG("starting watchdog main loop");
|
||||||
|
|
||||||
shutdownExistingProcesses();
|
shutdownExistingProcesses();
|
||||||
|
|
||||||
|
|
@ -277,7 +281,7 @@ void MSWindowsWatchdog::mainLoop(void *)
|
||||||
m_processStarted = false;
|
m_processStarted = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG((CLOG_DEBUG "watchdog main thread finished"));
|
LOG((CLOG_DEBUG "watchdog main loop finished"));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MSWindowsWatchdog::isProcessRunning()
|
bool MSWindowsWatchdog::isProcessRunning()
|
||||||
|
|
@ -347,8 +351,10 @@ void MSWindowsWatchdog::startProcess()
|
||||||
LOG((CLOG_ERR "exit code: %d", exitCode));
|
LOG((CLOG_ERR "exit code: %d", exitCode));
|
||||||
throw XArch(new XArchEvalWindows);
|
throw XArch(new XArchEvalWindows);
|
||||||
} else {
|
} else {
|
||||||
// wait for program to fail.
|
// Wait for program to fail. This needs to be 1 second, as the process may take some time to fail.
|
||||||
|
LOG_DEBUG("waiting for process start result");
|
||||||
ARCH->sleep(1);
|
ARCH->sleep(1);
|
||||||
|
|
||||||
if (!isProcessRunning()) {
|
if (!isProcessRunning()) {
|
||||||
m_process.reset();
|
m_process.reset();
|
||||||
throw XMSWindowsWatchdogError("process immediately stopped");
|
throw XMSWindowsWatchdogError("process immediately stopped");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue