Skip to content

Commit 0603267

Browse files
rasapaladtrawins
andauthored
Registry read and service errors handling (#3766)
Ticket:CVS-175101 Co-authored-by: Trawinski, Dariusz <dariusz.trawinski@intel.com>
1 parent 776c35d commit 0603267

File tree

11 files changed

+457
-135
lines changed

11 files changed

+457
-135
lines changed

src/capi_frontend/capi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ DLL_PUBLIC OVMS_Status* OVMS_ServerStartFromConfigurationFile(OVMS_Server* serve
370370
ovms::Server* srv = reinterpret_cast<ovms::Server*>(server);
371371
ovms::ServerSettingsImpl* serverSettings = reinterpret_cast<ovms::ServerSettingsImpl*>(server_settings);
372372
ovms::ModelsSettingsImpl* modelsSettings = reinterpret_cast<ovms::ModelsSettingsImpl*>(models_settings);
373-
auto res = srv->start(serverSettings, modelsSettings);
373+
auto res = srv->startFromSettings(serverSettings, modelsSettings);
374374
if (res.ok()) {
375375
std::atexit(server_atexit_handler);
376376
return nullptr;

src/cli_parser.cpp

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ std::string getConfigPath(const std::string& configPath) {
5252
return configPath;
5353
}
5454

55-
void CLIParser::parse(int argc, char** argv) {
55+
std::variant<bool, std::pair<int, std::string>> CLIParser::parse(int argc, char** argv) {
56+
std::stringstream ss;
5657
try {
5758
options = std::make_unique<cxxopts::Options>(argv[0], "OpenVINO Model Server");
5859
auto configOptions = std::make_unique<cxxopts::Options>("ovms --model_name <MODEL_NAME> --add_to_config <CONFIG_PATH> --model_repository_path <MODEL_REPO_PATH> \n ovms --model_path <MODEL_PATH> --model_name <MODEL_NAME> --add_to_config <CONFIG_PATH> \n ovms --remove_from_config <CONFIG_PATH> --model_name <MODEL_NAME>", "config management commands:");
@@ -353,78 +354,78 @@ void CLIParser::parse(int argc, char** argv) {
353354
break;
354355
}
355356
case UNKNOWN_GRAPH: {
356-
std::cerr << "error parsing options - --task parameter unsupported value: " + result->operator[]("task").as<std::string>();
357-
exit(OVMS_EX_USAGE);
357+
ss << "error parsing options - --task parameter unsupported value: " + result->operator[]("task").as<std::string>();
358+
return std::make_pair(OVMS_EX_USAGE, ss.str());
358359
}
359360
}
360361
} else {
361-
std::cerr << "error parsing options - --task parameter wasn't passed";
362-
exit(OVMS_EX_USAGE);
362+
ss << "error parsing options - --task parameter wasn't passed";
363+
return std::make_pair(OVMS_EX_USAGE, ss.str());
363364
}
364365

365366
if (unmatchedOptions.size()) {
366-
std::cerr << "task: " << enumToString(task) << " - error parsing options - unmatched arguments : ";
367+
ss << "task: " << enumToString(task) << " - error parsing options - unmatched arguments : ";
367368
for (auto& argument : unmatchedOptions) {
368-
std::cerr << argument << ", ";
369+
ss << argument << ", ";
369370
}
370-
std::cerr << std::endl;
371-
exit(OVMS_EX_USAGE);
371+
ss << std::endl;
372+
return std::make_pair(OVMS_EX_USAGE, ss.str());
372373
}
373374
} else if (result->unmatched().size()){
374-
std::cerr << "error parsing options - unmatched arguments: ";
375+
ss << "error parsing options - unmatched arguments: ";
375376
for (auto& argument : result->unmatched()) {
376-
std::cerr << argument << ", ";
377+
ss << argument << ", ";
377378
}
378-
std::cerr << std::endl;
379-
exit(OVMS_EX_USAGE);
379+
ss << std::endl;
380+
return std::make_pair(OVMS_EX_USAGE, ss.str());
380381
}
381382
if (isHFPullOrPullAndStart(this->result) && result->count("list_models")) {
382-
std::cerr << "error parsing options - --list_models cannot be used with --pull or --task" << std::endl;
383-
exit(OVMS_EX_USAGE);
383+
ss << "error parsing options - --list_models cannot be used with --pull or --task" << std::endl;
384+
return std::make_pair(OVMS_EX_USAGE, ss.str());
384385
}
385386
if (isHFPullOrPullAndStart(this->result) && result->count("remove_from_config")) {
386-
std::cerr << "error parsing options - --remove_from_config cannot be used with --pull or --task" << std::endl;
387-
exit(OVMS_EX_USAGE);
387+
ss << "error parsing options - --remove_from_config cannot be used with --pull or --task" << std::endl;
388+
return std::make_pair(OVMS_EX_USAGE, ss.str());
388389
}
389390
if (isHFPullOrPullAndStart(this->result) && result->count("add_to_config")) {
390-
std::cerr << "error parsing options - --add_to_config cannot be used with --pull or --task" << std::endl;
391-
exit(OVMS_EX_USAGE);
391+
ss << "error parsing options - --add_to_config cannot be used with --pull or --task" << std::endl;
392+
return std::make_pair(OVMS_EX_USAGE, ss.str());
392393
}
393394
if (result->count("add_to_config") && result->count("list_models")) {
394-
std::cerr << "error parsing options - --list_models cannot be used with --add_to_config" << std::endl;
395-
exit(OVMS_EX_USAGE);
395+
ss << "error parsing options - --list_models cannot be used with --add_to_config" << std::endl;
396+
return std::make_pair(OVMS_EX_USAGE, ss.str());
396397
}
397398
if (result->count("remove_from_config") && result->count("list_models")) {
398-
std::cerr << "error parsing options - --list_models cannot be used with --remove_from_config" << std::endl;
399-
exit(OVMS_EX_USAGE);
399+
ss << "error parsing options - --list_models cannot be used with --remove_from_config" << std::endl;
400+
return std::make_pair(OVMS_EX_USAGE, ss.str());
400401
}
401402
if (result->count("add_to_config") && result->count("model_repository_path") && result->count("model_path")) {
402-
std::cerr << "error parsing options - --model_repository_path cannot be used with --model_path" << std::endl;
403-
exit(OVMS_EX_USAGE);
403+
ss << "error parsing options - --model_repository_path cannot be used with --model_path" << std::endl;
404+
return std::make_pair(OVMS_EX_USAGE, ss.str());
404405
}
405406
if (result->count("remove_from_config") && result->count("model_repository_path")) {
406-
std::cerr << "error parsing options - --model_repository_path cannot be used with --remove_from_config" << std::endl;
407-
exit(OVMS_EX_USAGE);
407+
ss << "error parsing options - --model_repository_path cannot be used with --remove_from_config" << std::endl;
408+
return std::make_pair(OVMS_EX_USAGE, ss.str());
408409
}
409410
if (result->count("remove_from_config") && result->count("model_path")) {
410-
std::cerr << "error parsing options - --model_path cannot be used with --remove_from_config" << std::endl;
411-
exit(OVMS_EX_USAGE);
411+
ss << "error parsing options - --model_path cannot be used with --remove_from_config" << std::endl;
412+
return std::make_pair(OVMS_EX_USAGE, ss.str());
412413
}
413414
#pragma warning(push)
414415
#pragma warning(disable : 4129)
415416
if (result->count("version")) {
416417
std::string project_name(PROJECT_NAME);
417418
std::string project_version(PROJECT_VERSION);
418-
std::cout << project_name + " " + project_version << std::endl;
419-
std::cout << "OpenVINO backend " << OPENVINO_NAME << std::endl;
420-
std::cout << "Bazel build flags: " << BAZEL_BUILD_FLAGS << std::endl;
419+
ss << project_name + " " + project_version << std::endl;
420+
ss << "OpenVINO backend " << OPENVINO_NAME << std::endl;
421+
ss << "Bazel build flags: " << BAZEL_BUILD_FLAGS << std::endl;
421422
#pragma warning(pop)
422-
exit(OVMS_EX_OK);
423+
return std::make_pair(OVMS_EX_OK, ss.str());
423424
}
424425

425426
if (result->count("help") || result->arguments().size() == 0) {
426-
std::cout << options->help({"", "multi model", "single model", "pull hf model"}) << std::endl;
427-
std::cout << configOptions->help({CONFIG_MANAGEMENT_HELP_GROUP}) << std::endl;
427+
ss << options->help({"", "multi model", "single model", "pull hf model"}) << std::endl;
428+
ss << configOptions->help({CONFIG_MANAGEMENT_HELP_GROUP}) << std::endl;
428429
GraphCLIParser parser1;
429430
RerankGraphCLIParser parser2;
430431
EmbeddingsGraphCLIParser parser3;
@@ -435,11 +436,13 @@ void CLIParser::parse(int argc, char** argv) {
435436
parser2.printHelp();
436437
parser3.printHelp();
437438
imageGenParser.printHelp();
438-
exit(OVMS_EX_OK);
439+
return std::make_pair(OVMS_EX_OK, ss.str());
439440
}
441+
442+
return true;
440443
} catch (const std::exception& e) {
441-
std::cerr << "error parsing options: " << e.what() << std::endl;
442-
exit(OVMS_EX_USAGE);
444+
ss << "error parsing options: " << e.what() << std::endl;
445+
return std::make_pair(OVMS_EX_USAGE, ss.str());
443446
}
444447
}
445448

@@ -532,8 +535,7 @@ void CLIParser::prepareServer(ServerSettingsImpl& serverSettings) {
532535
}
533536
file.close();
534537
} else {
535-
std::cerr << "Error reading API key file: Unable to open file " << apiKeyFile << std::endl;
536-
exit(OVMS_EX_USAGE);
538+
throw std::filesystem::filesystem_error("Error reading API key file: Unable to open file ", apiKeyFile, std::error_code(2, std::generic_category()));
537539
}
538540
} else {
539541
const char* envApiKey = std::getenv(API_KEY_ENV_VAR);

src/cli_parser.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <memory>
1919
#include <string>
20+
#include <utility>
2021
#include <variant>
2122

2223
#include <cxxopts.hpp>
@@ -40,7 +41,7 @@ class CLIParser {
4041

4142
public:
4243
CLIParser() = default;
43-
void parse(int argc, char** argv);
44+
std::variant<bool, std::pair<int, std::string>> parse(int argc, char** argv);
4445
void prepare(ServerSettingsImpl*, ModelsSettingsImpl*);
4546

4647
protected:

src/config.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <filesystem>
1919
#include <limits>
2020
#include <regex>
21+
#include <utility>
2122
#include <thread>
2223
#include <vector>
2324

@@ -49,11 +50,21 @@ const size_t DEFAULT_GRPC_MEMORY_QUOTA = (size_t)2 * 1024 * 1024 * 1024; // 2GB
4950
const uint64_t MAX_REST_WORKERS = 10'000;
5051

5152
Config& Config::parse(int argc, char** argv) {
52-
ovms::CLIParser p;
53+
ovms::CLIParser parser;
5354
ovms::ServerSettingsImpl serverSettings;
5455
ovms::ModelsSettingsImpl modelsSettings;
55-
p.parse(argc, argv);
56-
p.prepare(&serverSettings, &modelsSettings);
56+
auto successOrExit = parser.parse(argc, argv);
57+
// Check for error in parsing
58+
if (std::holds_alternative<std::pair<int, std::string>>(successOrExit)) {
59+
auto printAndExit = std::get<std::pair<int, std::string>>(successOrExit);
60+
if (printAndExit.first > 0) {
61+
std::cerr << printAndExit.second;
62+
} else {
63+
std::cout << printAndExit.second;
64+
}
65+
exit(printAndExit.first);
66+
}
67+
parser.prepare(&serverSettings, &modelsSettings);
5768
if (!this->parse(&serverSettings, &modelsSettings))
5869
exit(OVMS_EX_USAGE);
5970
return *this;

src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ int main(int argc, char** argv) {
2222
return server.start(argc, argv);
2323
}
2424
#elif _WIN32
25+
#pragma warning(push)
26+
#pragma warning(disable : 6553)
2527
#include "main_windows.hpp"
28+
#pragma warning(pop)
2629

2730
int main(int argc, char** argv) {
2831
return ovms_service::main_windows(argc, argv);

0 commit comments

Comments
 (0)