Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# Distribution / packaging
.Python
.venv
env/
bin/
build/
develop-eggs/
dist/
eggs/
lib64/
parts/
sdist/
var/
.vscode/
*.egg-info/
.installed.cfg
*.egg

# vim
*.swp
64 changes: 61 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,66 @@
# Celery on Render
# Celery Task Queue with Flower

This repo can be used to deploy the [Celery](https://github.com/celery/celery) distributed task queue on [Render](https://render.com). It also includes deployment instructions for [Flower](https://github.com/mher/flower), a web monitoring frontend for Celery.
A modern Flask + Celery distributed task queue with Flower monitoring dashboard.

## Tech Stack

- **Flask 3.0** - Web framework
- **Celery 5.3** - Distributed task queue
- **Flower 2.0** - Real-time Celery monitoring
- **Redis** - Message broker & result backend

## Local Development

### Prerequisites

- Python 3.10+
- Redis server running locally

### Setup

```bash
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Set environment variables (optional)
export CELERY_BROKER_URL=redis://localhost:6379/0
export CELERY_RESULT_BACKEND=redis://localhost:6379/0
export FLASK_SECRET_KEY=your-secret-key
```

### Running

Start each service in a separate terminal:

```bash
# Terminal 1: Start Redis (if not running as a service)
redis-server

# Terminal 2: Start Celery worker
celery -A tasks worker --loglevel=info

# Terminal 3: Start Flower monitoring
celery -A tasks flower --port=5555

# Terminal 4: Start Flask app
flask run
```

Then visit:
- **Web App**: http://localhost:5000
- **Flower Dashboard**: http://localhost:5555

## Deployment

Follow the guide at https://render.com/docs/deploy-celery.
Follow the guide at https://render.com/docs/deploy-celery for production deployment on Render.

## Features

- Asynchronous task execution
- Real-time task monitoring via Flower
- Task result tracking with Redis backend
- Modern, responsive UI
29 changes: 19 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import os
from flask import Flask, flash, render_template, redirect, request

from flask import Flask, flash, redirect, render_template, request

from tasks import add

app = Flask(__name__)
app.secret_key = os.getenv('FLASK_SECRET_KEY', "super-secret")
app.secret_key = os.getenv("FLASK_SECRET_KEY", "super-secret")


@app.route('/')
@app.route("/")
def main():
return render_template('main.html')
return render_template("main.html")


@app.route('/add', methods=['POST'])
@app.route("/add", methods=["POST"])
def add_inputs():
x = int(request.form['x'])
y = int(request.form['y'])
add.delay(x, y)
flash("Your addition job has been submitted.")
return redirect('/')
try:
x = int(request.form["x"])
y = int(request.form["y"])
result = add.delay(x, y)
flash(f"Job submitted! Task ID: {result.id[:8]}...")
except (ValueError, KeyError):
flash("Please enter valid numbers for both X and Y.")
return redirect("/")


if __name__ == "__main__":
app.run(debug=True)
15 changes: 6 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
celery==4.3.0
Flask==1.0.2
flower==0.9.3
gunicorn==19.9.0
redis==3.2.1

# NOTE: Kombu 4.6.5 results in a build failure. Bumping down to 4.6.4
# See this github issue: https://github.com/celery/kombu/issues/1063
kombu==4.6.4
celery==5.3.6
Flask==3.0.0
flower==2.0.1
gunicorn==21.2.0
redis==5.0.1
kombu==5.3.4
24 changes: 21 additions & 3 deletions tasks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import os

from celery import Celery

app = Celery('tasks', broker=os.getenv("CELERY_BROKER_URL"))
# Configure Celery with broker and optional result backend
app = Celery(
"tasks",
broker=os.getenv("CELERY_BROKER_URL", "redis://localhost:6379/0"),
backend=os.getenv("CELERY_RESULT_BACKEND", "redis://localhost:6379/0"),
)

# Celery configuration
app.conf.update(
task_serializer="json",
accept_content=["json"],
result_serializer="json",
timezone="UTC",
enable_utc=True,
task_track_started=True,
result_expires=3600,
)


@app.task
def add(x, y):
@app.task(bind=True)
def add(self, x, y):
"""Add two numbers asynchronously."""
return x + y
2 changes: 1 addition & 1 deletion templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ <h1 class="mb-5">Asynchronous addition using Celery!</h1>
</div>
</div>
</body>
</html>
</html>