qswiki: Add background worker, currently to clean old sessions mainly

This commit is contained in:
Albert S. 2023-02-01 18:55:13 +01:00
parent b9595bd513
commit 004665e943

View File

@ -31,8 +31,8 @@ SOFTWARE.
#include "handlers/handlerfactory.h"
#include "database/databasefactory.h"
#include "config.h"
#include "template.h"
#include "session.h"
#include "template.h"
#include "logger.h"
#include "urlprovider.h"
#include "requestworker.h"
@ -74,6 +74,33 @@ std::unique_ptr<ICache> createCache(const ConfigVariableResolver &resolver)
return std::make_unique<FsCache>(path);
}
std::thread background_worker;
void start_background_worker(Database &database, Config &config)
{
background_worker = std::thread(
[&database, &config]()
{
while(true)
{
Logger::log() << "Executing background worker";
auto sessionDao = database.createSessionDao();
auto sessionList = sessionDao->fetch();
time_t now = time(NULL);
for(Session &sess : sessionList)
{
if(now - sess.creation_time > config.session_max_lifetime)
{
sessionDao->deleteSession(sess.token);
}
}
std::this_thread::sleep_for(std::chrono::hours(1));
}
});
}
int main(int argc, char **argv)
{
@ -136,6 +163,7 @@ int main(int argc, char **argv)
Logger::setStream(&logstream);
auto database = createDatabase(config);
std::string socketPath = config.configVarResolver.getConfig("socketpath");
CLIHandler cliHandler(config, *database);
@ -158,6 +186,8 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE);
}
start_background_worker(*database.get(), config);
CLIServer cliServer{cliHandler};
if(!cliServer.detachServer(socketPath))
{