tc-thinh / GitGPS-3.0

3 stars 0 forks source link

S-009: Research a way to run a local Redis server that is attached to our FastAPI backend server #14

Open tc-thinh opened 1 month ago

tc-thinh commented 1 month ago

The current stack will use Redis Queue to handle our requests, the workflow is expected to be as bellow:

from flask import Flask, request, jsonify
from rq import Queue
from redis import Redis
from time import sleep

app = Flask(__name__)
redis_conn = Redis()
q = Queue(connection=redis_conn)

def background_task(data):
    """Simulate a long-running task."""
    sleep(300)  # Simulates a 5-minute task
    return f"Processed {data}"

@app.route('/submit', methods=['POST'])
def submit():
    """Receive data from the front end and enqueue the task."""
    data = request.get_json()
    job = q.enqueue(background_task, data)
    return jsonify({'job_id': job.id, 'status': 'submitted'})

if __name__ == '__main__':
    app.run(debug=True)