Forums

How to setup scheduled restart task?

I use an always-on task for my app.

Some days, I test my app and it takes a longer time that usual to process (no errors). I then restarted the web app and it worked instantly.

I would like to setup a nightly task at 12am EST to restart daily.

Is there an example I can follow?

Thanks!

You can set up a scheduled task which will use our API to reload the web app. Alternatively you can use our pa cli tool to make it easier.

This is what I use to automatically update my applications. I put it in a deploy directory (e.g., /home/deploy/appname.sh) and made it an executable (chmod +x appname.sh). Then you can set up a scheduled task to run the following command:

~/deploy/myapp.sh --skip-color

It's set up for a django app that uses poetry, but you can adapt it as you need. I hope it helps.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/sh

set -e # exit when anycommand fails

BLUE='\033[1;34m'             # Blue
GREEN='\033[1;32m'            # Green
ENDCOLOR='\033[0m'            # No Color

for arg in "$@"; do
  shift
  case "$arg" in
    '--skip-color')   set -- "$@" '-s'   ;;
    *)                set -- "$@" "$arg" ;;
  esac
done

dry_run=false
branch=<master>                                                          # < --------- Update this 
OPTIND=1
while getopts 'b:ds' opt; do
    case "$opt" in
        d) dry_run=true ;;
        b) branch=${OPTARG};;
        s) BLUE='' GREEN='' ENDCOLOR='' ;;
        *) echo 'error in command line parsing' >&2
           exit 1
    esac
done
shift $(expr $OPTIND - 1) # remove options from positional parameters


cd </home/username/path/to/src>                                          # < --------- Update this

echo "Deploying ${BLUE}<username>.pythonanywhere.com${ENDCOLOR}..."      # < --------- Update this
echo " - Path:      ${PWD}"
echo " - Installed: $(poetry version)"
echo " - Branch:    $(git rev-parse --abbrev-ref HEAD)"
echo " - Commit:    $(git show -s --format='%h %s')"

echo "\n${BLUE}1. Checking out $branch:${ENDCOLOR}"
git checkout $branch

echo "\n${BLUE}2. Pulling from origin:${ENDCOLOR}"
git pull origin $branch

echo "\n${BLUE}3. Installing project:${ENDCOLOR}"
poetry install --only main --sync --extras databases

echo "\n${BLUE}4. Checking for missing migrations:${ENDCOLOR}"
poetry run python manage.py makemigrations --dry-run --check

echo "\n${BLUE}5. Checking deployment:${ENDCOLOR}"
poetry run python manage.py check --deploy --database default

if "$dry_run"; then
    echo "\n${BLUE}6. Checking for unapplied migrations:${ENDCOLOR}"
    poetry run python manage.py migrate --plan --check
else
    echo "\n${BLUE}6. Applying DB migrations:${ENDCOLOR}"
    poetry run python manage.py migrate
fi

echo -n "\n${BLUE}Reloading Application...${ENDCOLOR}"
touch /var/www/<username_pythonanywhere_com_wsgi>.py                    # < --------- Update this 
echo "${GREEN}Done!${ENDCOLOR}"

exit 0

Thanks

Glad it helped