Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve dev experience #30

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,22 @@ Start redis instance in docker container (if not started yet) and start worker p

```
poetry run invoke start-broker
poetry run invoke worker --beat
poetry run invoke worker --beat --watch
```

### Debugging the worker with PyCharm

1. Create a new "Run/Debug Configuration" of the type "Python Debug Server"
2. Follow the instructions inside this config e.g.:
1. `pip install pydevd-pycharm~=241.18034.82`
2. add the following code somewhere before the lines you want to debug:
```
import pydevd_pycharm
pydevd_pycharm.settrace('localhost', port=1234, stdoutToServer=True, stderrToServer=True)
```
3. Start the "Python Debug Server" config
4. Start the worker

## Environment variables

- the database can be configured with `SQLALCHEMY_DATABASE_URI`
Expand Down
46 changes: 45 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pydeps = "^1.10.24"
hypothesis = "^6.68.2"
# setuptools is required by sphinxcontrib-redoc
setuptools = "^70.0.0"
watchdog = "^4.0.1"

[tool.poetry.extras]
psycopg = ["psycopg"]
Expand Down
20 changes: 18 additions & 2 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def start_broker(c, port=None):


@task
def worker(c, pool="solo", concurrency=1, dev=False, log_level="INFO", beat=False):
def worker(
c, pool="solo", concurrency=1, dev=False, log_level="INFO", beat=False, watch=False
):
"""Run the celery worker, optionally starting the redis broker.

Args:
Expand All @@ -146,7 +148,8 @@ def worker(c, pool="solo", concurrency=1, dev=False, log_level="INFO", beat=Fals
concurrency (int, optional): the number of concurrent workers (defaults to 1 for development)
dev (bool, optional): If true the redis docker container will be started before the worker and stopped after the workers finished. Defaults to False.
log_level (str, optional): The log level of the celery logger in the worker (DEBUG|INFO|WARNING|ERROR|CRITICAL|FATAL). Defaults to "INFO".
beat (bool, optional): If true a celery beat scheduler will be started alongside the worker. This is needed for periodic tasks. Should only be set to True for one worker otherwise the periodic tasks get executed too often (see readme file).
beat (bool, optional): If True, a celery beat scheduler will be started alongside the worker. This is needed for periodic tasks. Should only be set to True for one worker otherwise the periodic tasks get executed too often (see readme file).
watch (bool, optional): If True, watch for file changes and restart workers automatically. Defaults to False.
"""
if dev:
start_broker(c)
Expand All @@ -164,6 +167,19 @@ def worker(c, pool="solo", concurrency=1, dev=False, log_level="INFO", beat=Fals
"-E",
]

if watch:
cmd = [
"watchmedo",
"auto-restart",
f"--directory=./{MODULE_NAME}",
"--pattern=*.py",
"--recursive",
"--debounce-interval=3",
"--kill-after=10",
"--no-restart-on-command-exit",
"--",
] + cmd

if beat:
cmd += ["-B"]

Expand Down
Loading