From 860aac3e503641168cba0f325eeaede33c6c1936 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 11 Jun 2017 09:54:09 +0800 Subject: [PATCH] For #913, config reload and persistence --- trunk/src/app/srs_app_config.cpp | 80 ++++++++++++++++---------------- trunk/src/app/srs_app_config.hpp | 8 ++-- trunk/src/app/srs_app_server.cpp | 9 ++-- 3 files changed, 48 insertions(+), 49 deletions(-) diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index b2b767dc5c..5d3c539bec 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -793,9 +793,10 @@ int SrsConfDirective::parse(SrsConfigBuffer* buffer) return parse_conf(buffer, parse_file); } -int SrsConfDirective::persistence(SrsFileWriter* writer, int level) +srs_error_t SrsConfDirective::persistence(SrsFileWriter* writer, int level) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; static char SPACE = SRS_CONSTS_SP; static char SEMICOLON = SRS_CONSTS_SE; @@ -809,33 +810,33 @@ int SrsConfDirective::persistence(SrsFileWriter* writer, int level) // indent by (level - 1) * 4 space. for (int i = 0; i < level - 1; i++) { if ((ret = writer->write((char*)INDENT, 4, NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write indent"); } } // directive name. if ((ret = writer->write((char*)name.c_str(), (int)name.length(), NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write name"); } if (!args.empty() && (ret = writer->write((char*)&SPACE, 1, NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write name space"); } // directive args. for (int i = 0; i < (int)args.size(); i++) { std::string& arg = args.at(i); if ((ret = writer->write((char*)arg.c_str(), (int)arg.length(), NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write arg"); } if (i < (int)args.size() - 1 && (ret = writer->write((char*)&SPACE, 1, NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write arg space"); } } // native directive, without sub directives. if (directives.empty()) { if ((ret = writer->write((char*)&SEMICOLON, 1, NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write arg semicolon"); } } } @@ -844,22 +845,22 @@ int SrsConfDirective::persistence(SrsFileWriter* writer, int level) if (level > 0) { if (!directives.empty()) { if ((ret = writer->write((char*)&SPACE, 1, NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write sub-dir space"); } if ((ret = writer->write((char*)&LB, 1, NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write sub-dir left-brace"); } } if ((ret = writer->write((char*)&LF, 1, NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write sub-dir linefeed"); } } for (int i = 0; i < (int)directives.size(); i++) { SrsConfDirective* dir = directives.at(i); - if ((ret = dir->persistence(writer, level + 1)) != ERROR_SUCCESS) { - return ret; + if ((err = dir->persistence(writer, level + 1)) != srs_success) { + return srs_error_wrap(err, "sub-dir %s", dir->name.c_str()); } } @@ -867,21 +868,21 @@ int SrsConfDirective::persistence(SrsFileWriter* writer, int level) // indent by (level - 1) * 4 space. for (int i = 0; i < level - 1; i++) { if ((ret = writer->write((char*)INDENT, 4, NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write sub-dir indent"); } } if ((ret = writer->write((char*)&RB, 1, NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write sub-dir right-brace"); } if ((ret = writer->write((char*)&LF, 1, NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write sub-dir linefeed"); } } - return ret; + return err; } SrsJsonArray* SrsConfDirective::dumps_args() @@ -1187,32 +1188,32 @@ void SrsConfig::unsubscribe(ISrsReloadHandler* handler) subscribes.erase(it); } -int SrsConfig::reload() +srs_error_t SrsConfig::reload() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsConfig conf; if ((ret = conf.parse_file(config_file.c_str())) != ERROR_SUCCESS) { - srs_error("ignore config reloader parse file failed. ret=%d", ret); - ret = ERROR_SUCCESS; - return ret; + return srs_error_new(ret, "parse file"); } srs_info("config reloader parse file success."); // transform config to compatible with previous style of config. if ((ret = srs_config_transform_vhost(conf.root)) != ERROR_SUCCESS) { - srs_error("transform config failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "transform config"); } if ((ret = conf.check_config()) != ERROR_SUCCESS) { - srs_error("ignore config reloader check config failed. ret=%d", ret); - ret = ERROR_SUCCESS; - return ret; + return srs_error_new(ret, "check config"); } - return reload_conf(&conf); + if ((ret = reload_conf(&conf)) != ERROR_SUCCESS) { + return srs_error_new(ret, "reload config"); + } + + return err; } int SrsConfig::reload_vhost(SrsConfDirective* old_root) @@ -2022,9 +2023,10 @@ int SrsConfig::initialize_cwd() return ret; } -int SrsConfig::persistence() +srs_error_t SrsConfig::persistence() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // write to a tmp file, then mv to the config. std::string path = config_file + ".tmp"; @@ -2032,37 +2034,35 @@ int SrsConfig::persistence() // open the tmp file for persistence SrsFileWriter fw; if ((ret = fw.open(path)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "open file"); } // do persistence to writer. - if ((ret = do_persistence(&fw)) != ERROR_SUCCESS) { + if ((err = do_persistence(&fw)) != srs_success) { ::unlink(path.c_str()); - return ret; + return srs_error_wrap(err, "persistence"); } // rename the config file. if (::rename(path.c_str(), config_file.c_str()) < 0) { ::unlink(path.c_str()); - - ret = ERROR_SYSTEM_CONFIG_PERSISTENCE; - srs_error("rename config from %s to %s failed. ret=%d", path.c_str(), config_file.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_PERSISTENCE, "rename %s=>%s", + path.c_str(), config_file.c_str()); } - return ret; + return err; } -int SrsConfig::do_persistence(SrsFileWriter* fw) +srs_error_t SrsConfig::do_persistence(SrsFileWriter* fw) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // persistence root directive to writer. - if ((ret = root->persistence(fw, 0)) != ERROR_SUCCESS) { - return ret; + if ((err = root->persistence(fw, 0)) != srs_success) { + return srs_error_wrap(err, "root persistence"); } - return ret; + return err; } int SrsConfig::minimal_to_json(SrsJsonObject* obj) diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 3ac3153624..6dacca9150 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -244,7 +244,7 @@ class SrsConfDirective * persistence the directive to writer. * @param level, the root is level0, all its directives are level1, and so on. */ - virtual int persistence(SrsFileWriter* writer, int level); + virtual srs_error_t persistence(SrsFileWriter* writer, int level); /** * dumps the args[0-N] to array(string). */ @@ -377,7 +377,7 @@ class SrsConfig * reload the config file. * @remark, user can test the config before reload it. */ - virtual int reload(); + virtual srs_error_t reload(); private: /** * reload the vhost section of config. @@ -421,9 +421,9 @@ class SrsConfig /** * persistence current config to file. */ - virtual int persistence(); + virtual srs_error_t persistence(); private: - virtual int do_persistence(SrsFileWriter* fw); + virtual srs_error_t do_persistence(SrsFileWriter* fw); public: /** * dumps the global sections to json. diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 3687f0b82c..c052fb990c 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -898,7 +898,6 @@ void SrsServer::on_signal(int signo) srs_error_t SrsServer::do_cycle() { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; // find the max loop @@ -961,8 +960,8 @@ srs_error_t SrsServer::do_cycle() signal_persistence_config = false; srs_info("get signal to persistence config to file."); - if ((ret = _srs_config->persistence()) != ERROR_SUCCESS) { - return srs_error_new(ret, "config persistence to file"); + if ((err = _srs_config->persistence()) != srs_success) { + return srs_error_wrap(err, "config persistence to file"); } srs_trace("persistence config to file success."); } @@ -972,8 +971,8 @@ srs_error_t SrsServer::do_cycle() signal_reload = false; srs_info("get signal to reload the config."); - if ((ret = _srs_config->reload()) != ERROR_SUCCESS) { - return srs_error_new(ret, "config reload"); + if ((err = _srs_config->reload()) != srs_success) { + return srs_error_wrap(err, "config reload"); } srs_trace("reload config success."); }