Skip to content

Commit 9eabc8d

Browse files
committed
Better encapsulation + API name changes
1 parent 3891f04 commit 9eabc8d

File tree

6 files changed

+42
-22
lines changed

6 files changed

+42
-22
lines changed

trinity/extensibility/plugin.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,18 @@ def configure_parser(self, arg_parser: ArgumentParser, subparser: _SubParsersAct
115115
"""
116116
pass
117117

118-
def boot(self) -> None:
118+
def start(self) -> None:
119119
"""
120120
Prepare the plugin to get started and eventually cause ``start`` to get called.
121121
"""
122122
self.running = True
123-
self.start()
123+
self._start()
124124
self.event_bus.broadcast(
125125
PluginStartedEvent(type(self))
126126
)
127127
self.logger.info("Plugin started: %s", self.name)
128128

129-
def start(self) -> None:
129+
def _start(self) -> None:
130130
"""
131131
The ``start`` method is called only once when the plugin is started. In the case
132132
of an `BaseIsolatedPlugin` this method will be launched in a separate process.
@@ -138,18 +138,40 @@ class BaseSyncStopPlugin(BasePlugin):
138138
"""
139139
A ``BaseSyncStopPlugin`` unwinds synchronoulsy, hence blocks until shut down is done.
140140
"""
141-
def stop(self) -> None:
141+
def _stop(self) -> None:
142+
"""
143+
Stop the plugin. Should be overwritten by subclasses.
144+
"""
142145
pass
143146

147+
def stop(self) -> None:
148+
"""
149+
Stop the plugin by delegating to
150+
:meth:`~trinity.extensibility.plugin.BaseSyncStopPlugin._stop`
151+
"""
152+
self._stop()
153+
self.running = False
154+
144155

145156
class BaseAsyncStopPlugin(BasePlugin):
146157
"""
147158
A ``BaseAsyncStopPlugin`` unwinds asynchronoulsy, hence needs to be awaited.
148159
"""
149160

150-
async def stop(self) -> None:
161+
async def _stop(self) -> None:
162+
"""
163+
Asynchronously stop the plugin. Should be overwritten by subclasses.
164+
"""
151165
pass
152166

167+
async def stop(self) -> None:
168+
"""
169+
Asynchronously stop the plugin by delegating to
170+
:meth:`~trinity.extensibility.plugin.BaseAsyncStopPlugin._stop`
171+
"""
172+
await self._stop()
173+
self.running = False
174+
153175

154176
class BaseMainProcessPlugin(BasePlugin):
155177
"""
@@ -170,7 +192,7 @@ class BaseIsolatedPlugin(BaseSyncStopPlugin):
170192

171193
_process: Process = None
172194

173-
def boot(self) -> None:
195+
def start(self) -> None:
174196
"""
175197
Prepare the plugin to get started and eventually cause ``start`` to get called.
176198
"""
@@ -190,9 +212,9 @@ def _prepare_start(self) -> None:
190212
self.event_bus.broadcast(
191213
PluginStartedEvent(type(self))
192214
)
193-
self.start()
215+
self._start()
194216

195-
def stop(self) -> None:
217+
def _stop(self) -> None:
196218
self.context.event_bus.stop()
197219
kill_process_gracefully(self._process, self.logger)
198220

@@ -212,7 +234,7 @@ def configure_parser(self, arg_parser: ArgumentParser, subparser: _SubParsersAct
212234
def handle_event(self, activation_event: BaseEvent) -> None:
213235
self.logger.info("Debug plugin: handle_event called: %s", activation_event)
214236

215-
def start(self) -> None:
237+
def _start(self) -> None:
216238
self.logger.info("Debug plugin: start called")
217239
asyncio.ensure_future(self.count_forever())
218240

@@ -223,5 +245,5 @@ async def count_forever(self) -> None:
223245
i += 1
224246
await asyncio.sleep(1)
225247

226-
async def stop(self) -> None:
248+
async def _stop(self) -> None:
227249
self.logger.info("Debug plugin: stop called")

trinity/extensibility/plugin_manager.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ def shutdown_blocking(self) -> None:
195195
try:
196196
self._logger.info("Stopping plugin: %s", plugin.name)
197197
plugin.stop()
198-
plugin.running = False
199198
self._logger.info("Successfully stopped plugin: %s", plugin.name)
200199
except Exception:
201200
self._logger.exception("Exception thrown while stopping plugin %s", plugin.name)
@@ -224,7 +223,6 @@ async def shutdown(self) -> None:
224223
'Exception thrown while stopping plugin %s: %s', plugin.name, result
225224
)
226225
else:
227-
plugin.running = False
228226
self._logger.info("Successfully stopped plugin: %s", plugin.name)
229227

230228
def _stop_plugins(self,

trinity/plugins/builtin/ethstats/plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ def ready(self) -> None:
101101
self.node_id = args.ethstats_node_id
102102
self.node_contact = args.ethstats_node_contact
103103

104-
self.boot()
104+
self.start()
105105

106-
def start(self) -> None:
106+
def _start(self) -> None:
107107
service = EthstatsService(
108108
self.context,
109109
self.server_url,

trinity/plugins/builtin/json_rpc/plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def name(self) -> str:
3535

3636
def ready(self) -> None:
3737
if not self.context.args.disable_rpc:
38-
self.boot()
38+
self.start()
3939

4040
def configure_parser(self, arg_parser: ArgumentParser, subparser: _SubParsersAction) -> None:
4141
arg_parser.add_argument(
@@ -44,7 +44,7 @@ def configure_parser(self, arg_parser: ArgumentParser, subparser: _SubParsersAct
4444
help="Disables the JSON-RPC Server",
4545
)
4646

47-
def start(self) -> None:
47+
def _start(self) -> None:
4848
db_manager = create_db_manager(self.context.trinity_config.database_ipc_path)
4949
db_manager.connect()
5050

trinity/plugins/builtin/light_peer_chain_bridge/plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ def should_start(self) -> bool:
5555
def handle_event(self, event: ResourceAvailableEvent) -> None:
5656
if event.resource_type is BaseChain:
5757
self.chain = event.resource
58-
self.boot()
58+
self.start()
5959

60-
def start(self) -> None:
60+
def _start(self) -> None:
6161
chain = cast(LightDispatchChain, self.chain)
6262
self.handler = LightPeerChainEventBusHandler(chain._peer_chain, self.context.event_bus)
6363
asyncio.ensure_future(self.handler.run())
6464

65-
async def stop(self) -> None:
65+
async def _stop(self) -> None:
6666
# This isn't really needed for the standard shutdown case as the LightPeerChain will
6767
# automatically shutdown whenever the `CancelToken` it was chained with is triggered.
6868
# It may still be useful to stop the LightPeerChain Bridge plugin individually though.

trinity/plugins/builtin/tx_pool/plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ def handle_event(self, event: ResourceAvailableEvent) -> None:
7575
self.chain = event.resource
7676

7777
if all((self.peer_pool is not None, self.chain is not None, self.is_enabled)):
78-
self.boot()
78+
self.start()
7979

80-
def start(self) -> None:
80+
def _start(self) -> None:
8181
if isinstance(self.chain, BaseMainnetChain):
8282
validator = DefaultTransactionValidator(self.chain, BYZANTIUM_MAINNET_BLOCK)
8383
elif isinstance(self.chain, BaseRopstenChain):
@@ -90,7 +90,7 @@ def start(self) -> None:
9090
self.tx_pool = TxPool(self.peer_pool, validator, self.cancel_token)
9191
asyncio.ensure_future(self.tx_pool.run())
9292

93-
async def stop(self) -> None:
93+
async def _stop(self) -> None:
9494
# This isn't really needed for the standard shutdown case as the TxPool will automatically
9595
# shutdown whenever the `CancelToken` it was chained with is triggered. It may still be
9696
# useful to stop the TxPool plugin individually though.

0 commit comments

Comments
 (0)