Poll Django DB regularly from within app - permissions error

29 Views Asked by At

I would like to keep record of some "check-in" events in my Django app database. If a client/user hasn't posted a check-in (done via HTTP request to my app) for over X minutes, then I would like to emit a socketIO message to the client. The client can post a check-in as frequently as they like. I am running this app from a Kubernetes pod, with database username and password being exported as environment variables on pod deployment.

My solution is to kick off a thread at app startup, and have this thread poll the Django database every 10 minutes. If a check-in record is not found in the database in the last 10 minutes, then call my socket emission function. Otherwise, go back to sleep for 10 minutes. Unfortunately, when I try to poll the database from within my thread, I'm met with:

django.db.utils.ProgrammingError: permission denied for table check_in_table

Some simplified code snippets:

# WatchCheckIn().start() is called from my apps.py file within my `AppConfig`'s `ready()`
class WatchCheckIn(threading.Thread):
    def run(self) -> None:
        logging.info('Querying database for check-ins.')
        try:
            latest_record = models.CheckIn.objects.last()
            check_in_period = datetime.datetime.now(
                tz=datetime.timezone.utc) - datetime.timedelta(minutes=10)
            if latest_record.check_in_time < check_in_period:
                socket_views.notify_need_check_in()
        except IndexError:
            logging.warning('No previous check-ins found.')
        finally:
            time.sleep(600)

In my settings.py for my app, I grab the username and password from what's been exported as part of my K8s deployment, as I mentioned above:

DATABASES = {
    'default': {
        # <other items>
        'USER': os.environ.get('DB_USER'),
        'PASSWORD': os.environ.get('DB_PASSWORD'),
    }
}

I do have corresponding models.py, views.py, and serializers.py files for my app which I use work just fine when clients make requests, but I'm not sure if/how I should be using those from within my own app. I'm generally just confused on how to give my app permission to query its own database at a regular cadence.

I know that there are other solutions besides this, like Celery, but I would really like to see if this approach could work if possible. Thanks in advance for any help!

1

There are 1 best solutions below

0
Ojas Kale On

Looks like this user does not have permission to access to the DB where this check_in_table is.

Can you do this? Exec into the POD and try to run the same SQL command from Django shell. It won't work.

then use psql command so that any user can access the DB. you can always fine grain the permission. but this looks to be root cause.

sudo -u postgres psql <database_name>
GRANT CREATE ON SCHEMA public TO <your_dbuser>;

you can do something like this to grant the permission.

BTW, this has nothing to do with kubernetes.