Skip to content

Commit

Permalink
mgr: refactor get_modules/list_modules
Browse files Browse the repository at this point in the history
list_modules is really about searching for them
on disk, so it's now probe_modules and private.

Both methods now return values instead of populating
an argument, since when called they were always writing into
a newly constructed container.

Signed-off-by: John Spray <john.spray@redhat.com>
  • Loading branch information
John Spray committed Dec 18, 2017
1 parent 4721786 commit 0f41a0d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/mgr/MgrStandby.cc
Expand Up @@ -152,8 +152,7 @@ void MgrStandby::send_beacon()
assert(lock.is_locked_by_me());
dout(1) << state_str() << dendl;

std::list<PyModuleRef> modules;
py_module_registry.get_modules(&modules);
std::list<PyModuleRef> modules = py_module_registry.get_modules();

// Construct a list of the info about each loaded module
// which we will transmit to the monitor.
Expand Down
21 changes: 9 additions & 12 deletions src/mgr/PyModuleRegistry.cc
Expand Up @@ -69,8 +69,7 @@ int PyModuleRegistry::init(const MgrMap &map)

std::list<std::string> failed_modules;

std::set<std::string> module_names;
list_modules(&module_names);
std::set<std::string> module_names = probe_modules();
// Load python code
for (const auto& module_name : module_names) {
dout(1) << "Loading python module '" << module_name << "'" << dendl;
Expand Down Expand Up @@ -221,14 +220,16 @@ void PyModuleRegistry::shutdown()
Py_Finalize();
}

static void _list_modules(
const std::string path,
std::set<std::string> *modules)
std::set<std::string> PyModuleRegistry::probe_modules() const
{
std::string path = g_conf->get_val<std::string>("mgr_module_path");

DIR *dir = opendir(path.c_str());
if (!dir) {
return;
return {};
}

std::set<std::string> modules_out;
struct dirent *entry = NULL;
while ((entry = readdir(dir)) != NULL) {
string n(entry->d_name);
Expand All @@ -239,17 +240,13 @@ static void _list_modules(
string initfn = fn + "/module.py";
r = ::stat(initfn.c_str(), &st);
if (r == 0) {
modules->insert(n);
modules_out.insert(n);
}
}
}
closedir(dir);
}

void PyModuleRegistry::list_modules(std::set<std::string> *modules)
{
g_conf->with_val<std::string>("mgr_module_path",
&_list_modules, modules);
return modules_out;
}

int PyModuleRegistry::handle_command(
Expand Down
18 changes: 14 additions & 4 deletions src/mgr/PyModuleRegistry.h
Expand Up @@ -54,17 +54,27 @@ class PyModuleRegistry
// before ClusterState exists.
MgrMap mgr_map;

/**
* Discover python modules from local disk
*/
std::set<std::string> probe_modules() const;

public:
static std::string config_prefix;

void list_modules(std::set<std::string> *modules);

void get_modules(std::list<PyModuleRef> *modules_out)
/**
* Get references to all modules (whether they have loaded and/or
* errored) or not.
*/
std::list<PyModuleRef> get_modules() const
{
Mutex::Locker l(lock);
std::list<PyModuleRef> modules_out;
for (const auto &i : modules) {
modules_out->push_back(i.second);
modules_out.push_back(i.second);
}

return modules_out;
}

PyModuleRegistry(LogChannelRef clog_)
Expand Down

0 comments on commit 0f41a0d

Please sign in to comment.