Skip to content

Commit

Permalink
feat: reduced minimum requirement for running DP application
Browse files Browse the repository at this point in the history
  • Loading branch information
naman108 committed Nov 27, 2024
1 parent 76b6069 commit 3c46ad5
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 15 deletions.
5 changes: 3 additions & 2 deletions digitalpy/core/api/api_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ class ApiService(DigitalPyService):
"""

# The path to the blueprints directory in this module
base_blueprints = pathlib.Path(pathlib.Path(__file__).parent, "blueprints")
base_blueprints = pathlib.Path(__file__).parent / "blueprints"

def __init__(
self,
service: ServiceConfiguration,
integration_manager_subscriber: IntegrationManagerSubscriber,
integration_manager_pusher: IntegrationManagerPusher,
subject_pusher: SubjectPusher,
blueprint_path,
blueprint_path: pathlib.Path,
blueprint_import_base: str,
serialization: Serialization,
):
Expand Down Expand Up @@ -180,6 +180,7 @@ def _get_blueprints(self):

# get the blueprint from the file
blueprints.append(blueprint_module.page)

# iterate through base blueprints
for filename in os.listdir(ApiService.base_blueprints):
if filename.endswith(".py") and filename != "__init__.py":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_configuration(self):
raise NotImplementedError("get_configuration is not yet implemented")

def add_configuration(
self, name: Union[str, StringIO], process_values=True
self, name: Union[str, StringIO, PurePath], process_values=True
) -> None:
"""
Args:
Expand All @@ -42,6 +42,8 @@ def add_configuration(
afterwards
"""
with self._lock:
if isinstance(name, PurePath):
name = str(name)
filename = self.config_path + name

num_parsed_files = len(self.__added_files)
Expand Down
105 changes: 93 additions & 12 deletions digitalpy/core/main/DigitalPy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import multiprocessing
import os
import pathlib
import sys
import threading
from time import sleep
from typing import TYPE_CHECKING

from digitalpy.core.service_management.domain.model.service_configuration import ServiceConfiguration
from digitalpy.core.service_management.domain.model.service_configuration import (
ServiceConfiguration,
)
from digitalpy.core.digipy_configuration.controllers.action_flow_controller import (
ActionFlowController,
)
Expand Down Expand Up @@ -119,6 +122,25 @@ def __init__(self):
)
)

def _initialize_app_configuration(self):
app_conf_path = self.get_app_root() / "configuration"

self.configuration.add_configuration(app_conf_path / "object_configuration.ini")

base_conf: Configuration = InifileConfiguration("")
base_conf.add_configuration(
app_conf_path / "application_configuration.ini",
)

SingletonConfigurationFactory.add_configuration(base_conf)

# set blueprint path
self.configuration.set_value(
"blueprint_path",
str(self.get_app_root() / "blueprints"),
"digitalpy.core_api",
)

def _initialize_status_factory(self):
self.status_factory = StatusFactory()

Expand Down Expand Up @@ -173,7 +195,7 @@ def register_flows(self):

action_flow_controller.create_action_flow(file)

def register_components(self):
def register_core_components(self):
"""register digitalpy core components, these must be registered before any other components
furthermore, these are registered without the use of component management to avoid circular
dependencies. This method of registration also assumes that the components are defined in
Expand Down Expand Up @@ -214,9 +236,62 @@ def register_component(facade: DefaultFacade):

register_component(ServiceManagement(None, None, None, None))

def test_event_loop(self):
"""the main event loop of the application should be called within a continuous while loop"""
sleep(1)
def register_app_components(self):
"""
Registers application components and sets up their installation paths.
This method performs the following steps:
1. Determines the root directory of the application.
2. Sets the installation path for components and blueprints in the configuration.
3. Retrieves an instance of ComponentManagement.
4. Installs all components from the specified directory.
5. Clears the request and response values in the component management.
Raises:
KeyError: If the configuration keys are not found.
ImportError: If the ComponentManagement class cannot be imported.
"""
try:

# Get the application root directory
app_root = self.get_app_root()

# Define the paths for components and blueprints
app_components = app_root / "components"
app_blueprints = app_root / "blueprints"

# Set the installation path for components in the configuration
self.configuration.set_value(
"component_installation_path",
str(app_components),
"ComponentManagement",
)

# Set the blueprint path for components in the configuration
self.configuration.set_value(
"component_blueprint_path", str(app_blueprints), "ComponentManagement"
)

# Retrieve an instance of ComponentManagement
component_management: ComponentManagement = ObjectFactory.get_instance(
"ComponentManagement"
)

# Install all components from the specified directory
component_management.POSTInstallAllComponents(
Directory=str(app_components),
)

# Clear the request and response values in the component management
component_management.request.clear_values()
component_management.response.clear_values()
except KeyError as ke:
raise KeyError("Configuration key not found") from ke
except ImportError as ie:
raise ImportError("Failed to import ComponentManagement") from ie

def get_app_root(self) -> pathlib.Path:
"""Get the root directory of the application."""
app_class = sys.modules[self.__class__.__module__]
return pathlib.Path(app_class.__file__).parent

def event_loop(self):
"""the main event loop of the application should be called within a continuous while loop"""
Expand All @@ -227,7 +302,8 @@ def start(self): # type: ignore
"""Begin the execution of the application, this should be overriden
by any inheriting classes"""

self.register_components()
self.register_core_components()
self.register_app_components()
self.register_flows()
self.configure()

Expand Down Expand Up @@ -323,7 +399,9 @@ def start_service_manager(self) -> bool:
"""
try:
service_configuration: ServiceConfiguration = (
SingletonConfigurationFactory.get_configuration_object("ServiceManagementConfiguration")
SingletonConfigurationFactory.get_configuration_object(
"ServiceManagementConfiguration"
)
)

# begin the service_manager
Expand All @@ -350,17 +428,20 @@ def stop_service_manager(self) -> bool:
try:

self.service_manager.stop()
self.service_manager.join()
self.service_manager.join(5)
return True
except Exception as ex:
raise ex

def stop(self):
"""End the execution of the application"""
self.stop_core_services()
self.stop_zmanager()

exit(0)
try:
self.stop_core_services()
self.stop_zmanager()
except Exception as e:
print("error stopping with exception ", str(e))
finally:
exit(0)

def stop_core_services(self):
"""Stop the core services of the application"""
Expand Down

0 comments on commit 3c46ad5

Please sign in to comment.