Using Redis Background Task Controller with Lock by Args

Example

import asyncio
import os

from apidaora import appdaora, route


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


@route.background(
    '/hello-single', lock_args=True, 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

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 tasks

Creating the task three times, getting error just on same arguments:

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

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

HTTP/1.1 303 See Other
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
location: hello-single?task_id=b'4ee301eb-6487-48a0-b6ed-e5f576accfc2'
x-apidaora-lock: args-signature
transfer-encoding: chunked



HTTP/1.1 202 Accepted
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 169

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

Quering

Quering the server for the tasks:

curl -i localhost:8000/hello-single?task_id=4ee301eb-6487-48a0-b6ed-e5f576accfc2
echo && echo
curl -i localhost:8000/hello-single?task_id=5ee301eb-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: 169

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

HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 169

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

Wait the tasks to finish and get results:

sleep 1
curl -i localhost:8000/hello-single?task_id=4ee301eb-6487-48a0-b6ed-e5f576accfc2
echo && echo
curl -i localhost:8000/hello-single?task_id=5ee301eb-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: 230

{"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":"de01341028ac"}

HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 231

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

Creating another task

Now the task should be able to run again

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

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

Quering

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

curl -i localhost:8000/hello-single?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: 169

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

Wait the task to finish and get results:

sleep 1 && curl -i localhost:8000/hello-single?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: 230

{"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":"de01341028ac"}