task-patterns
Production-ready Celery task templates with error handling, retries, rate limiting, time limits, and custom task classes. Use when creating Celery tasks, implementing retry logic, adding rate limiting, setting time limits, building custom task classes, validating task inputs with Pydantic, handling database operations, making API calls, or when user mentions task patterns, retry mechanisms, task templates, error handling, task best practices.
What this skill does
# Task Patterns Skill
Production-ready Celery task templates with comprehensive error handling, retry mechanisms, rate limiting, and custom behavior patterns.
## Overview
This skill provides battle-tested templates and patterns for building robust Celery tasks. Each template demonstrates production-ready code with proper error handling, logging, retry logic, and security best practices.
## Core Patterns
### 1. Basic Task Template
**Template**: `templates/basic-task.py`
Simple task with standard error handling and logging.
**Use when**:
- Creating your first Celery task
- Need straightforward task execution
- No complex retry or limiting requirements
**Features**:
- Proper logging setup
- Basic error handling
- Synchronous and asynchronous execution examples
- Clear docstrings
### 2. Retry Task Template
**Template**: `templates/retry-task.py`
Tasks with automatic retry mechanisms and exponential backoff.
**Use when**:
- Calling external APIs that may fail transiently
- Database operations that could timeout
- Network operations prone to connection issues
**Features**:
- Automatic retry with `autoretry_for`
- Exponential backoff with jitter
- Manual retry control
- Fixed delay retries
- Configurable max retries
**Example**: See `examples/task-with-retries.md` for complete implementation guide
### 3. Rate Limited Task Template
**Template**: `templates/rate-limited-task.py`
Tasks with rate limiting to control execution speed.
**Use when**:
- Respecting external API rate limits
- Protecting database from overload
- Controlling resource consumption
- Meeting SLA requirements
**Features**:
- Per-second, per-minute, per-hour limits
- Batch processing with rate control
- Large dataset handling
- Dynamic rate limit adjustment
**Example**: See `examples/rate-limiting.md` for complete guide
### 4. Time Limited Task Template
**Template**: `templates/time-limited-task.py`
Tasks with soft and hard time limits to prevent runaway execution.
**Use when**:
- Long-running operations that could hang
- Operations with external dependencies
- Tasks that must complete within timeframe
- Preventing resource exhaustion
**Features**:
- Soft time limit (catchable)
- Hard time limit (force kill)
- Graceful timeout handling
- Progress saving on timeout
- Combined with retry logic
### 5. Custom Task Class Template
**Template**: `templates/custom-task-class.py`
Custom task base classes with specialized behavior.
**Use when**:
- Need database connection pooling
- Want to cache task results
- Require metrics tracking
- Need shared resources across tasks
- Implementing lifecycle hooks
**Features**:
- Database connection pooling
- Automatic result caching
- Metrics and monitoring
- Resource pool management
- Lifecycle hooks (before_start, on_success, on_failure, on_retry, after_return)
**Example**: See `examples/custom-task-classes.md` for comprehensive guide
### 6. Pydantic Validation Template
**Template**: `templates/pydantic-validation.py`
Type-safe tasks with Pydantic model validation.
**Use when**:
- Need strict input validation
- Want type safety
- Complex nested data structures
- API contract enforcement
**Features**:
- Pydantic models for input validation
- Enum types for constrained values
- Custom validators
- Standardized result format
- Comprehensive error messages
### 7. Database Task Template
**Template**: `templates/database-task.py`
Best practices for database operations in tasks.
**Use when**:
- Performing database queries
- Bulk database operations
- Transactional operations
- Database migrations
**Features**:
- Connection pooling
- Parameterized queries (SQL injection prevention)
- Transaction management
- Bulk insert operations
- Pagination support
- Aggregation queries
### 8. API Task Template
**Template**: `templates/api-task.py`
Best practices for external API calls in tasks.
**Use when**:
- Calling external APIs
- Webhook delivery
- Paginated API fetching
- Batch API calls
- API authentication
**Features**:
- Automatic retry on connection errors
- Rate limiting for API quotas
- Timeout handling
- Authentication patterns (Bearer, API key)
- Pagination support
- Batch processing
## Scripts
### generate-task.sh
**Usage**: `./scripts/generate-task.sh <template_name> <output_file> [task_name]`
Generate new Celery task from template with customizations.
**Available templates**:
- basic-task
- retry-task
- rate-limited-task
- time-limited-task
- custom-task-class
- pydantic-validation
- database-task
- api-task
**Example**:
```bash
./scripts/generate-task.sh retry-task tasks/my_api_call.py fetch_data
```
### test-task.sh
**Usage**: `./scripts/test-task.sh <task_file.py> [task_name]`
Test Celery task by:
1. Validating Python syntax
2. Checking imports
3. Verifying task structure
4. Security scanning
5. Best practices check
6. Optionally running task
**Example**:
```bash
./scripts/test-task.sh templates/retry-task.py fetch_api_data
```
### validate-task.sh
**Usage**: `./scripts/validate-task.sh <task_file.py>`
Comprehensive validation including:
- Required elements (Celery import, app initialization, decorators)
- Best practices (docstrings, type hints, error handling, logging)
- Retry configuration
- Security checks (no hardcoded credentials, SQL injection)
- Performance checks (rate limits, time limits)
- Code quality (examples, configuration)
- Pattern detection
**Example**:
```bash
./scripts/validate-task.sh templates/api-task.py
```
## Usage Examples
### Example 1: Create API Task with Retries
```python
# Use retry-task.py template
from templates.retry_task import fetch_api_data
# Queue task
result = fetch_api_data.delay('https://api.example.com/data')
# Get result
data = result.get(timeout=60)
print(data)
```
### Example 2: Rate-Limited Batch Processing
```python
# Use rate-limited-task.py template
from templates.rate_limited_task import api_call_rate_limited
# Process 100 items at 10/minute rate
for i in range(100):
api_call_rate_limited.delay(f'/endpoint/{i}')
# Celery automatically enforces rate limit
```
### Example 3: Database Operations with Connection Pooling
```python
# Use database-task.py template
from templates.database_task import insert_record, bulk_insert
# Single insert
result1 = insert_record.delay('users', {
'name': 'John Doe',
'email': '[email protected]'
})
# Bulk insert
records = [
{'name': 'Jane', 'email': '[email protected]'},
{'name': 'Bob', 'email': '[email protected]'},
]
result2 = bulk_insert.delay('users', records)
```
### Example 4: Custom Task with Metrics
```python
# Use custom-task-class.py template
from templates.custom_task_class import monitored_task
# Automatic metrics tracking
result = monitored_task.delay(123)
# Metrics logged automatically:
# - Start time
# - Duration
# - Success/failure
# - Arguments
```
### Example 5: Type-Safe Task with Pydantic
```python
# Use pydantic-validation.py template
from templates.pydantic_validation import process_user
# Valid data
user_data = {
'user_id': 123,
'email': '[email protected]',
'username': 'john_doe',
'age': 30,
'tags': ['premium']
}
result = process_user.delay(user_data)
# Invalid data returns structured error
invalid_data = {
'user_id': -1, # Invalid
'email': 'not-email', # Invalid
}
result = process_user.delay(invalid_data)
# Returns: {'status': 'error', 'errors': [...]}
```
## Best Practices
### 1. Always Use Placeholders for Credentials
```python
# ✅ CORRECT
api_key = os.getenv('API_KEY', 'your_api_key_here')
# ❌ WRONG
api_key = 'sk-abc123xyz456'
```
### 2. Set Appropriate Timeouts
```python
# ✅ CORRECT
response = requests.get(url, timeout=30)
# ❌ WRONG
response = requests.get(url) # Can hang forever
```
### 3. Use Parameterized Queries
```python
# ✅ CORRECT
db.execute("SELECT * FROM users WHERE id = %s", (user_id,))
# ❌ WRONG
db.execute(f"SELECT * FROM users WHERE id = {user_id}")
```
### 4. Combine Retry with Rate Limiting
```python
@appRelated in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.