add AutoStart option, Last Start and Last Stop
This commit is contained in:
parent
95db23458f
commit
d37369ad4f
6 changed files with 48 additions and 16 deletions
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
project(smsub LANGUAGES CXX VERSION 0.7)
|
||||
project(smsub LANGUAGES CXX VERSION 0.8)
|
||||
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*****************************************************************************
|
||||
* smsub Server Manager Subprocess
|
||||
* Copyright (C) 2020-2023 Syping
|
||||
* Copyright (C) 2020-2024 Syping
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
|
@ -18,9 +18,9 @@
|
|||
|
||||
#include <QCoreApplication>
|
||||
#include <QTextStream>
|
||||
#include <QDateTime>
|
||||
#include "SMSubProcess.h"
|
||||
#include "smsub.h"
|
||||
#include <QDebug>
|
||||
|
||||
SMSubProcess::SMSubProcess(const QString &executable, const QStringList &arguments, const QString &workingDirectory, const int &termTimeout, const bool &keepAlive) :
|
||||
termTimeout(termTimeout), keepAlive(keepAlive)
|
||||
|
@ -41,12 +41,12 @@ SMSubProcess::SMSubProcess(const QString &executable, const QStringList &argumen
|
|||
QObject::connect(&process, &QProcess::errorOccurred, this, &SMSubProcess::processError);
|
||||
QObject::connect(&process, &QProcess::started, this, [=]() {
|
||||
QTextStream(stderr) << "Subprocess started!" << smsub_endl;
|
||||
emit statusUpdated(true);
|
||||
emit statusUpdated(true, QDateTime::currentDateTimeUtc().toSecsSinceEpoch());
|
||||
});
|
||||
QObject::connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [=](int exitCode) {
|
||||
QTextStream(stderr) << "Subprocess exited!" << smsub_endl;
|
||||
// Exit with the same exit code as the process
|
||||
emit statusUpdated(false);
|
||||
emit statusUpdated(false, QDateTime::currentDateTimeUtc().toSecsSinceEpoch());
|
||||
if (!keepAlive)
|
||||
QCoreApplication::exit(exitCode);
|
||||
});
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*****************************************************************************
|
||||
* smsub Server Manager Subprocess
|
||||
* Copyright (C) 2020-2023 Syping
|
||||
* Copyright (C) 2020-2024 Syping
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
|
@ -46,7 +46,7 @@ private slots:
|
|||
|
||||
signals:
|
||||
void outputWritten(const QByteArray &output);
|
||||
void statusUpdated(const bool status);
|
||||
void statusUpdated(const bool status, const qint64 time);
|
||||
};
|
||||
|
||||
#endif // SMSUBPROCESS_H
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*****************************************************************************
|
||||
* smsub Server Manager Subprocess
|
||||
* Copyright (C) 2020-2023 Syping
|
||||
* Copyright (C) 2020-2024 Syping
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
|
@ -33,6 +33,8 @@ SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &soc
|
|||
type = ServerType::Local;
|
||||
server = localServer;
|
||||
status = false;
|
||||
startTime = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
|
||||
stopTime = startTime;
|
||||
}
|
||||
|
||||
SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &serverName, const quint16 &port) : serverSettings(serverSettings)
|
||||
|
@ -45,6 +47,8 @@ SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &ser
|
|||
type = ServerType::WebSocket;
|
||||
server = webSocketServer;
|
||||
status = false;
|
||||
startTime = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
|
||||
stopTime = startTime;
|
||||
}
|
||||
|
||||
bool SMSubServer::isListening()
|
||||
|
@ -126,10 +130,10 @@ bool SMSubServer::messageReceived(QObject *socket, const QByteArray &message)
|
|||
}
|
||||
else if (message.startsWith("status")) {
|
||||
if (status) {
|
||||
sendMessage(socket, "Status: on\n");
|
||||
sendMessage(socket, QString("Status: on\nLast Start: %1\nLast Stop: %2\n").arg(QString::number(startTime), QString::number(stopTime)).toUtf8());
|
||||
}
|
||||
else {
|
||||
sendMessage(socket, "Status: off\n");
|
||||
sendMessage(socket, QString("Status: off\nLast Start: %1\nLast Stop: %2\n").arg(QString::number(startTime), QString::number(stopTime)).toUtf8());
|
||||
}
|
||||
}
|
||||
else if (message.startsWith("start")) {
|
||||
|
@ -273,7 +277,8 @@ void SMSubServer::registerToken(const QString &token)
|
|||
});
|
||||
}
|
||||
|
||||
void SMSubServer::statusUpdated(const bool status_)
|
||||
void SMSubServer::statusUpdated(const bool status_, const qint64 time)
|
||||
{
|
||||
status = status_;
|
||||
status ? (startTime = time) : (stopTime = time);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*****************************************************************************
|
||||
* smsub Server Manager Subprocess
|
||||
* Copyright (C) 2020-2023 Syping
|
||||
* Copyright (C) 2020-2024 Syping
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
|
@ -45,7 +45,7 @@ public:
|
|||
public slots:
|
||||
void writeOutput(const QByteArray &output);
|
||||
void registerToken(const QString &token);
|
||||
void statusUpdated(const bool status);
|
||||
void statusUpdated(const bool status, const qint64 time);
|
||||
|
||||
private slots:
|
||||
void wsMessageReceived(const QByteArray &message);
|
||||
|
@ -60,6 +60,8 @@ private:
|
|||
SMSubServerSettings *serverSettings;
|
||||
QVector<QObject*> sockets;
|
||||
QVector<QString> tokens;
|
||||
qint64 startTime;
|
||||
qint64 stopTime;
|
||||
ServerType type;
|
||||
QObject *server;
|
||||
bool status;
|
||||
|
|
29
main.cpp
29
main.cpp
|
@ -1,6 +1,6 @@
|
|||
/*****************************************************************************
|
||||
* smsub Server Manager Subprocess
|
||||
* Copyright (C) 2020-2023 Syping
|
||||
* Copyright (C) 2020-2024 Syping
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
|
@ -134,13 +134,14 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
a.setApplicationName("Server Manager Subprocess");
|
||||
a.setApplicationVersion("0.7");
|
||||
a.setApplicationVersion("0.8");
|
||||
|
||||
#ifdef Q_OS_UNIX
|
||||
catchUnixSignals({SIGINT, SIGHUP, SIGQUIT, SIGTERM});
|
||||
#endif
|
||||
|
||||
bool rportSet = false;
|
||||
bool autoStart = true;
|
||||
bool keepAlive = false;
|
||||
bool timeoutSet = false;
|
||||
int termTimeout = 60000;
|
||||
|
@ -152,6 +153,7 @@ int main(int argc, char *argv[])
|
|||
QStringList argumentList;
|
||||
|
||||
const QByteArray envEnvironmentMode = qgetenv("SMSUB_ENVIRONMENT_MODE");
|
||||
const QByteArray envAutoStart = qgetenv("SMSUB_AUTOSTART");
|
||||
const QByteArray envKeepAlive = qgetenv("SMSUB_KEEPALIVE");
|
||||
const QByteArray envManifest = qgetenv("SMSUB_JSON");
|
||||
const QByteArray envExecutable = qgetenv("SMSUB_EXEC");
|
||||
|
@ -204,6 +206,12 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
if (!envAutoStart.isEmpty()) {
|
||||
if (envAutoStart == "0" || envAutoStart.toLower() == "false" || envAutoStart.toLower() == "no") {
|
||||
autoStart = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!envKeepAlive.isEmpty()) {
|
||||
if (envKeepAlive == "1" || envKeepAlive.toLower() == "true" || envKeepAlive.toLower() == "yes") {
|
||||
keepAlive = true;
|
||||
|
@ -342,6 +350,12 @@ int main(int argc, char *argv[])
|
|||
executable = QString::fromUtf8(envExecutable);
|
||||
}
|
||||
|
||||
if (!envAutoStart.isEmpty()) {
|
||||
if (envAutoStart == "0" || envAutoStart.toLower() == "false" || envAutoStart.toLower() == "no") {
|
||||
autoStart = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!envKeepAlive.isEmpty()) {
|
||||
if (envKeepAlive == "1" || envKeepAlive.toLower() == "true" || envKeepAlive.toLower() == "yes") {
|
||||
keepAlive = true;
|
||||
|
@ -385,6 +399,16 @@ int main(int argc, char *argv[])
|
|||
return 1;
|
||||
}
|
||||
|
||||
if (jsonObject.contains("AutoStart")) {
|
||||
const QJsonValue jsonAutoStart = jsonObject.value("AutoStart");
|
||||
if (!jsonAutoStart.isBool()) {
|
||||
QTextStream(stderr) << "AutoStart is not a bool in manifest, aborting!" << smsub_endl;
|
||||
manifestFile.close();
|
||||
return 1;
|
||||
}
|
||||
autoStart = jsonAutoStart.toBool();
|
||||
}
|
||||
|
||||
if (jsonObject.contains("KeepAlive")) {
|
||||
const QJsonValue jsonKeepAlive = jsonObject.value("KeepAlive");
|
||||
if (!jsonKeepAlive.isBool()) {
|
||||
|
@ -562,6 +586,7 @@ int main(int argc, char *argv[])
|
|||
QObject::connect(&subLocal, &SMSubServer::killRequested, &subProcess, &SMSubProcess::killProcess);
|
||||
QObject::connect(&a, &QCoreApplication::aboutToQuit, &subProcess, &SMSubProcess::aboutToQuit);
|
||||
|
||||
if (autoStart)
|
||||
subProcess.startProcess();
|
||||
|
||||
QTextStream(stderr) << QString("SMSub Version %1 initialized!").arg(QCoreApplication::applicationVersion()) << smsub_endl;
|
||||
|
|
Loading…
Reference in a new issue