Skip to content

Commit

Permalink
Place core manager methods in the logical order for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Dec 3, 2021
1 parent 342f71a commit 29df8ee
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions src/tribler-gui/tribler_gui/core_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,49 +51,6 @@ def on_core_connected(self, _):
if not self.core_finished:
self.core_connected = True

def on_core_stdout_read_ready(self):
raw_output = bytes(self.core_process.readAllStandardOutput())
self.last_core_stdout_output = raw_output.decode("utf-8").strip()
try:
print(self.last_core_stdout_output) # print core output # noqa: T001
except OSError:
# Possible reason - cannot write to stdout as it was already closed during the application shutdown
if not self.quitting_app:
raise

def on_core_stderr_read_ready(self):
raw_output = bytes(self.core_process.readAllStandardError())
self.last_core_stderr_output = raw_output.decode("utf-8").strip()
try:
print(self.last_core_stderr_output, file=sys.stderr) # print core output # noqa: T001
except OSError:
# Possible reason - cannot write to stdout as it was already closed during the application shutdown
if not self.quitting_app:
raise

def on_core_started(self):
self.core_started = True
self.core_running = True

def on_core_finished(self, exit_code, exit_status):
self.core_running = False
self.core_finished = True
if self.shutting_down:
if self.should_quit_app_on_core_finished:
self.quit_application()
else:
error_message = (
f"The Tribler core has unexpectedly finished with exit code {exit_code} and status: {exit_status}!\n"
f"Last core output: \n {self.last_core_stderr_output or self.last_core_stdout_output}"
)
self._logger.warning(error_message)

# Stop the event manager loop if it is running
if self.events_manager.connect_timer and self.events_manager.connect_timer.isActive():
self.events_manager.connect_timer.stop()

raise CoreCrashedError(error_message)

def start(self, core_args=None, core_env=None, upgrade_manager=None, run_core=True):
"""
First test whether we already have a Tribler process listening on port <CORE_API_PORT>.
Expand Down Expand Up @@ -139,6 +96,30 @@ def start_tribler_core(self):
connect(self.core_process.finished, self.on_core_finished)
self.core_process.start(sys.executable, core_args)

def on_core_started(self):
self.core_started = True
self.core_running = True

def on_core_stdout_read_ready(self):
raw_output = bytes(self.core_process.readAllStandardOutput())
self.last_core_stdout_output = raw_output.decode("utf-8").strip()
try:
print(self.last_core_stdout_output) # print core output # noqa: T001
except OSError:
# Possible reason - cannot write to stdout as it was already closed during the application shutdown
if not self.quitting_app:
raise

def on_core_stderr_read_ready(self):
raw_output = bytes(self.core_process.readAllStandardError())
self.last_core_stderr_output = raw_output.decode("utf-8").strip()
try:
print(self.last_core_stderr_output, file=sys.stderr) # print core output # noqa: T001
except OSError:
# Possible reason - cannot write to stdout as it was already closed during the application shutdown
if not self.quitting_app:
raise

def stop(self, quit_app_on_core_finished=True):
if quit_app_on_core_finished:
self.should_quit_app_on_core_finished = True
Expand All @@ -153,6 +134,25 @@ def stop(self, quit_app_on_core_finished=True):
self.events_manager.shutting_down = True
TriblerNetworkRequest("shutdown", lambda _: None, method="PUT", priority=QNetworkRequest.HighPriority)

def on_core_finished(self, exit_code, exit_status):
self.core_running = False
self.core_finished = True
if self.shutting_down:
if self.should_quit_app_on_core_finished:
self.quit_application()
else:
error_message = (
f"The Tribler core has unexpectedly finished with exit code {exit_code} and status: {exit_status}!\n"
f"Last core output: \n {self.last_core_stderr_output or self.last_core_stdout_output}"
)
self._logger.warning(error_message)

# Stop the event manager loop if it is running
if self.events_manager.connect_timer and self.events_manager.connect_timer.isActive():
self.events_manager.connect_timer.stop()

raise CoreCrashedError(error_message)

def quit_application(self):
if not self.quitting_app:
self.quitting_app = True
Expand Down

0 comments on commit 29df8ee

Please sign in to comment.