Skip to content

Commit 0b46eb9

Browse files
authored
Separate and cache services (#224)
* Split Jira and Bugzilla services into separate modules * Use lru_cache(maxsize=1) for service getters, Settings class, and `version.json` read We were already using `lru_cache` for the settings class getter, but this PR added the `maxsize=1` arg
1 parent d63b664 commit 0b46eb9

File tree

15 files changed

+367
-333
lines changed

15 files changed

+367
-333
lines changed

jbi/actions/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ Let's create a `new_action`!
3131

3232
1. Use the `payload` to perform the desired processing!
3333
1. List the required Jira permissions to be set on projects that will use this action in the `JIRA_REQUIRED_PERMISSIONS` constant. The list of built-in permissions is [available on Atlanssian API docs](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-permission-schemes/#built-in-permissions).
34-
1. Use the available service calls from `jbi/services.py` (or make new ones)
34+
1. Use the available service calls from `jbi/services` (or make new ones)
3535
1. Update the `README.md` to document your action
3636
1. Now the action `jbi.actions.my_team_actions` can be used in the YAML configuration, under the `module` key.

jbi/actions/default.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
BugzillaWebhookRequest,
1919
JiraContext,
2020
)
21-
from jbi.services import get_bugzilla, get_jira
21+
from jbi.services import bugzilla, jira
2222

2323
settings = get_settings()
2424

@@ -50,8 +50,8 @@ def __init__(self, jira_project_key, **kwargs):
5050
self.jira_project_key = jira_project_key
5151
self.sync_whiteboard_labels = kwargs.get("sync_whiteboard_labels", True)
5252

53-
self.bugzilla_client = get_bugzilla()
54-
self.jira_client = get_jira()
53+
self.bugzilla_client = bugzilla.get_client()
54+
self.jira_client = jira.get_client()
5555

5656
def __call__( # pylint: disable=inconsistent-return-statements
5757
self, payload: BugzillaWebhookRequest

jbi/environment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ class Config:
5555
env_file_encoding = "utf-8"
5656

5757

58-
@lru_cache()
58+
@lru_cache(maxsize=1)
5959
def get_settings() -> Settings:
6060
"""Return the Settings object; use cache"""
6161
return Settings()
6262

6363

64-
@lru_cache()
64+
@lru_cache(maxsize=1)
6565
def get_version():
6666
"""Return contents of version.json. This has generic data in repo, but gets the build details in CI."""
6767
info = {}

jbi/router.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from jbi.environment import Settings, get_settings, get_version
1414
from jbi.models import Actions, BugzillaWebhookRequest
1515
from jbi.runner import IgnoreInvalidRequestError, execute_action
16-
from jbi.services import jbi_service_health_map, jira_visible_projects
16+
from jbi.services import bugzilla, jira
1717

1818
router = APIRouter()
1919

@@ -37,8 +37,11 @@ def root(request: Request, settings: Settings = Depends(get_settings)):
3737
@router.head("/__heartbeat__")
3838
def heartbeat(response: Response, actions: Actions = Depends(get_actions)):
3939
"""Return status of backing services, as required by Dockerflow."""
40-
health_map = jbi_service_health_map(actions)
41-
health_checks = []
40+
health_map = {
41+
"bugzilla": bugzilla.check_health(),
42+
"jira": jira.check_health(actions),
43+
}
44+
health_checks: list = []
4245
for health in health_map.values():
4346
health_checks.extend(health.values())
4447
if not all(health_checks):
@@ -87,7 +90,7 @@ def get_whiteboard_tags(
8790
@router.get("/jira_projects/")
8891
def get_jira_projects():
8992
"""API for viewing projects that are currently accessible by API"""
90-
visible_projects: list[dict] = jira_visible_projects()
93+
visible_projects: list[dict] = jira.fetch_visible_projects()
9194
return [project["key"] for project in visible_projects]
9295

9396

jbi/runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from jbi.environment import Settings
1010
from jbi.errors import ActionNotFoundError, IgnoreInvalidRequestError
1111
from jbi.models import Actions, BugzillaWebhookRequest, RunnerLogContext
12-
from jbi.services import get_bugzilla
12+
from jbi.services import bugzilla
1313

1414
logger = logging.getLogger(__name__)
1515

@@ -41,7 +41,7 @@ def execute_action(
4141
try:
4242
if request.bug.is_private:
4343
request = request.copy(
44-
update={"bug": get_bugzilla().get_bug(request.bug.id)}
44+
update={"bug": bugzilla.get_client().get_bug(request.bug.id)}
4545
)
4646
except Exception as err:
4747
logger.exception("Failed to get bug: %s", err, extra=log_context.dict())

jbi/services.py

Lines changed: 0 additions & 284 deletions
This file was deleted.

jbi/services/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""Exports the bugzilla and jira services modules for convenience"""
2+
3+
from . import bugzilla, jira

0 commit comments

Comments
 (0)