Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kiwix-serve can monitor and reload the library automatically #503

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 45 additions & 5 deletions src/server/kiwix-serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# include <unistd.h>
# include <signal.h>
#endif
#include <sys/stat.h>

#ifdef __APPLE__
# import <sys/sysctl.h>
Expand Down Expand Up @@ -98,7 +99,7 @@ string loadCustomTemplate (string customIndexPath) {
}

volatile sig_atomic_t waiting = false;
volatile sig_atomic_t library_reload_requested = false;
volatile sig_atomic_t libraryMustBeReloaded = false;

#ifndef _WIN32
void handle_sigterm(int signum)
Expand All @@ -111,7 +112,7 @@ void handle_sigterm(int signum)

void handle_sighup(int signum)
{
library_reload_requested = true;
libraryMustBeReloaded = true;
}

typedef void (*SignalHandler)(int);
Expand All @@ -132,6 +133,31 @@ void setup_sighandlers()
}
#endif

uint64_t fileModificationTime(const std::string& path)
{
#if defined(_WIN32)
#define stat _stat
#endif
struct stat fileStatData;
if ( stat(path.c_str(), &fileStatData) == 0 ) {
return fileStatData.st_mtime;
}
return 0;
#ifdef _WIN32
#undef stat
#endif
}

uint64_t newestFileTimestamp(const std::vector<std::string>& paths)
{
uint64_t t = 0;
for ( const auto& p : paths ) {
t = std::max(t, fileModificationTime(p));
}

return t;
}

bool reloadLibrary(kiwix::Manager& mgr, const std::vector<std::string>& paths)
{
try {
Expand Down Expand Up @@ -172,6 +198,7 @@ int main(int argc, char** argv)
bool noDateAliasesFlag = false;
bool blockExternalLinks = false;
bool isVerboseFlag = false;
bool monitorLibrary = false;
unsigned int PPID = 0;

static struct option long_options[]
Expand All @@ -189,13 +216,14 @@ int main(int argc, char** argv)
{"threads", required_argument, 0, 't'},
{"urlRootLocation", required_argument, 0, 'r'},
{"customIndex", required_argument, 0, 'c'},
{"monitorLibrary", no_argument, 0, 'M'},
{0, 0, 0, 0}};

/* Argument parsing */
while (true) {
int option_index = 0;
int c
= getopt_long(argc, argv, "zmnbdvVla:p:f:t:r:i:c:", long_options, &option_index);
= getopt_long(argc, argv, "zmnbdvVla:p:f:t:r:i:c:M", long_options, &option_index);

if (c != -1) {
switch (c) {
Expand Down Expand Up @@ -241,6 +269,9 @@ int main(int argc, char** argv)
case 'c':
customIndexPath = string(optarg);
break;
case 'M':
monitorLibrary = true;
break;
}
} else {
if (optind < argc) {
Expand Down Expand Up @@ -285,6 +316,8 @@ int main(int argc, char** argv)
}
}
}
auto libraryFileTimestamp = newestFileTimestamp(libraryPaths);
auto curLibraryFileTimestamp = libraryFileTimestamp;

#ifndef _WIN32
/* Fork if necessary */
Expand Down Expand Up @@ -361,10 +394,17 @@ int main(int argc, char** argv)
}

kiwix::sleep(1000);
if ( library_reload_requested && !libraryPaths.empty() ) {

if ( monitorLibrary ) {
curLibraryFileTimestamp = newestFileTimestamp(libraryPaths);
libraryMustBeReloaded += curLibraryFileTimestamp > libraryFileTimestamp;
}

if ( libraryMustBeReloaded && !libraryPaths.empty() ) {
libraryFileTimestamp = curLibraryFileTimestamp;
reloadLibrary(manager, libraryPaths);
nameMapper.update();
library_reload_requested = false;
libraryMustBeReloaded = false;
}
} while (waiting);

Expand Down