Skip to content

Best Seller Task and Configuration Documentation

This document provides an explanation of the Celery tasks and configurations related to managing best seller metrics and rankings in the system, outlining their functionality and scheduling details.


Celery Setup Overview

Celery is configured to manage asynchronous tasks within the project. The Celery app is initialized in celery.py, which:

  • Sets the default Django settings module.
  • Configures Celery to use Django settings under the CELERY namespace.
  • Auto-discovers tasks from installed Django apps.
  • Defines periodic task schedules using beat_schedule.

Tasks Description

1. Update Best Seller Metrics

This task updates the best seller metrics for the products based on recent sales and rankings: - Runs every 30 minutes to keep the rankings and metrics up to date. - Ensures products' best seller metrics are refreshed for accurate ranking and sales data.

Key Points: - Ensures that best seller rankings are dynamically updated based on sales. - Helps keep the best seller lists relevant and accurate in real-time. - Operates on a recurring schedule for constant updates.

2. Update Best Seller Rankings

This task updates only the bestseller rankings for products: - Updates rankings based on predefined metrics. - Clears related caches to ensure the updated rankings are reflected immediately across the application.

Key Points: - Only updates rankings without modifying other metrics. - Uses cache clearing to prevent stale data from being served. - Provides an efficient method to keep rankings current without overloading the system.


Helper Function: Retrieve Configuration Values

A helper function is used to fetch custom configuration values from the CustomConfig table. If a value is not found, a default is returned. This is critical for dynamic control of best seller ranking updates and cache clearing periods.


Task Scheduling

Celery Beat is used to schedule tasks at predefined intervals. The schedules are defined in beat_schedule and include:

Scheduled Tasks:

  • Update Best Seller Metrics:
  • Runs every 30 minutes.
  • Updates the best seller metrics for products, keeping rankings and sales data up to date.

  • Update Best Seller Rankings:

  • Triggered manually by the task update_bestseller_rankings.
  • Updates rankings and clears the associated cache to ensure that the freshest ranking data is available.

Error Handling and Logging

Each task includes mechanisms to: - Log task execution results (e.g., count of updated metrics and rankings). - Handle unexpected errors gracefully to ensure system stability. - Prevent redundant processing by filtering data carefully.