0

I'm using Django 3.0.6 as a web interface for a python pool controller.

The web server is Nginx 1.14.2 with Gunicorn.

The system manages various items including, opening and closing valves, monitoring of valve positions, controlling pump speeds, pool lights, chlorinator, scheduling etc.

The software and the hardware build is coming along nicely, although it has been a massive learning curve for me.

I need to find a way that just after Nginx/Gunicorn/Django boot (i.e. when the server is ready), a simple python function is executed only once to set the default 'pool program' and schedule and system status variables.

The solutions that I have seen read and tried have various outcomes - running the start-up function twice, or only when the index page is requested, or not at all.

Thank you in advance.

Radial
  • 342
  • 1
  • 4
  • 14
  • What *exactly* should that function do? – Klaus D. May 28 '20 at 04:33
  • Hi Klaus, sorry I didn’t make myself clear. I have already written the function, it’s just that I don’t know how to execute it at system start (server ready - Django/Nginx/Gunicorn). – Radial May 28 '20 at 04:58
  • What you are writing sounds like an [X Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). To give you good advise you should add details about the workload of the function. In general Django has some facilities to do "tasks". Running a function on startup might not be the best solution. – Klaus D. May 28 '20 at 05:06
  • Thanks Klaus, the function sets the days schedule, opens the correct valves, starts the pump and sets the system status variables and then ends. – Radial May 28 '20 at 05:07
  • It sounds like it should be a [management command](https://docs.djangoproject.com/en/3.0/howto/custom-management-commands/). – Klaus D. May 28 '20 at 18:35
  • Sorry Klaus, I missed your post. Can you suggest the code that I could use to execute an imported function (from my_app.somepy import *) in manage.py? At the moment I am stuck as using the AppConfig solution which executes the function using Django runserver, but does not execute at all when I restart the hardware or use systemctl restart apache2 (i've switched from Nginx to apache2). – Radial Jun 01 '20 at 08:08
  • Hi Klaus, You are absolutely correct in that a management command is what I was looking for. Thank you for your help. Please post your response as an answer and I will Mark it as answered. Thanks again. – Radial Jun 04 '20 at 10:29

1 Answers1

0

take a look at this: Execute code when Django starts ONCE only?

you can write the function in the urls.py or apps.py.

  • Hi Mahsa, I’ve tried this, but it runs the code twice when using the Django development server and with Nginx it only runs when a page is called. – Radial May 28 '20 at 06:23
  • have you tried calling the function in apps.py? I think it's called just once in initialization. – Sedigheh Monavari May 28 '20 at 07:50
  • Hi Mahsa, I’m going to try it in a basic version of Django project - I’ll get back to you. – Radial May 28 '20 at 08:54
  • The function executes when Gunicorn is restarted (sudo systemctl restart gunicorn). It does not work when the server (hardware) is rebooted. After server reboot the function only executes when the index page is requested from views. – Radial May 28 '20 at 16:35