qswiki: Add background worker, currently to clean old sessions mainly
This commit is contained in:
parent
b9595bd513
commit
004665e943
32
qswiki.cpp
32
qswiki.cpp
@ -31,8 +31,8 @@ SOFTWARE.
|
|||||||
#include "handlers/handlerfactory.h"
|
#include "handlers/handlerfactory.h"
|
||||||
#include "database/databasefactory.h"
|
#include "database/databasefactory.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "template.h"
|
|
||||||
#include "session.h"
|
#include "session.h"
|
||||||
|
#include "template.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include "urlprovider.h"
|
#include "urlprovider.h"
|
||||||
#include "requestworker.h"
|
#include "requestworker.h"
|
||||||
@ -74,6 +74,33 @@ std::unique_ptr<ICache> createCache(const ConfigVariableResolver &resolver)
|
|||||||
return std::make_unique<FsCache>(path);
|
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)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -136,6 +163,7 @@ int main(int argc, char **argv)
|
|||||||
Logger::setStream(&logstream);
|
Logger::setStream(&logstream);
|
||||||
|
|
||||||
auto database = createDatabase(config);
|
auto database = createDatabase(config);
|
||||||
|
|
||||||
std::string socketPath = config.configVarResolver.getConfig("socketpath");
|
std::string socketPath = config.configVarResolver.getConfig("socketpath");
|
||||||
CLIHandler cliHandler(config, *database);
|
CLIHandler cliHandler(config, *database);
|
||||||
|
|
||||||
@ -158,6 +186,8 @@ int main(int argc, char **argv)
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
start_background_worker(*database.get(), config);
|
||||||
|
|
||||||
CLIServer cliServer{cliHandler};
|
CLIServer cliServer{cliHandler};
|
||||||
if(!cliServer.detachServer(socketPath))
|
if(!cliServer.detachServer(socketPath))
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user