[Python] Run commands after deploying Django web service app

Patricio 6 Reputation points
2020-09-08T21:21:19.613+00:00

Hello, I hope you can help me.

I was deploying a Django on web service following the tutorial, connecting to a PostgreSQL and everything worked fine. I even was able to connect to Github to deploy on every new push to a specific branch.

However, I need to run two commands after every deployment. The first one is python manage.py migrate and the second one is python manage.py process_tasks &, note that the last one must run in background because it has to keep running.

I tried a lot of things to do this and nothing worked. I created a .sh file, a .txt file, with the first line being #!/usr/bin/env and without. I also tried adding gunicorn --bind=0.0.0.0 --timeout 600 api.wsgi as the last line but there was always an error.

Right now, the current setup is with a file called start.sh, the file "start.sh" in the Startup command of the General Settings screen. But it is not working. The github action deploy does not fail, but this is the log:

2020-09-08T21:04:58.654649998Z
2020-09-08T21:04:58.654676999Z   _____
2020-09-08T21:04:58.654682099Z   /  _  \ __________ _________   ____
2020-09-08T21:04:58.654685699Z  /  /_\  \___   /  |  \_  __ \_/ __ \
2020-09-08T21:04:58.654689599Z /    |    \/    /|  |  /|  | \/\  ___/
2020-09-08T21:04:58.654693199Z \____|__  /_____ \____/ |__|    \___  >
2020-09-08T21:04:58.654696999Z         \/      \/                  \/
2020-09-08T21:04:58.654700399Z
2020-09-08T21:04:58.654703599Z A P P   S E R V I C E   O N   L I N U X
2020-09-08T21:04:58.654706999Z
2020-09-08T21:04:58.654710199Z Documentation: http://aka.ms/webapp-linux
2020-09-08T21:04:58.654713499Z Python 3.7.7
2020-09-08T21:04:58.654716799Z Note: Any data outside '/home' is not persisted
2020-09-08T21:04:59.522999223Z Starting OpenBSD Secure Shell server: sshd.
2020-09-08T21:04:59.604943246Z Site's appCommandLine: start.sh
2020-09-08T21:04:59.604988047Z Checking of start.sh is a file
2020-09-08T21:04:59.605077849Z App command line is a file on disk
2020-09-08T21:04:59.656190187Z App command line is a shell script, will execute this script as startup script
2020-09-08T21:04:59.656238688Z Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite -bindPort 8000 -userStartupCommand start.sh
2020-09-08T21:05:00.105511786Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2020-09-08T21:05:00.114065477Z Build Operation ID: |UBO5z1J0tJU=.412804f7_
2020-09-08T21:05:00.115193102Z Oryx Version: 0.2.20200522.2, Commit: bf724c0e83f3da74487f55cd304c63331dab7067, ReleaseTagName: 20200522.2
2020-09-08T21:05:01.025369158Z Writing output script to '/opt/startup/startup.sh'
2020-09-08T21:05:01.677692775Z Found virtual environment .tar.gz archive.
2020-09-08T21:05:01.678318089Z Removing existing virtual environment directory /antenv...
2020-09-08T21:05:01.691245476Z Extracting to directory /antenv...
2020-09-08T21:05:47.345938665Z Using packages from virtual environment antenv located at /antenv.
2020-09-08T21:05:47.437320099Z Updated PYTHONPATH to ':/antenv/lib/python3.7/site-packages'


2020-09-08T21:08:48.929Z ERROR - Container q7-api-dev_0_1777f900 for site q7-api-dev did not start within expected time limit. Elapsed time = 230.4407077 sec
2020-09-08T21:08:48.959Z ERROR - Container q7-api-dev_0_1777f900 didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
2020-09-08T21:08:49.699Z INFO  - Stopping site q7-api-dev because it failed during startup.

I would love to know the proper way to do this. The documentation isn't clear, and I am spending days to configure a deploy that should be easy. Thank you very much.

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
{count} vote

1 answer

Sort by: Most helpful
  1. SnehaAgrawal-MSFT 22,706 Reputation points Moderator
    2020-09-09T11:14:00.41+00:00

    Apologize for the inconvenience and frustration with this issue. If I have understood right you are following below article.

    Tutorial: Deploy a Django web app with PostgreSQL in Azure App Service If so, Thanks for sharing feedback with us. I will internally connect to our content team for more clear documentation on this.

    As you mentioned “I need to run two commands after every deployment. The first one is python manage.py migrate and the second one is python manage.py process_tasks &, note that the last one must run in background because it has to keep running.”

    For Django-background-tasks, you will need to run python manage.py process_tasks and for Linux App Services here are some documents you want to investigate:

    Looking to the log:

    2020-09-08T21:08:48.929Z ERROR - Container q7-api-dev_0_1777f900 for site q7-api-dev did not start within expected time limit. Elapsed time = 230.4407077 sec
    2020-09-08T21:08:48.959Z ERROR - Container q7-api-dev_0_1777f900 didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
    2020-09-08T21:08:49.699Z INFO - Stopping site q7-api-dev because it failed during startup.

    To elaborate on this when we start your container, we'll wait a while for it to start and initialize. We consider the startup to be successful once the container is running and once we get a response to a ping so that we know it's ready to respond to HTTP traffic. We'll wait 230 seconds for that to happen. If we don't have a successful start within 230 seconds, we'll assume there's a problem and we'll stop the container.

    You can add an app setting called WEBSITES_CONTAINER_START_TIME_LIMIT and set it to the number of seconds you would like for us to wait for your container to start (up to a maximum of 1800).

    Also, check container is listening to your port, we will wait a certain amount of time for your container to start before we consider it be a failed start. In order for us to consider a container to be successfully started, the container must start and it must respond to an HTTP ping. If the container starts but does not respond to a ping, we will eventually log an event in the Docker log saying that it didn't start.

    Could you please confirm that port 8000 is exposed, so that when trying to attempt to ping, the container is listening.

    You may use the WEBSITES_PORT app setting with a value of "8000" to expose that port.

    For details refer to Your container must respond to an HTTP ping
    If your container takes a long time to start, increase the start time limit

    For more details refer to Things You Should Know: Web Apps and Linux

    Let us know if issue persists.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.