Skip to content
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

Core/starter #501

Merged
merged 4 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ All notable changes to the kytos project will be documented in this file.
UNRELEASED - Under development
******************************

[2024.1.2] - 2024-09-16
***********************

Changed
=======
- Kytos will wait until uvicorn server starts before loading NApps.

[2024.1.1] - 2024-08-04
***********************

Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
# built documents.
#
# The short X.Y version.
version = u'2024.1.1'
version = u'2024.1.2'
show_version = False
# The full version, including alpha/beta/rc tags.
release = u'2024.1.1'
release = u'2024.1.2'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
22 changes: 15 additions & 7 deletions kytos/core/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,18 @@ def toggle_debug(self, name=None):
# enable debug
logger.setLevel(logging.DEBUG)

def start(self, restart=False):
async def start(self, restart=False):
"""Create pidfile and call start_controller method."""
self.enable_logs()
# pylint: disable=broad-except
try:
if self.options.database:
db_conn_wait(db_backend=self.options.database)
self.start_auth()
if self.options.apm:
self.apm = init_apm(self.options.apm, app=self.api_server.app)
if not restart:
self.create_pidfile()
self.start_controller()
await self.start_controller()
except (KytosDBInitException, KytosAPMInitException) as exc:
message = f"Kytos couldn't start because of {str(exc)}"
sys.exit(message)
Expand Down Expand Up @@ -361,7 +360,12 @@ def buffers(self) -> KytosBuffers:
self._buffers = KytosBuffers()
return self._buffers

def start_controller(self):
async def _wait_api_server_started(self):
"""Use this method to wait for Uvicorn server to start."""
while not self.api_server.server.started:
await asyncio.sleep(0.1)

async def start_controller(self):
"""Start the controller.

Starts the KytosServer (TCP Server) coroutine.
Expand All @@ -377,16 +381,20 @@ def start_controller(self):
self,
self.options.protocol_name)

self.log.info("Starting TCP server: %s", self.server)
self.server.serve_forever()

self.log.info("Starting API server")
task = self.loop.create_task(self.api_server.serve())
self._tasks.append(task)
await self._wait_api_server_started()

self.log.info(f"Starting TCP server: {self.server}")
self.server.serve_forever()

# ASYNC TODO: ensure all threads started correctly
# This is critical, if any of them failed starting we should exit.
# sys.exit(error_msg.format(thread, exception))

self.log.info("Starting authorization.")
self.start_auth()
self.log.info("Loading Kytos NApps...")
self.napp_dir_listener.start()
self.pre_install_napps(self.options.napps_pre_installed)
Expand Down
2 changes: 1 addition & 1 deletion kytos/core/kytosd.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def async_main(config):
if controller.options.debug:
loop.set_debug(True)

loop.call_soon(controller.start)
loop.run_until_complete(controller.start())

shell_task = None
if controller.options.foreground:
Expand Down
2 changes: 1 addition & 1 deletion kytos/core/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The present metadata is intended to be used mainly on the setup.
"""
__version__ = '2024.1.1'
__version__ = '2024.1.2'
__author__ = 'Kytos Team'
__author_email__ = 'devel@lists.kytos.io'
__license__ = 'MIT'
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/test_core/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ def test_debug_wrong_name(self):
@patch('kytos.core.controller.Controller.start_controller')
@patch('kytos.core.controller.Controller.create_pidfile')
@patch('kytos.core.controller.Controller.enable_logs')
def test_start(self, *args):
async def test_start(self, *args):
"""Test start method."""
(mock_enable_logs, mock_create_pidfile,
mock_start_controller, mock_db_conn_wait,
mock_init_apm) = args
self.controller.start()
await self.controller.start()

mock_enable_logs.assert_called()
mock_create_pidfile.assert_called()
Expand All @@ -190,13 +190,13 @@ def test_start(self, *args):
@patch('kytos.core.controller.Controller.start_controller')
@patch('kytos.core.controller.Controller.create_pidfile')
@patch('kytos.core.controller.Controller.enable_logs')
def test_start_error_broad_exception(self, *args):
async def test_start_error_broad_exception(self, *args):
"""Test start error handling broad exception."""
(mock_enable_logs, mock_create_pidfile,
mock_start_controller, mock_db_conn_wait,
mock_init_apm, mock_sys) = args
mock_start_controller.side_effect = Exception
self.controller.start()
await self.controller.start()

mock_enable_logs.assert_called()
mock_create_pidfile.assert_called()
Expand All @@ -210,14 +210,14 @@ def test_start_error_broad_exception(self, *args):
@patch('kytos.core.controller.Controller.start_controller')
@patch('kytos.core.controller.Controller.create_pidfile')
@patch('kytos.core.controller.Controller.enable_logs')
def test_start_with_mongodb_and_apm(self, *args):
async def test_start_with_mongodb_and_apm(self, *args):
"""Test start method with database and APM options set."""
(mock_enable_logs, mock_create_pidfile,
mock_start_controller, mock_db_conn_wait,
mock_init_apm) = args
self.controller.options.database = "mongodb"
self.controller.options.apm = "es"
self.controller.start()
await self.controller.start()

mock_enable_logs.assert_called()
mock_create_pidfile.assert_called()
Expand All @@ -229,11 +229,11 @@ def test_start_with_mongodb_and_apm(self, *args):
@patch('kytos.core.controller.sys.exit')
@patch('kytos.core.controller.Controller.create_pidfile')
@patch('kytos.core.controller.Controller.enable_logs')
def test_start_with_invalid_database_backend(self, *args):
async def test_start_with_invalid_database_backend(self, *args):
"""Test start method with unsupported database backend."""
(mock_enable_logs, _, mock_sys_exit) = args
self.controller.options.database = "invalid"
self.controller.start()
await self.controller.start()
mock_enable_logs.assert_called()
mock_sys_exit.assert_called()

Expand Down Expand Up @@ -722,7 +722,7 @@ async def test_start_controller(self, controller, monkeypatch):
controller.api_server = MagicMock()
controller.load_napps = MagicMock()
controller.options.napps_pre_installed = [napp]
controller.start_controller()
await controller.start_controller()
assert controller.buffers

controller.server.serve_forever.assert_called()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_core/test_kytosd.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_async_main(*args):

async_main(MagicMock())

event_loop.call_soon.assert_called_with(controller.start)
event_loop.run_until_complete.assert_called_with(controller.start())

@patch("builtins.open", create=True)
@patch('kytos.core.kytosd.os')
Expand Down
Loading