s3rius / FastAPI-template

Feature rich robust FastAPI template.
MIT License
1.83k stars 161 forks source link

Failed to start project with docker #160

Closed tonquangkhai closed 1 year ago

tonquangkhai commented 1 year ago

Hi Pavel Kirilin, I recently generated a FastAPI project from your template and I really like it. However, when I tried to build the project on my local machine (Macbook with Intel chip) using Docker Compose, I encountered an error related to adding TaskIQ support. I tried to fix it on my own, but unfortunately, I was not successful. Could you please help me with this issue?

Screen Shot 2023-04-21 at 17 36 47 Screen Shot 2023-04-21 at 17 42 06

Traceback (most recent call last):

File "/usr/local/bin/taskiq", line 8, in

sys.exit(main())

File "/usr/local/lib/python3.9/site-packages/taskiq/main.py", line 72, in main

command.exec(sys.argv[1:])

File "/usr/local/lib/python3.9/site-packages/taskiq/cli/worker/cmd.py", line 26, in exec

run_worker(wargs)

File "/usr/local/lib/python3.9/site-packages/taskiq/cli/worker/run.py", line 144, in run_worker

raise ValueError("To use '--reload' flag, please install 'taskiq[reload]'.")

ValueError: To use '--reload' flag, please install 'taskiq[reload]'.```

Thank for reading this :)

s3rius commented 1 year ago

Hi and thanks raising this issue. This is because of the recent taskiq updates. To enable reload, please enable reload extra in pyproject.toml. Like this:

taskiq = {version = "^0", extras = ["reload"] }

Hope it helps.

I will leave this issue open untill we add this extra in dev group of pyproject.toml for new projects.

s3rius commented 1 year ago

Also, taskiq-kafka is a relatively new project, so we haven't added support for it in fastapi-template. To use it, please check the readme in this repo: https://github.com/taskiq-python/taskiq-aio-kafka

tonquangkhai commented 1 year ago

Hi, I'll try to fix in my local. Thank you so much for your reply.

linhdmn commented 1 year ago

Due to the Kafka container starting very slowly so the connection between the fast-API container and the taskiq-worker with Kafka can not be established. Here are my solutions and you can choose one of them:

  1. Increase timeout to 600s or more in docker-compose.yaml like this healthcheck: test: kafka-topics.sh --list --bootstrap-server localhost:9092 interval: 1s timeout: 1000s retries: 10

  2. Disable health check in both Fast-API and manually restart ONLY Fast-API when Kafka successfully started. Remove this line kafka: condition: service_healthy Here is my docker-compose example. docker-compose.md Good luck and cheers.

s3rius commented 1 year ago

Hi, @linhdmn. Thanks for your reply. But the problem was not in kafka or healthchecks. It was because of taskiq library was not installed properly. The template is fixed now in version https://github.com/s3rius/FastAPI-template/releases/tag/4.1.2.

s3rius commented 1 year ago

@tonquangkhai, can you try generating project with updated version and check if everything is working?

tonquangkhai commented 1 year ago

Currently everything worked fine in 4.1.2 but I am not adding kafka support option yet.

tonquangkhai commented 1 year ago

And also the command taskiq-worker in docker-compose.dev.yml is not fixed so it will cause small error when user try to build up in the dev enviroment. But overall everything is ok for now.

tonquangkhai commented 1 year ago

I will close this Issue since the project can be generated and worked well on new version 4.1.2. Just a small issue related to docker-compose.dev.yml if you guys need to using taskiq in local dev enviroment please reference to docker-compose.yml in the same folder, change <your_project_name>.taskiq:broker --> <your_project_name>.tkq:broker. Also if you choose to init kafka with the project you should increase timeout in healthcheck to 300s (more or less depend on your computer) so the project will start normally. Thank @linhdmn for pointing this.