### Start Django Development Server Source: https://github.com/yassi/dj-celery-panel/blob/main/docs/installation.md Starts the Django development server, allowing you to access your project and the DJ Celery Panel through a web browser. ```bash python manage.py runserver ``` -------------------------------- ### Start Celery Worker for Monitoring Source: https://github.com/yassi/dj-celery-panel/blob/main/docs/installation.md Launches a Celery worker process. This is required for the DJ Celery Panel to monitor active workers, queues, and tasks. ```bash celery -A your_project worker --loglevel=info ``` -------------------------------- ### Start Celery Worker Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md Navigates to the project directory, applies database migrations, and then starts a Celery worker. The worker logs will be displayed with 'info' level. ```bash cd example_project python manage.py migrate celery -A example_project worker --loglevel=info ``` -------------------------------- ### Run Django Database Migrations Source: https://github.com/yassi/dj-celery-panel/blob/main/docs/installation.md Applies pending database migrations for Django applications, including any necessary for DJ Celery Panel. This ensures the database schema is up-to-date. ```bash python manage.py migrate ``` -------------------------------- ### GitHub Actions CI Pipeline Overview Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md The Continuous Integration pipeline automatically manages the setup and testing of the project. It includes starting necessary services like Redis and PostgreSQL, applying database migrations, running a Celery worker, and executing the full test suite with coverage. ```yaml # This section describes the CI pipeline actions, not executable code. # The pipeline automatically: # - Starts Redis and PostgreSQL services # - Runs database migrations # - Starts a Celery worker in detached mode # - Executes the full test suite with coverage reporting ``` -------------------------------- ### Set up Example Project for Development Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md Navigates into the example project directory within the repository and runs Django migrations and creates a superuser. This prepares a sample Django project for testing DJ Celery Panel. ```bash cd example_project python manage.py migrate python manage.py createsuperuser ``` -------------------------------- ### Install DJ Celery Panel using pip Source: https://github.com/yassi/dj-celery-panel/blob/main/docs/installation.md Installs the dj-celery-panel package using pip. This is the first step in integrating the panel into your Django project. ```bash pip install dj-celery-panel ``` -------------------------------- ### Start Redis for Local Testing Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md Starts a Redis server in a Docker container, which is required as a broker for Celery when running tests or development locally without Docker Compose. ```bash # Terminal 1: Start Redis docker run -d -p 6379:6379 redis:7 ``` -------------------------------- ### Add dj_celery_panel to Django INSTALLED_APPS Source: https://github.com/yassi/dj-celery-panel/blob/main/docs/installation.md Configures the Django project's settings to include 'dj_celery_panel' in the INSTALLED_APPS list. This enables the Django application to recognize and load the panel's components. ```python INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'dj_celery_panel', # Add this # ... your other apps ] ``` -------------------------------- ### Install DJ Celery Panel Locally with Virtualenv Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md Sets up a Python virtual environment, activates it, and installs the dj-celery-panel package in editable mode along with development requirements. This allows for local development and testing. ```bash python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -e . # install dj-celery-panel package locally pip intall -r requirements.txt # install all dev requirements # Alternatively make install # this will also do the above in one single command ``` -------------------------------- ### Start PostgreSQL Database Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md Starts a PostgreSQL database in a Docker container. This is optional as the project can also use SQLite. It exposes the default PostgreSQL port 5432. ```bash docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres:16 ``` -------------------------------- ### Configure Celery Broker and Backend in Django Settings Source: https://github.com/yassi/dj-celery-panel/blob/main/docs/installation.md Sets up the Celery broker URL and result backend in the Django settings file. This is crucial for Celery to communicate with its broker and store task results. ```python # settings.py CELERY_BROKER_URL = 'redis://localhost:6379/0' # Your broker URL CELERY_RESULT_BACKEND = 'django-db' # or your preferred backend ``` -------------------------------- ### Include DJ Celery Panel URLs in Django urls.py Source: https://github.com/yassi/dj-celery-panel/blob/main/docs/installation.md Integrates the DJ Celery Panel's URL patterns into the main Django project's URL configuration. This makes the panel accessible via a specific URL path within the Django admin interface. ```python from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/dj-celery-panel/', include('dj_celery_panel.urls')), path('admin/', admin.site.urls), ] ``` -------------------------------- ### Implement Multi-Source Task Detail Fetching in Python Source: https://github.com/yassi/dj-celery-panel/blob/main/docs/configuration.md Example of a custom task backend that fetches task details from multiple sources, prioritizing cache and falling back to a database. This demonstrates handling data from various locations. ```python class MultiSourceTasksBackend(CeleryAbstractInterface): def get_task_detail(self, task_id): # Try cache first task = self.get_from_redis(task_id) if not task: # Fallback to database task = self.get_from_database(task_id) return TaskDetailPage(task=task) ``` -------------------------------- ### Run Tests for DJ Celery Panel using Docker Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md Executes the test suite for DJ Celery Panel using Docker. This command automatically starts all required services (Redis, PostgreSQL, Celery workers) for comprehensive testing. ```bash make test_docker ``` -------------------------------- ### CeleryInspector: Get Periodic Tasks Source: https://context7.com/yassi/dj-celery-panel/llms.txt Illustrates how to fetch periodic tasks defined in the Celery beat schedule using CeleryInspector. The output includes task names, schedules, arguments, and keyword arguments. ```python from celery import current_app from dj_celery_panel.celery_utils import CeleryInspector inspector = CeleryInspector(current_app) # Get periodic tasks from beat schedule periodic = inspector.get_periodic_tasks() # Returns: [ # {'name': 'health-check', 'task': 'app.tasks.health_check', 'schedule': '300.0', 'args': [], 'kwargs': {}}, # ... # ] ``` -------------------------------- ### CeleryInspector: Get Celery Configuration Source: https://context7.com/yassi/dj-celery-panel/llms.txt Demonstrates how to use CeleryInspector to retrieve Celery configuration details without needing a broker connection. The output includes broker URL, result backend, timezone, and serializer settings. ```python from celery import current_app from dj_celery_panel.celery_utils import CeleryInspector inspector = CeleryInspector(current_app) # Get Celery configuration (no broker connection required) config = inspector.get_configuration_info() # Returns: { # 'broker_url': 'redis://localhost:6379/0', # 'broker_type': 'Redis', # 'result_backend': 'django-db', # 'result_backend_type': 'Database', # 'timezone': 'UTC', # 'task_serializer': 'json', # 'task_track_started': True, # 'task_time_limit': None, # 'default_queue': 'celery', # 'worker_prefetch_multiplier': 4, # ... # } ``` -------------------------------- ### Get Backend Information (Python) Source: https://context7.com/yassi/dj-celery-panel/llms.txt Retrieves information about the configured Celery results backend. This includes the backend's name, a brief description, and the data source it uses. ```python # Assuming task_interface is an instance of a CeleryTasksInterface backend_info = task_interface.get_backend_info() # Returns: { # 'name': 'CeleryTasksDjangoCeleryResultsBackend', # 'description': 'Task history with pagination and search', # 'data_source': 'Django Database (django-celery-results)' # } ``` -------------------------------- ### CeleryInspector: Get Active Queues Source: https://context7.com/yassi/dj-celery-panel/llms.txt Shows how to retrieve information about active Celery queues from workers using CeleryInspector. The output includes queue names, associated exchanges, routing keys, and the workers consuming from them. ```python from celery import current_app from dj_celery_panel.celery_utils import CeleryInspector inspector = CeleryInspector(current_app) # Get active queues from workers queues = inspector.get_queues() # Returns: { # 'queues': [ # {'name': 'celery', 'exchange': 'celery', 'routing_key': 'celery', 'workers': ['celery@worker1']}, # ... # ], # 'error': None # } ``` -------------------------------- ### Get Available Task Filters (Python) Source: https://context7.com/yassi/dj-celery-panel/llms.txt Retrieves a list of available filter options for Celery tasks, typically used to populate UI elements. Each filter option includes a value and a human-readable label. ```python # Assuming task_interface is an instance of a CeleryTasksInterface filters = task_interface.get_available_filters() # Returns: [{'value': None, 'label': 'All'}, {'value': 'success', 'label': 'Success'}, ...] ``` -------------------------------- ### Get Detailed Worker Information (Python) Source: https://context7.com/yassi/dj-celery-panel/llms.txt Fetches detailed information for a specific Celery worker, identified by its hostname. The returned data includes the worker's status, configuration, resource usage, and lists of active, reserved, scheduled, and registered tasks. ```python from celery import current_app from dj_celery_panel.celery_utils import CeleryWorkersInterface, WorkerDetailPage worker_interface = CeleryWorkersInterface(current_app) detail: WorkerDetailPage = worker_interface.get_worker_detail("celery@hostname") # WorkerDetailPage.worker contains: # { # 'name': 'celery@hostname', # 'status': 'online', # 'pool': 'prefork', # 'concurrency': 4, # 'pid': 12345, # 'prefetch_count': 16, # 'total_tasks_executed': 1523, # 'active_tasks': [...], # 'reserved_tasks': [...], # 'scheduled_tasks': [...], # 'registered_tasks': ['app.tasks.send_email', ...], # 'active_queues': [{'name': 'celery', ...}], # 'broker': {'hostname': 'localhost', ...}, # 'rusage': {'maxrss': 123456, ...} # } ``` -------------------------------- ### Get Paginated Task List with Search and Filter (Python) Source: https://context7.com/yassi/dj-celery-panel/llms.txt Retrieves a paginated list of Celery tasks, with options to search by name or ID, and filter by status (pending, success, failure, etc.). The result includes task details, pagination information, and potential errors. ```python from dj_celery_panel.celery_utils import TaskListPage # Assuming task_interface is an instance of a CeleryTasksInterface result: TaskListPage = task_interface.get_tasks( search_query="send_email", # Search by task name or task ID page=1, per_page=50, filter_type="success" # Filter: None, 'pending', 'started', 'success', 'failure' ) # TaskListPage contains: # result.tasks - List of task dictionaries # result.total_count - Total matching tasks # result.page, result.per_page, result.total_pages - Pagination info # result.has_previous, result.has_next - Navigation flags # result.previous_page, result.next_page - Page numbers # result.error - Error message if any ``` -------------------------------- ### Launch Quick Task Form Source: https://github.com/yassi/dj-celery-panel/blob/main/example_project/app/templates/task_launcher.html This section allows users to launch a Celery task that completes immediately. It requires a 'Message' input and provides a button to initiate the task. The interface includes styling for buttons and result messages. ```html ⚡ Quick Task ------------ Launch a task that completes immediately Message Launch Quick Task ``` -------------------------------- ### Custom Backend Implementation and Settings Source: https://github.com/yassi/dj-celery-panel/blob/main/docs/features.md Demonstrates how to create a custom backend for dj-celery-panel by inheriting from CeleryAbstractInterface and registering it in Django settings. This allows for custom data retrieval for tasks, workers, or queues. ```python from dj_celery_panel.celery_utils import CeleryAbstractInterface class CustomTasksBackend(CeleryAbstractInterface): # Backend metadata - displayed in the UI BACKEND_DESCRIPTION = "Data from custom monitoring" DATA_SOURCE = "Custom API" def get_tasks(self, search_query=None, page=1, per_page=50): # Fetch from Redis, external API, custom database, etc. return TaskListPage(...) ``` ```python DJ_CELERY_PANEL_SETTINGS = { "tasks_backend": "myapp.backends.CustomTasksBackend", } ``` -------------------------------- ### CeleryTasksInterface: Initialize Source: https://context7.com/yassi/dj-celery-panel/llms.txt Demonstrates the initialization of CeleryTasksInterface, which provides an interface for retrieving task information with pluggable backend support. It requires the current Celery application instance. ```python from celery import current_app from dj_celery_panel.celery_utils import CeleryTasksInterface, TaskListPage, TaskDetailPage task_interface = CeleryTasksInterface(current_app) ``` -------------------------------- ### Run Django Migrations and Create Superuser Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md Applies database migrations to set up necessary tables for Django and DJ Celery Panel, and creates an administrator user for accessing the Django admin interface. ```bash python manage.py migrate python manage.py createsuperuser # If you don't have an admin user ``` -------------------------------- ### CeleryInspector: Get Worker Status Source: https://context7.com/yassi/dj-celery-panel/llms.txt Shows how to use CeleryInspector to get the status of active Celery workers. This method makes a single stats() call for efficiency and returns details about available workers, their status, pool type, and concurrency. ```python from celery import current_app from dj_celery_panel.celery_utils import CeleryInspector inspector = CeleryInspector(current_app) # Get worker status (single stats() call for efficiency) status = inspector.get_status() # Returns: { # 'celery_available': True, # 'workers': ['celery@worker1', 'celery@worker2'], # 'workers_detail': [ # {'name': 'celery@worker1', 'status': 'online', 'pool': 'prefork', 'concurrency': 4, ...}, # ... # ], # 'active_workers_count': 2, # 'error': None # } ``` -------------------------------- ### CeleryInspector: Get Registered Tasks Source: https://context7.com/yassi/dj-celery-panel/llms.txt Demonstrates retrieving a list of registered Celery tasks using CeleryInspector. The `exclude_internal` parameter can be used to filter out Celery's internal tasks. ```python from celery import current_app from dj_celery_panel.celery_utils import CeleryInspector inspector = CeleryInspector(current_app) # Get registered tasks (local operation, excludes internal celery.* tasks) tasks = inspector.get_registered_tasks(exclude_internal=True) # Returns: ['app.tasks.quick_noop_task', 'app.tasks.slow_task', ...] ``` -------------------------------- ### Set up DJ Celery Panel Development Environment with Docker Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md Uses Docker Compose to bring up all necessary services (Redis, PostgreSQL, Celery workers) for development and testing. 'make docker_shell' opens a shell within the running Docker container. ```bash make docker_up # Bring up all services (Redis, PostgreSQL, Celery workers) make docker_shell # Open a shell in the docker container ``` -------------------------------- ### Spawn Scheduled Tasks Form Source: https://github.com/yassi/dj-celery-panel/blob/main/example_project/app/templates/task_launcher.html This section facilitates the creation of multiple Celery tasks with future execution times. It allows users to specify the number of tasks, delay in seconds, and choose between ETA or countdown. Options for quick vs. slow tasks are also available. ```html 📅 Scheduled Tasks ------------------ Spawn multiple tasks with future execution times Number of Tasks Delay (seconds) Use ETA (vs countdown) Quick tasks (vs slow) Spawn Scheduled Tasks ``` -------------------------------- ### Custom Backend Implementation Source: https://context7.com/yassi/dj-celery-panel/llms.txt Information on how to implement custom Celery result backends for the dj-celery-panel. ```APIDOC ## Custom Backend Implementation ### Description This section outlines the process for creating and integrating custom Celery result backends with dj-celery-panel. To implement a custom backend, you need to create a class that inherits from the appropriate base class provided by dj-celery-panel and implement the required interface methods. ### Requirements Your custom backend class must implement the following methods: - `get_tasks(...)` - `get_task_detail(...)` - `get_available_filters()` - `get_backend_info()` Refer to the library's source code for the exact method signatures and expected return types. Ensure your implementation adheres to the expected data structures for tasks, filters, and backend information. ``` -------------------------------- ### Get Detailed Task Information (Python) Source: https://context7.com/yassi/dj-celery-panel/llms.txt Fetches detailed information for a specific Celery task using its unique ID. The result includes the full task dictionary with traceback and any errors encountered if the task is not found. ```python from dj_celery_panel.celery_utils import TaskDetailPage # Assuming task_interface is an instance of a CeleryTasksInterface detail: TaskDetailPage = task_interface.get_task_detail("task-uuid-12345") # detail.task - Task dictionary with full details including traceback # detail.error - Error message if task not found ``` -------------------------------- ### Run Project Tests Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md Executes the test suite located in the 'tests/' directory using pytest. The '-v' flag enables verbose output, showing individual test results. ```bash pytest tests/ -v ``` -------------------------------- ### Spawn Bulk Tasks Form Source: https://github.com/yassi/dj-celery-panel/blob/main/example_project/app/templates/task_launcher.html This form is designed to spawn a large number of Celery tasks immediately, primarily for testing throughput. Users specify the number of tasks to be spawned. ```html 🚀 Bulk Tasks ------------- Spawn many tasks immediately to test throughput Number of Tasks Spawn Bulk Tasks ``` -------------------------------- ### Configure Celery Broker and Result Backend Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md Sets up the Celery broker URL and result backend in Django settings. This is crucial for Celery and DJ Celery Panel to communicate and store task results. ```python # Celery Configuration CELERY_BROKER_URL = 'redis://localhost:6379/0' # or your broker URL CELERY_RESULT_BACKEND = 'django-db' # or your preferred backend # Optional: Advanced configuration DJ_CELERY_PANEL_SETTINGS = { # Backend classes for each interface "tasks_backend": "dj_celery_panel.celery_utils.CeleryTasksDjangoCeleryResultsBackend", "workers_backend": "dj_celery_panel.celery_utils.CeleryWorkersInspectBackend", "queues_backend": "dj_celery_panel.celery_utils.CeleryQueuesInspectBackend", } ``` -------------------------------- ### Get Detailed Queue Information (Python) Source: https://context7.com/yassi/dj-celery-panel/llms.txt Fetches detailed information for a specific Celery queue, identified by its name. This includes details like exchange type, durability, auto-delete status, associated workers, message count, and consumer count. ```python from celery import current_app from dj_celery_panel.celery_utils import CeleryQueuesInterface, QueueDetailPage queue_interface = CeleryQueuesInterface(current_app) detail: QueueDetailPage = queue_interface.get_queue_detail("celery") # QueueDetailPage.queue contains: # { # 'name': 'celery', # 'exchange': 'celery', # 'exchange_type': 'direct', # 'routing_key': 'celery', # 'durable': True, # 'auto_delete': False, # 'workers': ['celery@host1'], # 'message_count': 42, # 'consumer_count': 2 # } ``` -------------------------------- ### Get CSRF Token from Cookie (JavaScript) Source: https://github.com/yassi/dj-celery-panel/blob/main/example_project/app/templates/task_launcher.html This function retrieves the CSRF token from the document's cookies. It iterates through cookies to find the one matching the provided name, decodes it, and returns its value. This is essential for making authenticated POST requests. ```javascript function getCookie(name) { let cookieValue = null; if (document.cookie && document.cookie !== '') { const cookies = document.cookie.split(';'); for (let i = 0; i < cookies.length; i++) { const cookie = cookies[i].trim(); if (cookie.substring(0, name.length + 1) === (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } ``` -------------------------------- ### Launch Slow Task Form Source: https://github.com/yassi/dj-celery-panel/blob/main/example_project/app/templates/task_launcher.html This form enables the launch of a Celery task that runs for a specified duration. Users need to provide the duration in seconds and an optional task name. The interface includes input fields and a launch button. ```html ⏳ Slow Task ----------- Launch a task that runs for a specified duration Duration (seconds) Task Name Launch Slow Task ``` -------------------------------- ### Launch Retrying Task Form Source: https://github.com/yassi/dj-celery-panel/blob/main/example_project/app/templates/task_launcher.html This form enables the launch of a Celery task that is configured to fail and then retry. Users can specify the number of times the task should fail before potentially succeeding or exhausting retries. ```html 🔄 Retrying Task ---------------- Launch a task that fails then retries Fail Times Launch Retrying Task ``` -------------------------------- ### Get List of Active Celery Queues (Python) Source: https://context7.com/yassi/dj-celery-panel/llms.txt Retrieves a list of all active Celery queues, including their names, associated exchanges, routing keys, and the number of messages currently in each queue. It also indicates if there was an error querying the broker for queue details. ```python from celery import current_app from dj_celery_panel.celery_utils import CeleryQueuesInterface, QueueListPage queue_interface = CeleryQueuesInterface(current_app) result: QueueListPage = queue_interface.get_queues() # QueueListPage contains: # result.queues - List of queue dictionaries # result.error - Error message if any # Each queue dict: # { # 'name': 'celery', # 'exchange': 'celery', # 'routing_key': 'celery', # 'workers': ['celery@host1', 'celery@host2'], # 'message_count': 42, # Queue length from broker (if available) # 'broker_query_error': None # Error if broker query failed # } ``` -------------------------------- ### Get List of Active Celery Workers (Python) Source: https://context7.com/yassi/dj-celery-panel/llms.txt Retrieves a list of all currently active Celery workers. The result includes worker names, detailed information about each worker (pool, concurrency, etc.), the count of online workers, and the reachability status of the Celery broker. ```python from celery import current_app from dj_celery_panel.celery_utils import CeleryWorkersInterface, WorkerListPage worker_interface = CeleryWorkersInterface(current_app) result: WorkerListPage = worker_interface.get_workers() # WorkerListPage contains: # result.workers - List of worker names ['celery@host1', 'celery@host2'] # result.workers_detail - List of worker info dicts with pool, concurrency, etc. # result.active_workers_count - Number of online workers # result.celery_available - Whether Celery broker is reachable # result.error - Error message if any ``` -------------------------------- ### Clone DJ Celery Panel Repository Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md Clones the DJ Celery Panel source code from GitHub to set up a local development environment. This is the first step for contributing to the project or running it locally. ```bash git clone https://github.com/yassi/dj-celery-panel.git cd dj-celery-panel ``` -------------------------------- ### Integrate Datadog Monitoring for Celery Tasks Source: https://github.com/yassi/dj-celery-panel/blob/main/docs/configuration.md This Python code defines a backend to fetch Celery task metrics from the Datadog API. It requires a Datadog client to be configured and provides a formatted list of tasks. This allows for external monitoring of Celery task performance. ```python class DatadogTasksBackend(CeleryAbstractInterface): def get_tasks(self, search_query=None, page=1, per_page=50): # Fetch task metrics from Datadog API metrics = datadog_client.get_celery_metrics() return TaskListPage(tasks=self.format_metrics(metrics), ...) ``` -------------------------------- ### HTML Structure for Celery Task Launcher Source: https://github.com/yassi/dj-celery-panel/blob/main/example_project/app/templates/task_launcher.html This HTML snippet defines the structure and styling for the Celery Task Launcher interface. It includes sections for launching different types of tasks and displaying results. It uses CSS variables for theming and media queries for dark mode support. ```html {% load static %} Celery Task Launcher body { font-family: "Roboto","Lucida Grande","DejaVu Sans","Bitstream Vera Sans",Verdana,Arial,sans-serif; background: var(--body-bg, #f8f8f8); padding: 20px; } .container { max-width: 1200px; margin: 0 auto; } .header { background: white; padding: 20px; border-radius: 8px; margin-bottom: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } .header h1 { margin: 0 0 10px 0; color: var(--body-fg, #333); } .header p { margin: 0; color: var(--body-quiet-color, #666); } .task-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(350px, 1fr)); gap: 20px; } .task-card { background: white; border-radius: 8px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } .task-card h2 { margin: 0 0 10px 0; font-size: 1.3em; color: var(--link-fg, #417690); } .task-card p { margin: 0 0 15px 0; color: var(--body-quiet-color, #666); font-size: 0.95em; } .form-group { margin-bottom: 15px; } .form-group label { display: block; margin-bottom: 5px; font-weight: 600; font-size: 0.9em; color: var(--body-fg, #333); } .form-group input[type="text""], .form-group input[type="number""], .form-group select { width: 100%; padding: 8px 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 14px; box-sizing: border-box; } .form-group input[type="checkbox"] { margin-right: 8px; } .launch-btn { width: 100%; padding: 12px; background: var(--link-fg, #417690); color: white; border: none; border-radius: 4px; font-size: 1em; font-weight: 600; cursor: pointer; transition: background 0.2s; } .launch-btn:hover { background: #2e5266; } .launch-btn:disabled { background: #ccc; cursor: not-allowed; } .result-message { margin-top: 15px; padding: 12px; border-radius: 4px; font-size: 0.9em; display: none; } .result-message.success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; display: block; } .result-message.error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; display: block; } .task-id { font-family: 'Monaco', 'Menlo', monospace; background: rgba(0,0,0,0.05); padding: 2px 6px; border-radius: 3px; font-size: 0.85em; } .back-link { display: inline-block; margin-bottom: 20px; color: var(--link-fg, #417690); text-decoration: none; } .back-link:hover { text-decoration: underline; } @media (prefers-color-scheme: dark) { body { background: var(--body-bg, #121212); } .header, .task-card { background: var(--darkened-bg, #1e1e1e); } .header h1, .task-card h2, .form-group label { color: var(--body-fg, #e0e0e0); } .header p, .task-card p { color: var(--body-quiet-color, #999); } .form-group input[type="text""], .form-group input[type="number""], .form-group select { background: var(--darkened-bg, #2a2a2a); border-color: #444; color: var(--body-fg, #e0e0e0); } .result-message.success { background: rgba(40, 167, 69, 0.2); color: #4caf50; border-color: rgba(40, 167, 69, 0.4); } .result-message.error { background: rgba(220, 53, 69, 0.2); color: #ef5350; border-color: rgba(220, 53, 69, 0.4); } } ``` -------------------------------- ### Python: Custom Celery Tasks Backend Implementation Source: https://github.com/yassi/dj-celery-panel/blob/main/README.md Demonstrates how to create a custom backend for fetching Celery tasks by extending `CeleryAbstractInterface`. This allows integration with custom data sources like external APIs or databases. Configuration is done via Django's `settings.py`. ```python from dj_celery_panel.celery_utils import CeleryAbstractInterface class CustomTasksBackend(CeleryAbstractInterface): """Custom backend that fetches tasks from your own API.""" def get_tasks(self, search_query=None, page=1, per_page=50): # Your custom implementation # Fetch from external API, custom database, etc. return TaskListPage(...) def get_task_detail(self, task_id): # Your custom implementation return TaskDetailPage(...) # Configure in settings.py DJ_CELERY_PANEL_SETTINGS = { "tasks_backend": "myapp.backends.CustomTasksBackend", } ``` -------------------------------- ### Handle Form Submissions for Task Launch (JavaScript) Source: https://github.com/yassi/dj-celery-panel/blob/main/example_project/app/templates/task_launcher.html This code handles form submissions for launching tasks. It prevents default form submission, collects form data, appends the task type, and sends an asynchronous POST request to '/tasks/launch/'. It then updates the UI based on the server's response, displaying success or error messages. The button is disabled during the request and re-enabled afterward. ```javascript document.querySelectorAll('.task-form').forEach(form => { form.addEventListener('submit', async (e) => { e.preventDefault(); const taskType = form.dataset.taskType; const button = form.querySelector('.launch-btn'); const resultDiv = form.querySelector('.result-message'); const formData = new FormData(form); formData.append('task_type', taskType); button.disabled = true; button.textContent = 'Launching...'; resultDiv.style.display = 'none'; try { const response = await fetch('/tasks/launch/', { method: 'POST', headers: { 'X-CSRFToken': getCookie('csrftoken') }, body: formData }); const data = await response.json(); if (data.success) { resultDiv.className = 'result-message success'; resultDiv.innerHTML = ` ✓ ${data.task_type} Launched!
Task ID: ${data.task_id}
${data.message} `; } else { resultDiv.className = 'result-message error'; resultDiv.innerHTML = `Error: ${data.error}`; } } catch (error) { resultDiv.className = 'result-message error'; resultDiv.innerHTML = `Error: ${error.message}`; } finally { button.disabled = false; button.textContent = button.textContent.replace('Launching...', button.getAttribute('data-original-text') || 'Launch'); if (!button.getAttribute('data-original-text')) { button.setAttribute('data-original-text', button.textContent); } } }); }); ``` -------------------------------- ### Configure Django Settings for dj-celery-panel Source: https://context7.com/yassi/dj-celery-panel/llms.txt Configures Django's settings.py to include dj-celery-panel and necessary Celery settings. It also shows optional configuration for custom backends for tasks, workers, and queues. ```python # settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django_celery_results', # Required for task history 'dj_celery_panel', # ... your other apps ] # Celery Configuration CELERY_BROKER_URL = 'redis://localhost:6379/0' CELERY_RESULT_BACKEND = 'django-db' CELERY_TASK_TRACK_STARTED = True CELERY_RESULT_EXTENDED = True # Optional: Configure backends DJ_CELERY_PANEL_SETTINGS = { "tasks_backend": "dj_celery_panel.celery_utils.CeleryTasksDjangoCeleryResultsBackend", "workers_backend": "dj_celery_panel.celery_utils.CeleryWorkersInspectBackend", "queues_backend": "dj_celery_panel.celery_utils.CeleryQueuesInspectBackend", } ``` -------------------------------- ### Launch Failing Task Form Source: https://github.com/yassi/dj-celery-panel/blob/main/example_project/app/templates/task_launcher.html This section allows users to launch a Celery task that is designed to fail with a specified error message. It includes an input field for the error message and a launch button. ```html ❌ Failing Task -------------- Launch a task that will fail with an error Error Message Launch Failing Task ``` -------------------------------- ### Custom Celery Tasks Backend Configuration Source: https://context7.com/yassi/dj-celery-panel/llms.txt Defines a custom backend for fetching Celery tasks from an external monitoring API. It includes configurations for task filtering and retrieval logic. This backend integrates with dj-celery-panel by specifying its path in the DJ_CELERY_PANEL_SETTINGS. ```python from dj_celery_panel.celery_utils import TaskListPage, TaskDetailPage class CustomTasksBackend: """Custom backend that fetches tasks from an external monitoring API.""" # Backend metadata displayed in the UI BACKEND_DESCRIPTION = "Aggregated task data from custom monitoring" DATA_SOURCE = "External Monitoring API" # Filter configuration DEFAULT_FILTER = None # Default filter when none specified AVAILABLE_FILTERS = [ {"value": None, "label": "All"}, {"value": "running", "label": "Running"}, {"value": "completed", "label": "Completed"}, {"value": "failed", "label": "Failed"}, ] def __init__(self, app): """Initialize with Celery app instance.""" self.app = app self.api_client = MyMonitoringClient() # Assuming MyMonitoringClient is defined elsewhere def get_tasks(self, search_query=None, page=1, per_page=50, filter_type=None): """Fetch task list from custom source.""" # Query external API response = self.api_client.get_tasks( query=search_query, status=filter_type, offset=(page - 1) * per_page, limit=per_page ) # Transform to expected format tasks = [ { "id": t["task_id"], "name": t["task_name"], "status": t["state"], "result": t.get("result"), "date_created": t["created_at"], "date_done": t.get("completed_at"), "worker": t.get("worker_hostname"), "args": t.get("arguments"), "kwargs": t.get("keyword_arguments"), } for t in response["tasks"] ] total = response["total_count"] total_pages = (total + per_page - 1) // per_page return TaskListPage( tasks=tasks, total_count=total, page=page, per_page=per_page, total_pages=total_pages, has_previous=page > 1, has_next=page < total_pages, previous_page=page - 1 if page > 1 else None, next_page=page + 1 if page < total_pages else None, ) def get_task_detail(self, task_id): """Fetch detailed task information.""" task = self.api_client.get_task(task_id) if not task: return TaskDetailPage(task=None, error="Task not found") return TaskDetailPage(task={ "id": task["task_id"], "name": task["task_name"], "status": task["state"], "result": task.get("result"), "traceback": task.get("exception_traceback"), "date_created": task["created_at"], "date_done": task.get("completed_at"), "worker": task.get("worker_hostname"), "args": task.get("arguments"), "kwargs": task.get("keyword_arguments"), "duration": task.get("execution_time_seconds"), }) # Register in settings.py DJ_CELERY_PANEL_SETTINGS = { "tasks_backend": "myapp.celery_backends.CustomTasksBackend", "workers_backend": "dj_celery_panel.celery_utils.CeleryWorkersInspectBackend", "queues_backend": "dj_celery_panel.celery_utils.CeleryQueuesInspectBackend", } ``` -------------------------------- ### Display Configuration String Values in Django Template Source: https://github.com/yassi/dj-celery-panel/blob/main/dj_celery_panel/templates/admin/dj_celery_panel/_config_value.html This Django template tag renders configuration labels and values. It handles cases where the value might be empty by displaying a fallback or a default 'Not configured' message. Optional parameters allow for displaying suffixes, code snippets, and help text. ```django {% load i18n %} {% comment %} Reusable partial for displaying configuration string values. Parameters: - label: The configuration label (e.g., "Broker:") - value: The value to display - fallback: (optional) Text to show if value is empty/None (default: "Not configured") - code: (optional) Code/URL to display after the value - suffix: (optional) Suffix to append to the value (e.g., "s" for seconds) - help_text: (optional) Explanation text to show below the value {% endcomment %}* **{{ label }}** {% if value %} {{ value }}{% if suffix %}{{ suffix }}{% endif %} {% if code %} `{{ code }}` {% endif %} {% else %} {% if fallback %}{{ fallback }}{% else %}{% trans 'Not configured' %}{% endif %} {% endif %} {% if help_text %} {{ help_text }} {% endif %} ``` -------------------------------- ### Define Custom Tasks Backend in Python Source: https://github.com/yassi/dj-celery-panel/blob/main/docs/configuration.md Implement a custom backend to fetch and manage Celery tasks from external sources or custom databases. This class defines metadata, filtering options, and methods for retrieving task lists and details. ```python from dj_celery_panel.celery_utils import TaskListPage, TaskDetailPage class CustomTasksBackend: """ Custom backend that fetches tasks from an external API or custom database. """ # Backend metadata displayed in the UI BACKEND_DESCRIPTION = "Aggregated task data with custom filtering" DATA_SOURCE = "External Monitoring API" # Define default filter behavior DEFAULT_FILTER = None # or a specific default like "running" # Define available filters for the UI AVAILABLE_FILTERS = [ {"value": None, "label": "All"}, {"value": "running", "label": "Running"}, {"value": "completed", "label": "Completed"}, {"value": "failed", "label": "Failed"}, ] def __init__(self, app): self.app = app def get_tasks(self, search_query=None, page=1, per_page=50, filter_type=None): """ Fetch task list from your custom source. Args: search_query: Optional search string page: Page number per_page: Items per page filter_type: Filter value (or None for default) Returns: TaskListPage: Object containing tasks and pagination info """ # Your custom implementation # Example: Fetch from external monitoring service tasks = self.fetch_from_custom_source(search_query, page, per_page, filter_type) return TaskListPage( tasks=tasks, total_count=len(tasks), page=page, per_page=per_page, ) def get_task_detail(self, task_id): """ Fetch detailed information about a specific task. Returns: TaskDetailPage: Object containing task details """ task = self.fetch_task_detail(task_id) return TaskDetailPage( task=task, error=None if task else "Task not found" ) def fetch_from_custom_source(self, search_query, page, per_page, filter_type): # Your custom implementation pass ``` -------------------------------- ### Queue Management API Source: https://context7.com/yassi/dj-celery-panel/llms.txt Endpoints for retrieving information about Celery queues, including lists of active queues and details for specific queues. ```APIDOC ## Get Queues List ### Description Retrieves a list of all active Celery queues and their associated information. ### Method GET ### Endpoint /queues ### Response #### Success Response (200) - **queues** (array) - A list of dictionaries, each representing a queue with its name, exchange, routing key, workers, and message count. #### Response Example ```json { "queues": [ { "name": "celery", "exchange": "celery", "routing_key": "celery", "workers": [ "celery@host1", "celery@host2" ], "message_count": 42, "broker_query_error": null } ] } ``` ## Get Queue Detail ### Description Retrieves detailed information about a specific Celery queue. ### Method GET ### Endpoint /queues/{queue_name} ### Parameters #### Path Parameters - **queue_name** (string) - Required - The name of the queue to retrieve details for. ### Response #### Success Response (200) - **queue** (object) - A dictionary containing detailed information about the queue, including name, exchange, durability, message count, and consumer count. #### Response Example ```json { "queue": { "name": "celery", "exchange": "celery", "exchange_type": "direct", "routing_key": "celery", "durable": true, "auto_delete": false, "workers": [ "celery@host1" ], "message_count": 42, "consumer_count": 2 } } ``` ``` -------------------------------- ### Display Backend Information (Django Template) Source: https://github.com/yassi/dj-celery-panel/blob/main/dj_celery_panel/templates/admin/dj_celery_panel/_backend_info.html This snippet conditionally displays backend information, specifically the backend's name and data source. It relies on the `backend_info` variable being available in the template context and uses Django's `i18n` tags for potential internationalization. ```html {% load i18n %} {% if backend_info %} Backend: `{{ backend_info.name }}` — {{ backend_info.data_source }} {% endif %} ``` -------------------------------- ### Celery Beat Schedule Configuration Source: https://context7.com/yassi/dj-celery-panel/llms.txt Configures periodic tasks using Celery Beat. This involves defining tasks and their schedules in the `CELERY_BEAT_SCHEDULE` setting. It supports fixed intervals and cron-like scheduling using `crontab`. ```python # settings.py from celery.schedules import crontab CELERY_BEAT_SCHEDULE = { # Run every 30 seconds "send-notification-every-30-seconds": { "task": "app.tasks.send_periodic_notification", "schedule": 30.0, }, # Run every 5 minutes "health-check-every-5-minutes": { "task": "app.tasks.health_check", "schedule": 300.0, }, # Run every hour at minute 0 "generate-report-hourly": { "task": "app.tasks.generate_hourly_report", "schedule": crontab(minute=0), }, # Run daily at midnight "cleanup-old-results-daily": { "task": "app.tasks.cleanup_old_results", "schedule": crontab(hour=0, minute=0), }, } ``` -------------------------------- ### Alternative Tasks Backend: CeleryTasksInspectBackend Source: https://github.com/yassi/dj-celery-panel/blob/main/docs/configuration.md Configures Django Celery Panel to use the `CeleryTasksInspectBackend` for the tasks interface. This backend uses Celery's inspect API for real-time monitoring of active tasks, requiring no database storage for task history. ```python DJ_CELERY_PANEL_SETTINGS = { "tasks_backend": "dj_celery_panel.celery_utils.CeleryTasksInspectBackend", } ```