add AutoStart option, Last Start and Last Stop

This commit is contained in:
Syping 2024-04-25 23:11:46 +02:00
parent 95db23458f
commit d37369ad4f
6 changed files with 48 additions and 16 deletions

View file

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.5) 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) set(CMAKE_INCLUDE_CURRENT_DIR ON)

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* smsub Server Manager Subprocess * 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, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -18,9 +18,9 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QTextStream> #include <QTextStream>
#include <QDateTime>
#include "SMSubProcess.h" #include "SMSubProcess.h"
#include "smsub.h" #include "smsub.h"
#include <QDebug>
SMSubProcess::SMSubProcess(const QString &executable, const QStringList &arguments, const QString &workingDirectory, const int &termTimeout, const bool &keepAlive) : SMSubProcess::SMSubProcess(const QString &executable, const QStringList &arguments, const QString &workingDirectory, const int &termTimeout, const bool &keepAlive) :
termTimeout(termTimeout), keepAlive(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::errorOccurred, this, &SMSubProcess::processError);
QObject::connect(&process, &QProcess::started, this, [=]() { QObject::connect(&process, &QProcess::started, this, [=]() {
QTextStream(stderr) << "Subprocess started!" << smsub_endl; 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) { QObject::connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [=](int exitCode) {
QTextStream(stderr) << "Subprocess exited!" << smsub_endl; QTextStream(stderr) << "Subprocess exited!" << smsub_endl;
// Exit with the same exit code as the process // Exit with the same exit code as the process
emit statusUpdated(false); emit statusUpdated(false, QDateTime::currentDateTimeUtc().toSecsSinceEpoch());
if (!keepAlive) if (!keepAlive)
QCoreApplication::exit(exitCode); QCoreApplication::exit(exitCode);
}); });

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* smsub Server Manager Subprocess * 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, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -46,7 +46,7 @@ private slots:
signals: signals:
void outputWritten(const QByteArray &output); void outputWritten(const QByteArray &output);
void statusUpdated(const bool status); void statusUpdated(const bool status, const qint64 time);
}; };
#endif // SMSUBPROCESS_H #endif // SMSUBPROCESS_H

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* smsub Server Manager Subprocess * 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, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -33,6 +33,8 @@ SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &soc
type = ServerType::Local; type = ServerType::Local;
server = localServer; server = localServer;
status = false; status = false;
startTime = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
stopTime = startTime;
} }
SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &serverName, const quint16 &port) : serverSettings(serverSettings) 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; type = ServerType::WebSocket;
server = webSocketServer; server = webSocketServer;
status = false; status = false;
startTime = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
stopTime = startTime;
} }
bool SMSubServer::isListening() bool SMSubServer::isListening()
@ -126,10 +130,10 @@ bool SMSubServer::messageReceived(QObject *socket, const QByteArray &message)
} }
else if (message.startsWith("status")) { else if (message.startsWith("status")) {
if (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 { 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")) { 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 = status_;
status ? (startTime = time) : (stopTime = time);
} }

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* smsub Server Manager Subprocess * 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, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -45,7 +45,7 @@ public:
public slots: public slots:
void writeOutput(const QByteArray &output); void writeOutput(const QByteArray &output);
void registerToken(const QString &token); void registerToken(const QString &token);
void statusUpdated(const bool status); void statusUpdated(const bool status, const qint64 time);
private slots: private slots:
void wsMessageReceived(const QByteArray &message); void wsMessageReceived(const QByteArray &message);
@ -60,6 +60,8 @@ private:
SMSubServerSettings *serverSettings; SMSubServerSettings *serverSettings;
QVector<QObject*> sockets; QVector<QObject*> sockets;
QVector<QString> tokens; QVector<QString> tokens;
qint64 startTime;
qint64 stopTime;
ServerType type; ServerType type;
QObject *server; QObject *server;
bool status; bool status;

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* smsub Server Manager Subprocess * 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, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -134,13 +134,14 @@ int main(int argc, char *argv[])
{ {
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
a.setApplicationName("Server Manager Subprocess"); a.setApplicationName("Server Manager Subprocess");
a.setApplicationVersion("0.7"); a.setApplicationVersion("0.8");
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
catchUnixSignals({SIGINT, SIGHUP, SIGQUIT, SIGTERM}); catchUnixSignals({SIGINT, SIGHUP, SIGQUIT, SIGTERM});
#endif #endif
bool rportSet = false; bool rportSet = false;
bool autoStart = true;
bool keepAlive = false; bool keepAlive = false;
bool timeoutSet = false; bool timeoutSet = false;
int termTimeout = 60000; int termTimeout = 60000;
@ -152,6 +153,7 @@ int main(int argc, char *argv[])
QStringList argumentList; QStringList argumentList;
const QByteArray envEnvironmentMode = qgetenv("SMSUB_ENVIRONMENT_MODE"); const QByteArray envEnvironmentMode = qgetenv("SMSUB_ENVIRONMENT_MODE");
const QByteArray envAutoStart = qgetenv("SMSUB_AUTOSTART");
const QByteArray envKeepAlive = qgetenv("SMSUB_KEEPALIVE"); const QByteArray envKeepAlive = qgetenv("SMSUB_KEEPALIVE");
const QByteArray envManifest = qgetenv("SMSUB_JSON"); const QByteArray envManifest = qgetenv("SMSUB_JSON");
const QByteArray envExecutable = qgetenv("SMSUB_EXEC"); 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.isEmpty()) {
if (envKeepAlive == "1" || envKeepAlive.toLower() == "true" || envKeepAlive.toLower() == "yes") { if (envKeepAlive == "1" || envKeepAlive.toLower() == "true" || envKeepAlive.toLower() == "yes") {
keepAlive = true; keepAlive = true;
@ -342,6 +350,12 @@ int main(int argc, char *argv[])
executable = QString::fromUtf8(envExecutable); executable = QString::fromUtf8(envExecutable);
} }
if (!envAutoStart.isEmpty()) {
if (envAutoStart == "0" || envAutoStart.toLower() == "false" || envAutoStart.toLower() == "no") {
autoStart = false;
}
}
if (!envKeepAlive.isEmpty()) { if (!envKeepAlive.isEmpty()) {
if (envKeepAlive == "1" || envKeepAlive.toLower() == "true" || envKeepAlive.toLower() == "yes") { if (envKeepAlive == "1" || envKeepAlive.toLower() == "true" || envKeepAlive.toLower() == "yes") {
keepAlive = true; keepAlive = true;
@ -385,6 +399,16 @@ int main(int argc, char *argv[])
return 1; 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")) { if (jsonObject.contains("KeepAlive")) {
const QJsonValue jsonKeepAlive = jsonObject.value("KeepAlive"); const QJsonValue jsonKeepAlive = jsonObject.value("KeepAlive");
if (!jsonKeepAlive.isBool()) { if (!jsonKeepAlive.isBool()) {
@ -562,6 +586,7 @@ int main(int argc, char *argv[])
QObject::connect(&subLocal, &SMSubServer::killRequested, &subProcess, &SMSubProcess::killProcess); QObject::connect(&subLocal, &SMSubServer::killRequested, &subProcess, &SMSubProcess::killProcess);
QObject::connect(&a, &QCoreApplication::aboutToQuit, &subProcess, &SMSubProcess::aboutToQuit); QObject::connect(&a, &QCoreApplication::aboutToQuit, &subProcess, &SMSubProcess::aboutToQuit);
if (autoStart)
subProcess.startProcess(); subProcess.startProcess();
QTextStream(stderr) << QString("SMSub Version %1 initialized!").arg(QCoreApplication::applicationVersion()) << smsub_endl; QTextStream(stderr) << QString("SMSub Version %1 initialized!").arg(QCoreApplication::applicationVersion()) << smsub_endl;