Skip to content

Commit

Permalink
LITE-28560: Adding task to check and update product on commerce
Browse files Browse the repository at this point in the history
  • Loading branch information
akodelia committed Sep 18, 2023
1 parent 7800bc6 commit d6c3123
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM cloudblueconnect/connect-extension-runner:28.8
FROM cloudblueconnect/connect-extension-runner:28.9

COPY pyproject.toml /install_temp/.
COPY poetry.* /install_temp/.
Expand Down
53 changes: 46 additions & 7 deletions connect_ext_ppr/tasks_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy.orm import joinedload, selectinload

from connect_ext_ppr.client.exception import ClientError
from connect_ext_ppr.db import get_db_ctx_manager
from connect_ext_ppr.db import get_cbc_extension_db, get_db_ctx_manager
from connect_ext_ppr.models.enums import CBCTaskLogStatus
from connect_ext_ppr.models.enums import (
DeploymentRequestStatusChoices,
Expand All @@ -16,12 +16,23 @@
from connect_ext_ppr.models.deployment import DeploymentRequest
from connect_ext_ppr.models.ppr import PPRVersion
from connect_ext_ppr.models.task import Task
from connect_ext_ppr.services.cbc_extension import get_hub_credentials
from connect_ext_ppr.services.cbc_hub import CBCService


class TaskException(Exception):
pass


def _get_cbc_service(config, deployment):
cbc_db = get_cbc_extension_db(config)
hub_credentials = get_hub_credentials(deployment.hub_id, cbc_db)
if not hub_credentials:
raise TaskException(f'Hub Credentials not found for Hub ID {deployment.hub_id}')

return CBCService(hub_credentials, False)


def _execute_with_retries(function, func_args, num_retries=5):
"""
@param function: reference to function to execute
Expand Down Expand Up @@ -71,15 +82,38 @@ def _check_cbc_task_status(cbc_service, tracking_id):
raise TaskException(f'Something went wrong with task: {tracking_id}')


def validate_ppr(deployment_request):
def validate_ppr(deployment_request, **kwargs):
return True


def apply_ppr_and_delegate_to_marketplaces(deployment_request):
def check_and_update_product(deployment_request, cbc_service, **kwargs):
if not deployment_request.manually:

product_id = deployment_request.deployment.product_id

response = _execute_with_retries(
cbc_service.get_product_details, func_args={'product_id': product_id},
)

if 'error' in response.keys():
raise Exception(response['error'])

if response.get('isUpdateAvailable'):
response = _execute_with_retries(
cbc_service.update_product, func_args={'product_id': product_id},
)

if 'error' in response.keys():
raise Exception(response['error'])

return True


def apply_ppr_and_delegate_to_marketplaces(deployment_request, **kwargs):
return True


def delegate_to_l2(deployment_request):
def delegate_to_l2(deployment_request, **kwargs):
return True


Expand All @@ -90,8 +124,10 @@ def delegate_to_l2(deployment_request):
}


def execute_tasks(db, tasks):
def execute_tasks(db, config, tasks):
was_succesfull = False
cbc_service = _get_cbc_service(config, tasks[0].deployment_request.deployment)

for task in tasks:
db.refresh(task, with_for_update=True)
if task.status == TasksStatusChoices.pending:
Expand All @@ -101,7 +137,10 @@ def execute_tasks(db, tasks):
db.commit()

try:
was_succesfull = TASK_PER_TYPE.get(task.type)(task.deployment_request)
was_succesfull = TASK_PER_TYPE.get(task.type)(
deployment_request=task.deployment_request,
cbc_service=cbc_service,
)
task.status = TasksStatusChoices.done
if not was_succesfull:
task.status = TasksStatusChoices.error
Expand Down Expand Up @@ -148,7 +187,7 @@ def main_process(deployment_request_id, config):
deployment_request_id=deployment_request_id,
).order_by(Task.id).all()

was_succesfull = execute_tasks(db, tasks)
was_succesfull = execute_tasks(db, config, tasks)

db.refresh(deployment_request, with_for_update=True)

Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def _build_deployment_request(
status=None,
started_at=None,
finished_at=None,
manually=False,
):
if not deployment:
deployment = deployment_factory()
Expand All @@ -185,15 +186,18 @@ def _build_deployment_request(

dep_req = DeploymentRequest(
deployment_id=deployment.id,
deployment=deployment,
ppr_id=ppr.id,
created_by=deployment.account_id,
manually=manually,
delegate_l2=delegate_l2,
status=status,
started_at=started_at,
finished_at=finished_at,
)
dbsession.add(ppr)
dbsession.set_next_verbose(dep_req, 'deployment_id')
dbsession.commit()
return dep_req
return _build_deployment_request

Expand Down
Loading

0 comments on commit d6c3123

Please sign in to comment.