Improve error handling of server

* Return with non-zero exit code when an exception occurred
* Handle exceptions when terminating the service
This commit is contained in:
Martchus 2022-03-01 00:39:50 +01:00
parent 659a8b0e8b
commit 79123e4cd9
3 changed files with 33 additions and 15 deletions

View File

@ -651,7 +651,7 @@ void ServiceSetup::initStorage()
config.markAllDatabasesToBeDiscarded();
}
void ServiceSetup::run()
int ServiceSetup::run()
{
#ifndef CPP_UTILITIES_DEBUG_BUILD
try {
@ -662,8 +662,10 @@ void ServiceSetup::run()
#ifndef CPP_UTILITIES_DEBUG_BUILD
} catch (const std::exception &e) {
cerr << Phrases::SubError << e.what() << endl;
return -1;
} catch (...) {
cerr << Phrases::SubError << "An unknown error occurred." << endl;
return -2;
}
#endif
@ -686,22 +688,37 @@ void ServiceSetup::run()
#ifndef CPP_UTILITIES_DEBUG_BUILD
} catch (const std::exception &e) {
cerr << Phrases::ErrorMessage << "Server terminated due to exception: " << Phrases::End << " " << e.what() << Phrases::EndFlush;
return -3;
} catch (...) {
cerr << Phrases::ErrorMessage << "Server terminated due to an unknown error." << Phrases::EndFlush;
return -4;
}
#endif
}
saveState();
#ifndef CPP_UTILITIES_DEBUG_BUILD
try {
#endif
saveState();
building.forEachBuildAction([](LibPkg::StorageID, BuildAction &buildAction, bool &save) {
if (buildAction.isExecuting()) {
buildAction.status = BuildActionStatus::Finished;
buildAction.result = BuildActionResult::Aborted;
save = true;
}
return false;
});
#ifndef CPP_UTILITIES_DEBUG_BUILD
} catch (const std::exception &e) {
cerr << Phrases::ErrorMessage << "Exception occurred when terminating server: " << Phrases::End << " " << e.what() << Phrases::EndFlush;
return -5;
} catch (...) {
cerr << Phrases::ErrorMessage << "Unknown error occurred when terminating server." << Phrases::EndFlush;
return -6;
}
#endif
building.forEachBuildAction([](LibPkg::StorageID, BuildAction &buildAction, bool &save) {
if (buildAction.isExecuting()) {
buildAction.status = BuildActionStatus::Finished;
buildAction.result = BuildActionResult::Aborted;
save = true;
}
return false;
});
return 0;
}
void ServiceSetup::Locks::clear()

View File

@ -53,7 +53,7 @@ struct LIBREPOMGR_EXPORT ServiceSetup : public LibPkg::Lockable {
void restoreState();
std::size_t saveState();
void initStorage();
void run();
int run();
ServiceStatus computeStatus() const;
// variables relevant for the web server; only changed when (re)loading config

View File

@ -23,7 +23,8 @@ int main(int argc, const char *argv[])
SET_APPLICATION_INFO;
// define default server setup
ServiceSetup setup;
auto exitCode = 0;
auto setup = ServiceSetup();
// read cli args
ArgumentParser parser;
@ -33,17 +34,17 @@ int main(int argc, const char *argv[])
ConfigValueArgument forceLoadingDBsArg("force-loading-dbs", 'f', "forces loading DBs, even if DB files have not been modified since last parse");
runArg.setSubArguments({ &configFileArg, &forceLoadingDBsArg });
runArg.setImplicit(true);
runArg.setCallback([&setup, &configFileArg, &forceLoadingDBsArg](const ArgumentOccurrence &) {
runArg.setCallback([&setup, &exitCode, &configFileArg, &forceLoadingDBsArg](const ArgumentOccurrence &) {
if (const auto configFilePath = configFileArg.firstValue()) {
setup.configFilePath = configFilePath;
}
setup.building.forceLoadingDbs = forceLoadingDBsArg.isPresent();
setup.run();
exitCode = setup.run();
});
HelpArgument helpArg(parser);
NoColorArgument noColorArg;
parser.setMainArguments({ &runArg, &noColorArg, &helpArg });
parser.setDefaultArgument(&runArg);
parser.parseArgs(argc, argv);
return 0;
return exitCode;
}