-
Notifications
You must be signed in to change notification settings - Fork 152
Celery redis setup and Jobs endpoint #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chetanr25
wants to merge
7
commits into
fireform-core:development
Choose a base branch
from
chetanr25:celery_redis_setup
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
83e0ec1
feat: add celery and redis to docker compose to pull celery & redis d…
chetanr25 2bdc700
configured celery to FireForm
chetanr25 53f36d9
added required schemas for asuncformfill and to track celery jobs
chetanr25 436b15c
feat: add universal job tracking with UUID, progress, and structured …
chetanr25 838b85a
added make command to see logs for workers
chetanr25 77661fc
added init.py for tasks
chetanr25 60e1ce5
added tests for celery workers
chetanr25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| from fastapi import APIRouter, Depends | ||
| from sqlmodel import Session | ||
|
|
||
| from app.api.deps import get_db | ||
| from app.api.schemas.forms import ( | ||
| AsyncFormFill, | ||
| AsyncFormFillResponse, | ||
| AsyncJobSubmitResponse, | ||
| JobResponse, | ||
| ) | ||
| from app.core.errors.base import AppError | ||
| from app.db.repositories import create_job, get_job_by_uuid, get_template | ||
| from app.models import Job | ||
| from app.tasks.fill import fill_form_task | ||
|
|
||
| router = APIRouter(tags=["jobs"]) | ||
|
|
||
|
|
||
| @router.get("/jobs/{job_id}", response_model=JobResponse) | ||
| def get_job_status(job_id: str, db: Session = Depends(get_db)): | ||
| job = get_job_by_uuid(db, job_id) | ||
| if not job: | ||
| raise AppError("Job not found", status_code=404) | ||
| return JobResponse( | ||
| job_id=job.job_id, | ||
| job_type=job.job_type, | ||
| status=job.status, | ||
| progress_percent=job.progress_percent, | ||
| result_url=job.result_url, | ||
| error=job.error, | ||
| created_at=job.created_at.isoformat() if job.created_at else None, | ||
| updated_at=job.updated_at.isoformat() if job.updated_at else None, | ||
| ) | ||
|
|
||
|
|
||
| @router.post("/forms/jobs", response_model=AsyncFormFillResponse) | ||
| def submit_async_form_fill(form: AsyncFormFill, db: Session = Depends(get_db)): | ||
| for tid in form.template_ids: | ||
| if not get_template(db, tid): | ||
| raise AppError(f"Template {tid} not found", status_code=404) | ||
|
|
||
| jobs: list[AsyncJobSubmitResponse] = [] | ||
| for tid in form.template_ids: | ||
| result = fill_form_task.delay(tid, form.input_text, form.model) | ||
| job = Job( | ||
| celery_task_id=result.id, | ||
| job_type="form_generation", | ||
| template_id=tid, | ||
| input_text=form.input_text, | ||
| status="queued", | ||
| model=form.model, | ||
| ) | ||
| job = create_job(db, job) | ||
| jobs.append(AsyncJobSubmitResponse( | ||
| job_id=job.job_id, | ||
| status=job.status, | ||
| poll_url=f"/api/v1/jobs/{job.job_id}", | ||
| )) | ||
|
|
||
| return AsyncFormFillResponse(jobs=jobs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from celery import Celery | ||
|
|
||
| from app.core.config import CELERY_BROKER_URL, CELERY_RESULT_BACKEND | ||
|
|
||
| celery_app = Celery( | ||
| "fireform", | ||
| broker=CELERY_BROKER_URL, | ||
| backend=CELERY_RESULT_BACKEND, | ||
| ) | ||
|
|
||
| celery_app.conf.update( | ||
| task_serializer="json", | ||
| result_serializer="json", | ||
| accept_content=["json"], | ||
| task_track_started=True, | ||
| result_expires=86400, | ||
| ) | ||
|
|
||
| celery_app.conf.include = ["app.tasks.fill"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| """ORM models. Import from here: `from app.models import Template`.""" | ||
|
|
||
| from app.models.models import FormSubmission, Template | ||
| from app.models.models import FormSubmission, Job, Template | ||
|
|
||
| __all__ = ["Template", "FormSubmission"] | ||
| __all__ = ["Template", "FormSubmission", "Job"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import logging | ||
| from datetime import datetime, timezone | ||
|
|
||
| from app.core.celery import celery_app | ||
| from app.db.database import get_session | ||
| from app.db.repositories import get_job_by_celery_id, get_template, update_job, create_form | ||
| from app.models import FormSubmission | ||
| from app.services.controller import Controller | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @celery_app.task(bind=True, name="fill_form") | ||
| def fill_form_task(self, template_id: int, input_text: str, model: str | None = None): | ||
| session = next(get_session()) | ||
| try: | ||
| job = get_job_by_celery_id(session, self.request.id) | ||
| if not job: | ||
| raise RuntimeError(f"No job row for celery task {self.request.id}") | ||
|
|
||
| job.status = "processing" | ||
| job.progress_percent = 10 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This value hardcoded is to know that the task is already being processed so that you had to change the progress_percent to smthg bigger than 0? |
||
| job.updated_at = datetime.now(timezone.utc) | ||
| update_job(session, job) | ||
|
|
||
| template = get_template(session, template_id) | ||
| if not template: | ||
| raise ValueError(f"Template {template_id} not found") | ||
|
|
||
| controller = Controller() | ||
| path = controller.fill_form( | ||
| user_input=input_text, | ||
| fields=template.fields, | ||
| pdf_form_path=template.pdf_path, | ||
| model=model, | ||
| ) | ||
|
|
||
| submission = FormSubmission( | ||
| template_id=template_id, | ||
| input_text=input_text, | ||
| output_pdf_path=path, | ||
| ) | ||
| create_form(session, submission) | ||
|
|
||
| job.status = "completed" | ||
| job.progress_percent = 100 | ||
| job.result_url = f"/api/v1/forms/{submission.id}/download" | ||
| job.updated_at = datetime.now(timezone.utc) | ||
| update_job(session, job) | ||
|
|
||
| return {"job_id": job.job_id, "result_url": job.result_url} | ||
|
|
||
| except Exception as e: | ||
| logger.exception("fill_form_task failed") | ||
| job = get_job_by_celery_id(session, self.request.id) | ||
| if job: | ||
| job.status = "failed" | ||
| job.error = {"error_code": "TASK_FAILED", "message": str(e)} | ||
| job.updated_at = datetime.now(timezone.utc) | ||
| update_job(session, job) | ||
| raise | ||
| finally: | ||
| session.close() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be inside the v1_router? I.e. the endpoints shouldn't in be inside the v1 folder? Like Abhi did in his PR?