Skip to content
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

[BUG] Cannot use get_db as a dependency with repeated task #253

Open
rafaluk opened this issue Aug 5, 2021 · 4 comments
Open

[BUG] Cannot use get_db as a dependency with repeated task #253

rafaluk opened this issue Aug 5, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@rafaluk
Copy link

rafaluk commented Aug 5, 2021

While using fastapi-utils scheduler, passing Depends(get_db) as a parameter to the function doesn't work.

This is how it works within FastAPI endpoint:

@app.post("/sample_test")
async def sample_test(db: Session = Depends(get_db)):
    return db.query(models.User.height).all()

This is how it doesn't work with FastAPI utils:

@app.on_event("startup")
@repeat_every(seconds=10)
async def sample_test(db: Session = Depends(get_db)):
    return db.query(models.User.height).all()

(note that the functions are the same, only decorators have changed)

There is no error, but the query is not being called (I think the session is not created?). It just gets stuck.

The get_db() function differs a little bit between those two, but under the hood it does the same for both.

Environment:

  • OS: Windows 10
  • Python 3.9.5 with:
    • FastAPI Utils 0.2.1
    • FastAPI 0.63.0
    • SQLAlchemy 1.4.22
  • SQL Server v.18.8
@rafaluk rafaluk added the bug Something isn't working label Aug 5, 2021
@tintinthong
Copy link

Yup. I face the same issue here

@shakthifuture
Copy link

I am also facing the same issue, any solution?

@hussainsajib
Copy link

The reason it doesn't work in repeat_task is because Depends can't be used in our own functions. It has to be in FastAPI function. Refer to this: fastapi/fastapi#1693 (comment)

However, I also trying to access Session from a repeated_task function and is unable. Did anyone found any workaround?

@julienV
Copy link

julienV commented Aug 13, 2022

well, you can always get your objects without the dependency injection mechanism... Not great for testing though

from app.db import SessionLocal
....
@app.on_event("startup")
@repeat_every(seconds=60)
async def do_something() -> None:
    db = SessionLocal()
   ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants