From 2308f01de979a90fcfeed0d9ef799e82e76c392c Mon Sep 17 00:00:00 2001 From: Jake Hunsaker Date: Fri, 22 Mar 2019 15:19:44 -0400 Subject: [InitSystem] Cache service status information With the addition of predicates, there is the very real possibility that plugins can end up gating multiple add_cmd_output() or similar calls around the same predicate, such as service enablement. Previously, this meant every single check of the predicate resulted in a new InitSystem._query_service() call was made. Now, we only call _query_service() once and save the results, since status is unlikely to change during the course of plugin execution. Resolves: #1629 Signed-off-by: Jake Hunsaker Signed-off-by: Bryn M. Reeves --- sos/policies/__init__.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py index 07e07970..bd9ea4b1 100644 --- a/sos/policies/__init__.py +++ b/sos/policies/__init__.py @@ -124,17 +124,22 @@ class InitSystem(object): """Returns the status for the given service name along with the output of the query command """ + _default = { + 'name': name, + 'status': 'missing', + 'output': '' + } + if name not in self.services: + return _default + if 'status' in self.services[name]: + # service status has been queried before, return existing info + return self.services[name] svc = self._query_service(name) if svc is not None: - return {'name': name, - 'status': self.parse_query(svc['output']), - 'output': svc['output'] - } - else: - return {'name': name, - 'status': 'missing', - 'output': '' - } + self.services[name]['status'] = self.parse_query(svc['output']) + self.services[name]['output'] = svc['output'] + return self.services[name] + return _default class SystemdInit(InitSystem): -- cgit