Using Background Task Controller with Redis Backend

Example

import asyncio
import os

from apidaora import appdaora, route


REDIS_URI = f"redis://{os.environ.get('REDIS', '')}"


@route.background('/hello', tasks_repository_uri=REDIS_URI)
async def hello_task(name: str) -> str:
    await asyncio.sleep(1)
    return f'Hello {name}!'


app = appdaora(hello_task)

Running

You will need a redis server to run this example.

Running the server:

uvicorn myapp:app
INFO: Started server process [16220]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Creating task

Creating the task:

curl -X POST -i localhost:8000/hello?name=Me
HTTP/1.1 202 Accepted
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 159

{"task_id":"4ee301eb-6487-48a0-b6ed-e5f576accfc2","start_time":"1970-01-01T00:00:00+00:00","status":"running","signature":"aedb1ee4c3c7","args_signature":null}

Quering

Quering the server for the task (You must replace the task_id with the server output):

curl -i localhost:8000/hello?task_id=4ee301eb-6487-48a0-b6ed-e5f576accfc2
HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 159

{"task_id":"4ee301eb-6487-48a0-b6ed-e5f576accfc2","start_time":"1970-01-01T00:00:00+00:00","status":"running","signature":"aedb1ee4c3c7","args_signature":null}

Wait the task to finish and get results:

sleep 1 && curl -i localhost:8000/hello?task_id=4ee301eb-6487-48a0-b6ed-e5f576accfc2
HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 220

{"end_time":"1970-01-01T00:00:00+00:00","result":"Hello Me!","task_id":"4ee301eb-6487-48a0-b6ed-e5f576accfc2","start_time":"1970-01-01T00:00:00+00:00","status":"finished","signature":"aedb1ee4c3c7","args_signature":null}