Changeset - 8dfcbc87728f
[Not reviewed]
0 4 0
HanzZ - 13 years ago 2012-09-11 09:13:18
hanzz.k@gmail.com
Return proper exit code in spectrum2_manager when instance starts
4 files changed with 22 insertions and 14 deletions:
0 comments (0 inline, 0 general)
spectrum/src/main.cpp
Show inline comments
 
@@ -312,13 +312,12 @@ int main(int argc, char **argv)
 
		// daemonize
 
		daemonize(CONFIG_STRING(&config, "service.working_dir").c_str(), CONFIG_STRING(&config, "service.pidfile").c_str());
 
// 		removeOldIcons(CONFIG_STRING(&config, "service.working_dir") + "/icons");
 
    }
 
#endif
 

	
 
	Logging::initMainLogging(&config);
 

	
 
#ifndef WIN32
 
	if (!CONFIG_STRING(&config, "service.group").empty() ||!CONFIG_STRING(&config, "service.user").empty() ) {
 
		struct rlimit limit;
 
		getrlimit(RLIMIT_CORE, &limit);
 

	
 
@@ -370,17 +369,21 @@ int main(int argc, char **argv)
 
	if (storageBackend == NULL) {
 
		if (!error.empty()) {
 
			std::cerr << error << "\n";
 
			return -2;
 
		}
 
	}
 
	else if (!storageBackend->connect()) {        
 
	else if (!storageBackend->connect()) {
 
		std::cerr << "Can't connect to database. Check the log to find out the reason.\n";
 
		return -1;
 
	}
 

	
 
	// Logging has to be initialized after all std:cerr output here, because
 
	// it forwards std::cerr to log file.
 
	Logging::initMainLogging(&config);
 

	
 
	UserManager userManager(&transport, &userRegistry, storageBackend);
 
	userManager_ = &userManager;
 

	
 
	UserRegistration *userRegistration = NULL;
 
	UsersReconnecter *usersReconnecter = NULL;
 
	if (storageBackend) {
spectrum_manager/src/main.cpp
Show inline comments
 
@@ -123,13 +123,13 @@ int main(int argc, char **argv)
 
	if (command.empty()) {
 
		std::cout << desc << "\n";
 
		return 1;
 
	}
 
 
	if (command[0] == "start") {
 
		start_instances(&config);
 
		return start_instances(&config);
 
	}
 
	else if (command[0] == "stop") {
 
		stop_instances(&config);
 
	}
 
	else if (command[0] == "status") {
 
		return show_status(&config);
 
@@ -152,14 +152,13 @@ int main(int argc, char **argv)
 
 
		std::string jid = command[0];
 
		command.erase(command.begin());
 
		std::string cmd = boost::algorithm::join(command, " ");
 
 
		if (cmd == "start") {
 
			start_instances(&config, jid);
 
			return 0;
 
			return start_instances(&config, jid);
 
		}
 
		else if (cmd == "stop") {
 
			stop_instances(&config, jid);
 
			return 0;
 
		}
 
spectrum_manager/src/methods.cpp
Show inline comments
 
@@ -69,13 +69,13 @@ std::string searchForBinary(const std::string &binary) {
 
		}
 
	}
 
	return "";
 
}
 

	
 
// Executes new backend
 
unsigned long exec_(std::string path, std::string config, std::string jid) {
 
unsigned long exec_(std::string path, std::string config, std::string jid, int &rc) {
 
	// fork and exec
 
	pid_t pid = fork();
 
	if ( pid == 0 ) {
 
		// child process
 
		if (jid.empty()) {
 
			exit(execl(path.c_str(), path.c_str(), config.c_str(), NULL));
 
@@ -84,13 +84,13 @@ unsigned long exec_(std::string path, std::string config, std::string jid) {
 
			exit(execl(path.c_str(), path.c_str(), "-j", jid.c_str(), config.c_str(), NULL));
 
		}
 
	} else if ( pid < 0 ) {
 
		// fork failed
 
	}
 
	else {
 
		waitpid(pid, 0, 0);
 
		waitpid(pid, &rc, 0);
 
	}
 

	
 
	return (unsigned long) pid;
 
}
 

	
 
int getPort(const std::string &portfile) {
 
@@ -125,31 +125,32 @@ int isRunning(const std::string &pidfile) {
 
	if (kill(boost::lexical_cast<int>(pid), 0) != 0)
 
		return 0;
 

	
 
	return boost::lexical_cast<int>(pid);
 
}
 

	
 
void start_instances(ManagerConfig *config, const std::string &_jid) {
 
int start_instances(ManagerConfig *config, const std::string &_jid) {
 
	int rv = 0;
 
	response = "";
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 

	
 
	try {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
			return 6;
 
		}
 

	
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
			return 7;
 
		}
 

	
 
		std::string spectrum2_binary = searchForBinary("spectrum2");
 
		if (spectrum2_binary.empty()) {
 
			std::cerr << "spectrum2 binary not found in PATH\n";
 
			exit(8);
 
			return 8;
 
		}
 

	
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
@@ -174,28 +175,33 @@ void start_instances(ManagerConfig *config, const std::string &_jid) {
 
					if (!_jid.empty() && _jid != vhost) {
 
						continue;
 
					}
 

	
 
					int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
 
					if (pid == 0) {
 
						int rc;
 
						response = "Starting " + itr->path().string() + ": OK\n";
 
						std::cout << "Starting " << itr->path() << ": OK\n";
 
						exec_(spectrum2_binary, itr->path().string(), vhost);
 
						exec_(spectrum2_binary, itr->path().string(), vhost, rc);
 
						if (rv == 0) {
 
							rv = rc;
 
						}
 
					}
 
					else {
 
						response = "Starting " + itr->path().string() + ": Already started (PID=" + boost::lexical_cast<std::string>(pid) + ")\n";
 
						std::cout << "Starting " << itr->path() << ": Already started (PID=" << pid << ")\n";
 
					}
 
				}
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "boost filesystem error\n";
 
		exit(5);
 
		return 6;
 
	}
 
	return rv;
 
}
 

	
 
void stop_instances(ManagerConfig *config, const std::string &_jid) {
 
	response = "";
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 

	
spectrum_manager/src/methods.h
Show inline comments
 
@@ -41,13 +41,13 @@ std::string searchForBinary(const std::string &binary);
 
// Executes new backend
 
unsigned long exec_(std::string path, std::string config, std::string jid = "");
 

	
 
int getPort(const std::string &portfile);
 

	
 
int isRunning(const std::string &pidfile);
 
void start_instances(ManagerConfig *config, const std::string &_jid = "");
 
int start_instances(ManagerConfig *config, const std::string &_jid = "");
 
void stop_instances(ManagerConfig *config, const std::string &_jid = "");
 

	
 
int show_status(ManagerConfig *config);
 

	
 
void ask_local_server(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &jid, const std::string &message);
 

	
0 comments (0 inline, 0 general)