-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Brewtils Plugin requires #500
Merged
Merged
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2716a7e
Beer.conf plugin requires
1maple1 a7b3f4f
Add requires to system schema
1maple1 a57ff2a
Add str require to fix requires env plugin test
1maple1 e3e5a90
Add dependency check to plugin run method
1maple1 de33d39
Update status on backoff and shutdown after timeout
1maple1 9e5a394
Fix shutdown plugin on timeout
1maple1 b84f0dd
Add requires timeout
1maple1 ee6b19b
Fix tests by updating fixture
1maple1 b249615
Missing comma
1maple1 3275ec9
Merge branch 'develop' into plugin-requires
1maple1 24d65d2
Update CHANGELOG.rst
1maple1 4cf67a4
Add wait for plugin backoff
1maple1 d9242d8
Merge branch 'plugin-requires' of https://github.com/beer-garden/brew…
1maple1 81f006f
Unique systems on local garden only for dependency check
1maple1 e1c1053
Add requires to decorators
1maple1 a2e31b6
Use system requires instead of config
1maple1 4cd4868
Attempting to fix plugin test
1maple1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,9 @@ class Plugin(object): | |
- ``display_name`` | ||
- ``group`` | ||
- ``groups`` | ||
- ``require`` | ||
- ``requires`` | ||
- ``requires_timeout`` | ||
|
||
Connection information tells the Plugin how to communicate with Beer-garden. The | ||
most important of these is the ``bg_host`` (to tell the plugin where to find the | ||
|
@@ -172,6 +175,10 @@ class Plugin(object): | |
instance_name (str): Instance name | ||
namespace (str): Namespace name | ||
|
||
require (str): Required system dependency | ||
requires (list): Required systems dependencies | ||
requires_timeout (int): Timeout to wait for dependencies | ||
|
||
group (str): Grouping label applied to plugin | ||
groups (list): Grouping labels applied to plugin | ||
|
||
|
@@ -256,20 +263,24 @@ def run(self): | |
"attribute to an instance of a class decorated with @brewtils.system" | ||
) | ||
|
||
self._startup() | ||
self._logger.info("Plugin %s has started", self.unique_name) | ||
|
||
try: | ||
# Need the timeout param so this works correctly in Python 2 | ||
while not self._shutdown_event.wait(timeout=0.1): | ||
pass | ||
except KeyboardInterrupt: | ||
self._logger.debug("Received KeyboardInterrupt - shutting down") | ||
except Exception as ex: | ||
self._logger.exception("Exception during wait, shutting down: %s", ex) | ||
self._startup() | ||
self._logger.info("Plugin %s has started", self.unique_name) | ||
|
||
self._shutdown() | ||
self._logger.info("Plugin %s has terminated", self.unique_name) | ||
try: | ||
# Need the timeout param so this works correctly in Python 2 | ||
while not self._shutdown_event.wait(timeout=0.1): | ||
pass | ||
except KeyboardInterrupt: | ||
self._logger.debug("Received KeyboardInterrupt - shutting down") | ||
except Exception as ex: | ||
self._logger.exception("Exception during wait, shutting down: %s", ex) | ||
|
||
self._shutdown() | ||
except PluginValidationError: | ||
self._shutdown(status="ERROR") | ||
finally: | ||
self._logger.info("Plugin %s has terminated", self.unique_name) | ||
|
||
@property | ||
def client(self): | ||
|
@@ -368,6 +379,35 @@ def _hook(exc_type, exc_value, traceback): | |
|
||
sys.excepthook = _hook | ||
|
||
def get_system_dependency(self, require, timeout=300): | ||
wait_time = 0.1 | ||
while timeout > 0: | ||
system = self._ez_client.find_unique_system(name=require) | ||
if ( | ||
system | ||
and system.instances | ||
and any("RUNNING" == instance.status for instance in system.instances) | ||
): | ||
return system | ||
self._wait() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't give it the wait time There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now using the wait time |
||
self.logger.error( | ||
f"Waiting {wait_time:.1f} seconds before next attempt for {self._system} " | ||
f"dependency for {require}" | ||
) | ||
timeout = timeout - wait_time | ||
wait_time = min(wait_time * 2, 30) | ||
|
||
raise PluginValidationError( | ||
f"Failed to resolve {self._system} dependency for {require}" | ||
) | ||
|
||
def await_dependencies(self, config): | ||
for req in config.requires: | ||
system = self.get_system_dependency(req, config.requires_timeout) | ||
self.logger.info( | ||
f"Resolved system {system} for {req}: {config.name} {config.instance_name}" | ||
) | ||
|
||
def _startup(self): | ||
"""Plugin startup procedure | ||
|
||
|
@@ -406,12 +446,20 @@ def _startup(self): | |
self._logger.debug("Initializing and starting processors") | ||
self._admin_processor, self._request_processor = self._initialize_processors() | ||
self._admin_processor.startup() | ||
self._request_processor.startup() | ||
|
||
self._logger.debug("Setting signal handlers") | ||
self._set_signal_handlers() | ||
try: | ||
if self._config.requires: | ||
self.await_dependencies(self._config) | ||
except PluginValidationError: | ||
raise | ||
else: | ||
self._start() | ||
self._request_processor.startup() | ||
finally: | ||
self._logger.debug("Setting signal handlers") | ||
self._set_signal_handlers() | ||
|
||
def _shutdown(self): | ||
def _shutdown(self, status="STOPPED"): | ||
"""Plugin shutdown procedure | ||
|
||
This method gracefully stops the plugin. When it completes the plugin should be | ||
|
@@ -422,14 +470,18 @@ def _shutdown(self): | |
self._shutdown_event.set() | ||
|
||
self._logger.debug("Shutting down processors") | ||
self._request_processor.shutdown() | ||
# Join will cause an exception if processor thread wasn't started | ||
try: | ||
self._request_processor.shutdown() | ||
except RuntimeError: | ||
pass | ||
self._admin_processor.shutdown() | ||
|
||
try: | ||
self._ez_client.update_instance(self._instance.id, new_status="STOPPED") | ||
self._ez_client.update_instance(self._instance.id, new_status=status) | ||
except Exception: | ||
self._logger.warning( | ||
"Unable to notify Beer-garden that this plugin is STOPPED, so this " | ||
f"Unable to notify Beer-garden that this plugin is {status}, so this " | ||
"plugin's status may be incorrect in Beer-garden" | ||
) | ||
|
||
|
@@ -540,6 +592,7 @@ def _initialize_system(self): | |
"icon_name": self._system.icon_name, | ||
"template": self._system.template, | ||
"groups": self._system.groups, | ||
"requires": self._system.requires, | ||
} | ||
|
||
# And if this particular instance doesn't exist we want to add it | ||
|
@@ -590,13 +643,13 @@ def _initialize_processors(self): | |
thread_name="Admin Consumer", | ||
queue_name=self._instance.queue_info["admin"]["name"], | ||
max_concurrent=1, | ||
**common_args | ||
**common_args, | ||
) | ||
request_consumer = RequestConsumer.create( | ||
thread_name="Request Consumer", | ||
queue_name=self._instance.queue_info["request"]["name"], | ||
max_concurrent=self._config.max_concurrent, | ||
**common_args | ||
**common_args, | ||
) | ||
|
||
# Both RequestProcessors need an updater | ||
|
@@ -635,6 +688,13 @@ def _start(self): | |
self._instance.id, new_status="RUNNING" | ||
) | ||
|
||
def _wait(self): | ||
"""Handle wait request""" | ||
# Set the status to wait | ||
self._instance = self._ez_client.update_instance( | ||
self._instance.id, new_status="AWAITING_SYSTEM" | ||
) | ||
|
||
def _stop(self): | ||
"""Handle stop Request""" | ||
# Because the run() method is on a 0.1s sleep there's a race regarding if the | ||
|
@@ -847,6 +907,9 @@ def _setup_system(self, system, plugin_kwargs): | |
if self._config.group: | ||
self._config.groups.append(self._config.group) | ||
|
||
if self._config.require: | ||
self._config.requires.append(self._config.require) | ||
|
||
system = System( | ||
name=self._config.name, | ||
version=self._config.version, | ||
|
@@ -860,6 +923,8 @@ def _setup_system(self, system, plugin_kwargs): | |
template=self._config.template, | ||
groups=self._config.groups, | ||
prefix_topic=self._config.prefix_topic, | ||
requires=self._config.requires, | ||
requires_timeout=self._config.requires_timeout, | ||
) | ||
|
||
return system | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to check, but if we have access to the Garden name of the local plugin, we should pass that forward as a filter criteria.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local=True seems to be ok