> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sevalla.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> These are the docs for Sevalla, a platform for deploying applications, databases, static sites, object storage and load balancers. The dashboard is at https://app.sevalla.com.
> Bold text in steps is an exact dashboard label.
> To act on a Sevalla account, prefer the Sevalla MCP server (https://mcp.sevalla.com/mcp, see https://docs.sevalla.com/ai-agents/overview.md), then the Sevalla CLI (https://docs.sevalla.com/developer-tools/cli.md), then the REST API (https://api-docs.sevalla.com, OpenAPI spec at https://api.sevalla.com/v3/openapi.json).
> Every page index is at https://docs.sevalla.com/llms.txt.

# Run Flask background tasks

> Run Flask background tasks with Celery on Sevalla: the Flask application context in every task, a Redis database as the broker, and a background worker process for the queue.

A background task is work that runs outside the request, such as sending email or resizing images. On Sevalla, Celery runs the tasks in a background worker process, built from the same image as the web process, and a Redis database holds the queue between them. The setup follows Flask's own Celery pattern, so every task runs inside the Flask application context, with access to `db` and the configuration.

## Before you begin

* A Flask project deployed on Sevalla, as in [Deploy Flask](/guides/python/flask/deploy).

## Create a Redis database

<Steps>
  <Step title="Create the database">
    In the sidebar, click **Databases**, then click **Create database**. Under **Database**, choose **Redis**, or **Valkey**: the code in this guide works with both. Enter `storefront-redis` as the **Display name**, choose the same **Data center** as the application and a **Size**, then click **Create database**.
  </Step>

  <Step title="Connect it to the application">
    In the sidebar, click **Applications**, then click the application. Click the **Networking** tab. Under **Connected services**, click **Add internal connection**. Choose `storefront-redis` as the **Service**, turn on **Add environment variables to the application**, and click **Add internal connection**. Sevalla adds `REDIS_HOST`, `REDIS_PORT`, `REDIS_PASSWORD` and `REDIS_URL`. See [Connect an application](/databases/connect#connect-an-application).
  </Step>
</Steps>

## Add Celery to the project

Install Celery with its Redis support, and update `requirements.txt`:

```bash theme={null}
pip install "celery[redis]"
pip freeze > requirements.txt
```

Add the function that creates the Celery application from the Flask one:

```python storefront/celery.py theme={null}
from celery import Celery, Task
from flask import Flask


def celery_init_app(app: Flask) -> Celery:
    class FlaskTask(Task):
        def __call__(self, *args: object, **kwargs: object) -> object:
            with app.app_context():
                return self.run(*args, **kwargs)

    celery_app = Celery(app.name, task_cls=FlaskTask)
    celery_app.config_from_object(app.config["CELERY"])
    celery_app.set_default()
    app.extensions["celery"] = celery_app
    return celery_app
```

Add a task:

```python storefront/tasks.py theme={null}
from celery import shared_task


@shared_task
def send_order_confirmation(order_id: int) -> None:
    print(f"Sending the confirmation for order {order_id}")
```

Configure Celery with the Redis URL in the application factory, and add a route that queues the task:

```python storefront/__init__.py theme={null}
def create_app() -> Flask:
    app = Flask(__name__)
    app.config["SQLALCHEMY_ENGINES"] = {"default": database_url()}
    # A Valkey database's URL starts with valkey://, which Celery doesn't accept.
    redis_url = os.environ.get("REDIS_URL", "redis://localhost:6379/0")
    app.config["CELERY"] = {
        "broker_url": redis_url.replace("valkey://", "redis://", 1),
        "broker_connection_retry_on_startup": True,
        "task_ignore_result": True,
    }
    app.config.from_prefixed_env()

    db.init_app(app)
    alembic.init_app(app)
    celery_init_app(app)

    from storefront.tasks import send_order_confirmation

    @app.post("/orders/<int:order_id>/confirm")
    def confirm_order(order_id):
        send_order_confirmation.delay(order_id)
        return {"status": "queued"}

    # ...the other routes...
    return app
```

Import `celery_init_app` at the top with `from storefront.celery import celery_init_app`. Then create the worker's entry point in the repository root:

```python make_celery.py theme={null}
from storefront import create_app

flask_app = create_app()
celery_app = flask_app.extensions["celery"]
```

Commit and push the changes.

## Run the worker on Sevalla

<Steps>
  <Step title="Create the background worker">
    On the application's **Processes** tab, click **Create process**. Choose **Background worker**, enter `Celery worker` as the **Name** and this **Custom start command**:

    ```bash theme={null}
    celery -A make_celery worker --loglevel INFO --concurrency 2
    ```

    Choose a **Resource** and click **Create process**.
  </Step>

  <Step title="Deploy">
    Click **Deploy** in the undeployed changes bar at the bottom of the page.
  </Step>
</Steps>

Set `--concurrency` to match the size. Without it, Celery starts one worker process per CPU of the machine it runs on, which can be more than the process's memory holds. Two processes fit in **H1** (0.3 GB of memory). To process more tasks at once, add instances in the process's **Scaling** section, or choose a larger size and raise `--concurrency`. See [Scaling](/applications/scaling).

<Frame>
  <img className="block dark:hidden" src="https://assets.sevalla.com/screenshots/light/app-processes-worker-create.png" alt="The Create process sheet with Background worker selected, the Name and Custom start command fields, the resource and the scaling settings" width="768" />

  <img className="hidden dark:block" src="https://assets.sevalla.com/screenshots/dark/app-processes-worker-create.png" alt="The Create process sheet with Background worker selected, the Name and Custom start command fields, the resource and the scaling settings" width="768" />
</Frame>

## Run tasks on a schedule

Run scheduled work as a cron job: add a command to the Flask CLI with `@app.cli.command()`, then create a **Cron job** process with a **Schedule** in cron syntax, such as `0 3 * * *` for every day at 03:00 UTC, and the command as its **Custom start command**, such as `flask --app storefront cleanup`. See [Cron jobs](/applications/processes#cron-jobs).

## Verify

1. Queue a task, with your application's domain:

   ```bash theme={null}
   curl -X POST https://storefront-x7k2m.sevalla.app/orders/42/confirm
   ```

   The response is `{"status":"queued"}`.
2. Click the **Logs** tab and look at the worker's runtime logs. They show `Task storefront.tasks.send_order_confirmation[...] received`, the line `Sending the confirmation for order 42`, and `Task storefront.tasks.send_order_confirmation[...] succeeded`.

On the **Processes** tab, the `Celery worker` process shows **Running**. If it shows **Crashed**, the logs show why, for example `Error 111 connecting` when `REDIS_URL` is missing or wrong.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.