Skip to content

Commit

Permalink
added celery redis
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Dec 15, 2022
1 parent 6c1929c commit b6e1643
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
18 changes: 18 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ This project was bootstrapped with [Geodjango Template](https://github.com/itsk

#### Default database settings:
Create .env in root backend project , and add the credentials as provided on .env_sample , Export your secret key and database url to your env

Export your database url

export DATABASE_URL=postgis://postgres:postgres@localhost:5432/ai
#### Now change your username , password and db name in settings.py accordingly to your database
python manage.py makemigrations
python manage.py migrate
Expand All @@ -42,3 +46,17 @@ fAIr uses oauth2.0 Authentication using [osm-login-python](https://github.com/ks
2. Check authentiation by getting back your data
Hit ```/api/v1/auth/me/```
- URL requires access-token as header and in return you will see your osm username , id and image url


## Start celery workers
1. Install redis server in your pc

```
sudo apt install redis
```

2. Start celery workers

```
celery -A aiproject worker --loglevel=info
```
5 changes: 5 additions & 0 deletions backend/aiproject/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ("celery_app",)
29 changes: 29 additions & 0 deletions backend/aiproject/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html
"""
import os

from celery import Celery
from django.conf import settings

# this code copied from manage.py
# set the default Django settings module for the 'celery' app.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aiproject.settings")

# app name
app = Celery("aiproject")

# read config from Django settings, the CELERY namespace would make celery
# config keys has `CELERY` prefix
app.config_from_object("django.conf:settings", namespace="CELERY")

# discover and load tasks.py from from all registered Django apps
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task
def train_model(x, y):
import time

time.sleep(5)
return x / y
8 changes: 7 additions & 1 deletion backend/aiproject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
default="https://galaxy-api.hotosm.org/v1/raw-data/current-snapshot/",
)

ALLOWED_HOSTS = ['localhost',HOSTNAME]
ALLOWED_HOSTS = ["localhost", HOSTNAME]

if env("GDAL_LIBRARY_PATH", default=False):
GDAL_LIBRARY_PATH = env("GDAL_LIBRARY_PATH")
Expand Down Expand Up @@ -164,3 +164,9 @@

if DEBUG:
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"


# celery configuration

CELERY_BROKER_URL = env("CELERY_BROKER_URL", default="redis://127.0.0.1:6379/0")
CELERY_RESULT_BACKEND = env("CELERY_RESULT_BACKEND", default="redis://127.0.0.1:6379/0")
4 changes: 3 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ Pillow
django-environ==0.9.0 # used for environment
django-filter==22.1
django-cors-headers==3.13.0 # used for enabling cors when frontend is hosted on different server / origin
osm-login-python==0.0.2
osm-login-python==0.0.2
celery==5.2.7
redis==4.4.0

0 comments on commit b6e1643

Please sign in to comment.