-
Notifications
You must be signed in to change notification settings - Fork 654
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
Refactor plugin support to exclusively use EventBus events #1331
Refactor plugin support to exclusively use EventBus events #1331
Conversation
1a704b0
to
712285c
Compare
712285c
to
407e9be
Compare
a30b56b
to
f3f8b29
Compare
f3f8b29
to
e879f92
Compare
28dfc4d
to
4d268a7
Compare
@pipermerriam this is now based on the freshly released Maybe we should rename |
""" | ||
Called at startup, giving the plugin a chance to amend the Trinity CLI argument parser | ||
Called after the plugin received its context and is ready to bootstrap itself. |
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.
It'd be cool to have a large docstring for this class which diagram'd the lifecycle of a plugin (and probably a page in the docs once the API stablizes.)
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.
Yes, I plan to improve docs for plugins soon! (#1103)
trinity/extensibility/plugin.py
Outdated
pass | ||
|
||
def should_start(self) -> bool: | ||
def handle_start(self) -> None: |
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 like bootstrap
or boot
.
trinity/extensibility/plugin.py
Outdated
self.event_bus.broadcast( | ||
PluginStartedEvent(type(self)) | ||
) | ||
self.logger.info("Plugin started: %s", self.name) | ||
|
||
def start(self) -> None: |
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'm ok with run
but I think I like start
more since stop
is a natural opposite.
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.
Right, then let's leave this be called start
and rename handle_start
to boot
continue | ||
|
||
try: | ||
self._logger.info("Stopping plugin: %s", plugin.name) | ||
plugin.stop() | ||
plugin.running = False |
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.
This seems wrong for something external to the plugin to manage its state. Shouldn't this be handled by the plugin.stop
method?
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.
Mmh...well, since stop
is implemented by each individual plugin that would mean that plugin authors either need to remember to call super().stop()
(bad because if they forget it, they are basically leaving the PluginManager
thinking that the plugin would still be running when in reality it isn't.
Alternatively we do the same as we do with the start
/ boot
pair which means there will be another method (shutdown
?) which sets running=False
and then delegates to stop()
. At least, that would be symmetric to the start / boot
pair.
However setting plugin.running
from the manager lets us avoid this extra indirection and I'm personally ok with having the PluginManager
make the flip here. Your call.
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'm generally a fan of the model you proposed with stop/shutdown
. One is internal, only accessed by the manager and handles the orchestration like setting flags, triggering asyncio.Event
s, the other is the hook for the user to implement the logic. We've been using this with BaseService
for a while and I think it's working out well.
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.
Alright, will refactor that prior to merging this.
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.
Ok, I just looked at the BaseService
again and now I think the naming should be:
start
(public, the API to call to start the plugin)
_start
(private, only to be implemented by the plugin to encapsulate the starting logic)
stop
(public, the API called by the PluginManager
to stop the plugin (will flip ready
internally)
_stop
(private, only to be implemented by the plugin to encapsulate the starting logic)
That is inline with the BaseService
that uses run
+ _run
intended to be overwritten and cleanup
+ _cleanup
intended to be overwritten.
Do you agree with that?
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.
@pipermerriam this 5ee9b53
4d268a7
to
5ee9b53
Compare
What was wrong?
Currently plugins have two different ways of consuming events:
Comsuming
lahja
events through theEventBus
Consuming plugin events through
handle_event
With the upcoming
lahja
release, this can be unified.How was it fixed?
EventBus
events onlyshould_start
is gone. The model has turned. While previously thePluginManager
called_start()
on the plugins, plugins do now callhandle_start()
themselves.running
property on which thePluginManager
figures out which plugins need to be stoppedNode
does not depend on thePluginManager
any moreKnown issues:
start
andhandle_start
methods could use better names. Thehandle_start
method is what plugins need to call to start the plugin whereas thestart
method is what they need to implement.Cute Animal Picture