Skip to content

Commit

Permalink
For #913, config reload and persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jun 11, 2017
1 parent 7cac35a commit 860aac3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 49 deletions.
80 changes: 40 additions & 40 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}
}
}
Expand All @@ -844,44 +845,44 @@ 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());
}
}

if (level > 0 && !directives.empty()) {
// 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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -2022,47 +2023,46 @@ 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";

// 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)
Expand Down
8 changes: 4 additions & 4 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*/
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 4 additions & 5 deletions trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.");
}
Expand All @@ -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.");
}
Expand Down

0 comments on commit 860aac3

Please sign in to comment.