Skip to content

Tasks & Utils

Separates time-consuming , external , and cross-app logic into:

  • Celery Tasks → background execution
  • Utils → reusable business helpers

This keeps views thin , signals safe , and flows predictable .


PART A — CELERY TASKS (Background Execution)

Tasks run asynchronously using:

  • Redis (broker)
  • Celery workers
  • Celery Beat (scheduler)

1. Email Sending Task

Task

send_email_task

Triggered by

  • Email verification flow
  • Password reset flow

What it does

  • Sends transactional email via Resend
  • Retries automatically on failure

Why background?

  • Email delivery can be slow
  • Should not block user requests

Failure behavior

  • Retries up to 3 times
  • Logs error on final failure

2. Database Backup Task

Task

run_db_backup

Triggered by

  • Celery Beat (scheduled)
  • Manual trigger (if needed)

What it does

  • Runs django-dbbackup
  • Uploads DB dump to storage (local or S3)

Why background?

  • Database dump is heavy I/O
  • Should not affect live traffic

Failure behavior

  • Retries with backoff
  • Logs full traceback

3. Telegram Support Notification Task

Task

notify_seller_support

Triggered by

  • New support request creation

What it does

  • Sends formatted support alert to seller’s Telegram
  • Includes:
  • User email
  • Product
  • Message
  • Timestamp

Why background?

  • External HTTP call
  • Telegram downtime should not block support creation

Failure behavior

  • Retries on network failure
  • Silent retry without breaking UX

PART B — UTILS (Synchronous Helper Logic)

Utils are pure helpers :

  • No side effects
  • No external calls
  • Safe to reuse across apps

1. Access Control Utilities (Payments)

user_has_access

Purpose

  • Check if user owns a product

Used in

  • Download logic
  • UI decisions

Why exists

  • Centralized ownership rule
  • Prevents logic duplication

get_user_purchased_product

Purpose

  • Fetch all purchased products for a user

Used in

  • Dashboards
  • Listing logic

2. Support Utilities

has_active_support_request

Purpose

  • Check if user already has an active support request for a product

Used in

  • Support request creation
  • UI disabling logic

Why exists

  • Enforces “one active support per product” rule

3. Seller Analytics Utilities

Utilities

  • get_total_customers
  • get_current_month_revenue
  • get_revenue_last_12_months
  • get_latest_buyers

Purpose

  • Compute seller dashboard metrics

Why utils?

  • Reused by:
  • Dashboard view
  • API view
  • Keeps views lightweight

4. Short URL Helpers

generate_unique_slug

Purpose

  • Generate collision-safe short URLs

Why util-like

  • Used implicitly during model save
  • Deterministic and reusable

PART C — TASK VS SIGNAL VS UTIL (IMPORTANT)

Logic Type Where it lives Why
External calls Celery Tasks Async & safe
Cross-system sync Signals Automatic
Business rules Utils Reusable
UI orchestration Views Thin

This separation is intentional .


PART D — FAILURE & RETRY STRATEGY

Background Tasks

  • Automatic retry
  • Exponential backoff
  • Logging on final failure

Utils

  • No retry
  • Fail fast
  • Caller decides behavior

PART E — CUSTOMIZATION GUIDELINES

Safe to extend

  • Add new Celery tasks
  • Add new analytics utils
  • Add new notification channels

Unsafe changes

  • Moving API calls into utils
  • Heavy logic inside signals
  • Making utils stateful

Execution Summary

Area Role
Celery Tasks Async execution
Utils Pure helpers
Reliability Retry-safe
Coupling Minimal
Scalability High