-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Celery as an executor and refactor threadng logic
- Implement Celery for task execution - Refactor code to improve thread handling and structure
- Loading branch information
Mohammad Torkashvand
committed
Nov 13, 2024
1 parent
64fb491
commit 12895fe
Showing
16 changed files
with
336 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Environment configuration for LSO application | ||
|
||
# Ansible configuration | ||
ANSIBLE_PLAYBOOKS_ROOT_DIR=/path/to/ansible/playbooks | ||
|
||
# Executor configuration | ||
EXECUTOR=threadpool # Options: "threadpool", "celery" | ||
MAX_THREAD_POOL_WORKERS=10 | ||
|
||
# Request settings | ||
REQUEST_TIMEOUT_SEC=10 | ||
|
||
# Celery configuration | ||
CELERY_BROKER_URL=redis://localhost:6379/0 | ||
CELERY_RESULT_BACKEND=redis://localhost:6379/0 | ||
CELERY_TIMEZONE=Europe/Amsterdam | ||
CELERY_ENABLE_UTC=True | ||
CELERY_RESULT_EXPIRES=3600 | ||
WORKER_QUEUE_NAME=lso-worker-queue | ||
|
||
# Debug/Testing | ||
TESTING=False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2023-2024 GÉANT Vereniging. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Module defines tasks for executing Ansible playbooks asynchronously using Celery. | ||
The primary task, `run_playbook_proc_task`, runs an Ansible playbook and sends a POST request with | ||
the results to a specified callback URL. | ||
""" | ||
|
||
import logging | ||
from typing import Any | ||
|
||
import ansible_runner | ||
import requests | ||
from starlette import status | ||
|
||
from lso.config import settings | ||
from lso.worker import RUN_PLAYBOOK, celery | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@celery.task(name=RUN_PLAYBOOK) # type: ignore[misc] | ||
def run_playbook_proc_task( | ||
job_id: str, playbook_path: str, extra_vars: dict[str, Any], inventory: dict[str, Any] | str, callback: str | ||
) -> None: | ||
"""Celery task to run a playbook. | ||
:param str job_id: Identifier of the job being executed. | ||
:param str playbook_path: Path to the playbook to be executed. | ||
:param dict[str, Any] extra_vars: Extra variables to pass to the playbook. | ||
:param dict[str, Any] | str inventory: Inventory to run the playbook against. | ||
:param HttpUrl callback: Callback URL for status updates. | ||
:return: None | ||
""" | ||
msg = f"playbook_path: {playbook_path}" | ||
logger.info(msg) | ||
ansible_playbook_run = ansible_runner.run(playbook=playbook_path, inventory=inventory, extravars=extra_vars) | ||
|
||
payload = { | ||
"status": ansible_playbook_run.status, | ||
"job_id": job_id, | ||
"output": ansible_playbook_run.stdout.readlines(), | ||
"return_code": int(ansible_playbook_run.rc), | ||
} | ||
|
||
request_result = requests.post(str(callback), json=payload, timeout=settings.REQUEST_TIMEOUT_SEC) | ||
if not status.HTTP_200_OK <= request_result.status_code < status.HTTP_300_MULTIPLE_CHOICES: | ||
msg = f"Callback failed: {request_result.text}, url: {callback}" | ||
logger.error(msg) | ||
Oops, something went wrong.