Skip to content

[BUG] @repeat_every() does not run without @app.on_event('startup') decorator #256

@caseyjohnsonwv

Description

@caseyjohnsonwv

Describe the bug
The @repeat_every() decorator does not trigger the function it decorates unless the @app.on_event('startup') decorator is also present.

To Reproduce
I created this sample main.py to show the issue I've been seeing.

  1. Copy the below and run python3 main.py. The app will create a local file test.txt and write a Unix timestamp to it every second.
  2. Stop the app and comment out @app.on_event('startup')
  3. Run the app again. The file test.txt is no longer being updated. If you delete this file and restart the app, it will never be created.
import time
from fastapi import FastAPI
from fastapi_utils.tasks import repeat_every
import uvicorn

app = FastAPI()

@app.on_event('startup')
@repeat_every(seconds=1)
def do_something():
    with open('test.txt', 'w') as f:
        f.write(f"{time.time():.0f}")

if __name__ == '__main__':
    uvicorn.run('main:app')

Expected behavior
The function do_something() should be writing to the file every 1 second, even with @app.on_event('startup') commented out.

Environment:

  • OS: Windows 10
  • FastAPI Utils: 0.2.1
  • FastAPI: 0.75.0
  • Pydantic: 1.9.0
  • Python: 3.10.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions