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

Library reloading on SIGHUP #497

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions src/server/kiwix-serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ void usage()
<< "\t-V, --version\t\tprint software version" << std::endl
<< "\t-z, --nodatealiases\tcreate URL aliases for each content by removing the date" << std::endl
<< "\t-c, --customIndex\tadd path to custom index.html for welcome page" << std::endl
<< "\t--donottrustlibrary\tRead the metadata from the zim file instead of trusting the library." << std::endl
kelson42 marked this conversation as resolved.
Show resolved Hide resolved
<< std::endl

<< "Documentation:" << std::endl
Expand Down Expand Up @@ -99,6 +98,7 @@ string loadCustomTemplate (string customIndexPath) {
}

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

#ifndef _WIN32
void handle_sigterm(int signum)
Expand All @@ -109,6 +109,11 @@ void handle_sigterm(int signum)
waiting = false;
}

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

typedef void (*SignalHandler)(int);

void set_signal_handler(int sig, SignalHandler handler)
Expand All @@ -123,9 +128,27 @@ void setup_sighandlers()
{
set_signal_handler(SIGTERM, &handle_sigterm);
set_signal_handler(SIGINT, &handle_sigterm);
set_signal_handler(SIGHUP, &handle_sighup);
}
#endif

bool reloadLibrary(kiwix::Manager& mgr, const std::vector<std::string>& paths)
{
try {
std::cout << "Loading the library from the following files:\n";
for ( const auto& p : paths ) {
std::cout << "\t" << p << std::endl;
}
mgr.reload(paths);
std::cout << "The library was successfully loaded." << std::endl;
return true;
} catch ( const std::runtime_error& err ) {
std::cerr << "ERROR: " << err.what() << std::endl;
std::cerr << "Errors encountered while loading the library." << std::endl;
return false;
}
}

int main(int argc, char** argv)
{
#ifndef _WIN32
Expand All @@ -149,7 +172,6 @@ int main(int argc, char** argv)
bool noDateAliasesFlag = false;
bool blockExternalLinks = false;
bool isVerboseFlag = false;
bool trustlibrary = true;
unsigned int PPID = 0;

static struct option long_options[]
Expand All @@ -167,7 +189,6 @@ int main(int argc, char** argv)
{"threads", required_argument, 0, 't'},
{"urlRootLocation", required_argument, 0, 'r'},
{"customIndex", required_argument, 0, 'c'},
{"donottrustlibrary", no_argument, 0, 'T'},
{0, 0, 0, 0}};

/* Argument parsing */
Expand Down Expand Up @@ -202,9 +223,6 @@ int main(int argc, char** argv)
case 'm':
noLibraryButtonFlag = true;
break;
case 'T':
trustlibrary = false;
break;
case 'p':
serverPort = atoi(optarg);
break;
Expand Down Expand Up @@ -245,30 +263,11 @@ int main(int argc, char** argv)

/* Setup the library manager and get the list of books */
kiwix::Manager manager(&library);
vector<string> libraryPaths;
if (libraryFlag) {
vector<string> libraryPaths = kiwix::split(libraryPath, ";");
vector<string>::iterator itr;

for (itr = libraryPaths.begin(); itr != libraryPaths.end(); ++itr) {
if (!itr->empty()) {
bool retVal = false;

try {
string libraryPath
= kiwix::isRelativePath(*itr)
? kiwix::computeAbsolutePath(kiwix::getCurrentDirectory(), *itr)
: *itr;
retVal = manager.readFile(libraryPath, true, trustlibrary);
} catch (...) {
retVal = false;
}

if (!retVal) {
cerr << "Unable to open the XML library file '" << *itr << "'."
<< endl;
exit(1);
}
}
libraryPaths = kiwix::split(libraryPath, ";");
if ( !reloadLibrary(manager, libraryPaths) ) {
exit(1);
}

/* Check if the library is not empty (or only remote books)*/
Expand Down Expand Up @@ -306,7 +305,7 @@ int main(int argc, char** argv)
}
#endif

kiwix::HumanReadableNameMapper nameMapper(library, noDateAliasesFlag);
kiwix::UpdatableNameMapper nameMapper(library, noDateAliasesFlag);
kiwix::Server server(&library, &nameMapper);

if (!customIndexPath.empty()) {
Expand Down Expand Up @@ -362,6 +361,11 @@ int main(int argc, char** argv)
}

kiwix::sleep(1000);
if ( library_reload_requested && !libraryPaths.empty() ) {
reloadLibrary(manager, libraryPaths);
nameMapper.update();
library_reload_requested = false;
}
mgautierfr marked this conversation as resolved.
Show resolved Hide resolved
} while (waiting);

/* Stop the daemon */
Expand Down