13
13
)
14
14
from weakref import WeakSet
15
15
16
- from eth .tools .logging import TraceLogger
17
-
18
16
from cancel_token import CancelToken , OperationCancelled
17
+ from eth_utils import (
18
+ ValidationError ,
19
+ )
20
+
21
+ from eth .tools .logging import TraceLogger
19
22
20
23
from p2p .cancellable import CancellableMixin
21
24
from p2p .utils import get_asyncio_executor
@@ -88,9 +91,9 @@ async def run(
88
91
finished_callback (if one was passed).
89
92
"""
90
93
if self .is_running :
91
- raise RuntimeError ("Cannot start the service while it's already running" )
94
+ raise ValidationError ("Cannot start the service while it's already running" )
92
95
elif self .is_cancelled :
93
- raise RuntimeError ("Cannot restart a service that has already been cancelled" )
96
+ raise ValidationError ("Cannot restart a service that has already been cancelled" )
94
97
95
98
if finished_callback :
96
99
self ._finished_callbacks .append (finished_callback )
@@ -144,6 +147,15 @@ def run_child_service(self, child_service: 'BaseService') -> None:
144
147
"""
145
148
Run a child service and keep a reference to it to be considered during the cleanup.
146
149
"""
150
+ if child_service .is_running :
151
+ raise ValidationError (
152
+ f"Can't start service { child_service !r} , child of { self !r} : it's already running"
153
+ )
154
+ elif child_service .is_cancelled :
155
+ raise ValidationError (
156
+ f"Can't restart { child_service !r} , child of { self !r} : it's already completed"
157
+ )
158
+
147
159
self ._child_services .add (child_service )
148
160
self .run_task (child_service .run ())
149
161
@@ -153,6 +165,16 @@ def run_daemon(self, service: 'BaseService') -> None:
153
165
154
166
If the service finishes while we're still running, we'll terminate as well.
155
167
"""
168
+ if service .is_running :
169
+ raise ValidationError (
170
+ f"Can't start daemon { child_service !r} , child of { self !r} : it's already running"
171
+ )
172
+ elif service .is_cancelled :
173
+ raise ValidationError (
174
+ f"Can't restart daemon { service !r} , child of { self !r} : it's already completed"
175
+ )
176
+
177
+
156
178
self ._child_services .add (service )
157
179
158
180
async def _run_daemon_wrapper () -> None :
@@ -193,7 +215,7 @@ async def cancel(self) -> None:
193
215
self .logger .warning ("Tried to cancel %s, but it was already cancelled" , self )
194
216
return
195
217
elif not self .is_running :
196
- raise RuntimeError ("Cannot cancel a service that has not been started" )
218
+ raise ValidationError ("Cannot cancel a service that has not been started" )
197
219
198
220
self .logger .debug ("Cancelling %s" , self )
199
221
self .events .cancelled .set ()
0 commit comments