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:
parent
659a8b0e8b
commit
79123e4cd9
|
@ -651,7 +651,7 @@ void ServiceSetup::initStorage()
|
||||||
config.markAllDatabasesToBeDiscarded();
|
config.markAllDatabasesToBeDiscarded();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServiceSetup::run()
|
int ServiceSetup::run()
|
||||||
{
|
{
|
||||||
#ifndef CPP_UTILITIES_DEBUG_BUILD
|
#ifndef CPP_UTILITIES_DEBUG_BUILD
|
||||||
try {
|
try {
|
||||||
|
@ -662,8 +662,10 @@ void ServiceSetup::run()
|
||||||
#ifndef CPP_UTILITIES_DEBUG_BUILD
|
#ifndef CPP_UTILITIES_DEBUG_BUILD
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
cerr << Phrases::SubError << e.what() << endl;
|
cerr << Phrases::SubError << e.what() << endl;
|
||||||
|
return -1;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
cerr << Phrases::SubError << "An unknown error occurred." << endl;
|
cerr << Phrases::SubError << "An unknown error occurred." << endl;
|
||||||
|
return -2;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -686,22 +688,37 @@ void ServiceSetup::run()
|
||||||
#ifndef CPP_UTILITIES_DEBUG_BUILD
|
#ifndef CPP_UTILITIES_DEBUG_BUILD
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
cerr << Phrases::ErrorMessage << "Server terminated due to exception: " << Phrases::End << " " << e.what() << Phrases::EndFlush;
|
cerr << Phrases::ErrorMessage << "Server terminated due to exception: " << Phrases::End << " " << e.what() << Phrases::EndFlush;
|
||||||
|
return -3;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
cerr << Phrases::ErrorMessage << "Server terminated due to an unknown error." << Phrases::EndFlush;
|
cerr << Phrases::ErrorMessage << "Server terminated due to an unknown error." << Phrases::EndFlush;
|
||||||
|
return -4;
|
||||||
}
|
}
|
||||||
#endif
|
#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) {
|
return 0;
|
||||||
if (buildAction.isExecuting()) {
|
|
||||||
buildAction.status = BuildActionStatus::Finished;
|
|
||||||
buildAction.result = BuildActionResult::Aborted;
|
|
||||||
save = true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServiceSetup::Locks::clear()
|
void ServiceSetup::Locks::clear()
|
||||||
|
|
|
@ -53,7 +53,7 @@ struct LIBREPOMGR_EXPORT ServiceSetup : public LibPkg::Lockable {
|
||||||
void restoreState();
|
void restoreState();
|
||||||
std::size_t saveState();
|
std::size_t saveState();
|
||||||
void initStorage();
|
void initStorage();
|
||||||
void run();
|
int run();
|
||||||
ServiceStatus computeStatus() const;
|
ServiceStatus computeStatus() const;
|
||||||
|
|
||||||
// variables relevant for the web server; only changed when (re)loading config
|
// variables relevant for the web server; only changed when (re)loading config
|
||||||
|
|
|
@ -23,7 +23,8 @@ int main(int argc, const char *argv[])
|
||||||
SET_APPLICATION_INFO;
|
SET_APPLICATION_INFO;
|
||||||
|
|
||||||
// define default server setup
|
// define default server setup
|
||||||
ServiceSetup setup;
|
auto exitCode = 0;
|
||||||
|
auto setup = ServiceSetup();
|
||||||
|
|
||||||
// read cli args
|
// read cli args
|
||||||
ArgumentParser parser;
|
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");
|
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.setSubArguments({ &configFileArg, &forceLoadingDBsArg });
|
||||||
runArg.setImplicit(true);
|
runArg.setImplicit(true);
|
||||||
runArg.setCallback([&setup, &configFileArg, &forceLoadingDBsArg](const ArgumentOccurrence &) {
|
runArg.setCallback([&setup, &exitCode, &configFileArg, &forceLoadingDBsArg](const ArgumentOccurrence &) {
|
||||||
if (const auto configFilePath = configFileArg.firstValue()) {
|
if (const auto configFilePath = configFileArg.firstValue()) {
|
||||||
setup.configFilePath = configFilePath;
|
setup.configFilePath = configFilePath;
|
||||||
}
|
}
|
||||||
setup.building.forceLoadingDbs = forceLoadingDBsArg.isPresent();
|
setup.building.forceLoadingDbs = forceLoadingDBsArg.isPresent();
|
||||||
setup.run();
|
exitCode = setup.run();
|
||||||
});
|
});
|
||||||
HelpArgument helpArg(parser);
|
HelpArgument helpArg(parser);
|
||||||
NoColorArgument noColorArg;
|
NoColorArgument noColorArg;
|
||||||
parser.setMainArguments({ &runArg, &noColorArg, &helpArg });
|
parser.setMainArguments({ &runArg, &noColorArg, &helpArg });
|
||||||
parser.setDefaultArgument(&runArg);
|
parser.setDefaultArgument(&runArg);
|
||||||
parser.parseArgs(argc, argv);
|
parser.parseArgs(argc, argv);
|
||||||
return 0;
|
return exitCode;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue