summaryrefslogtreecommitdiff
path: root/portal/tools/site_cron.sh
blob: a06d9461651367fbd3cb27bd35075f413b911856 (plain)
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
#!/usr/local/bin/bash

# $Id$

# This script is intended to be run from cron say every 5 minutes.
# It will poll the database for requests to rebuild the whole or
# parts of the site, and will call the actual buildsite script as
# necessary to perform these updates.


PSQL="/usr/local/bin/psql -h pgsql74.hub.org 186_www -U 186_pgsql"
BS="/usr/local/www/wwwmaster.postgresql.org/www/tools/buildsite /usr/local/www/wwwmaster.postgresql.org/www"
RSYNC="/usr/local/bin/rsync"
WWWROOT="/usr/local/www/wwwmaster.postgresql.org/static/"

# Interlocking goes here
if [ ! -e /tmp/.website_update.lck ]; then
    trap "rm -f /tmp/.website_update.lck" INT TERM EXIT
    touch /tmp/.website_update.lck

    # Check if there are updates to be run
    R=$(${PSQL} -At -F_ -c "SELECT max(t),max(docs),max(ftp) FROM sync_request \
                            WHERE completed IS NULL AND t < CURRENT_TIMESTAMP")
    if [ "$R" ]; then
        TIME=$(echo $R | cut -f1 -d_)
        if [ "$TIME" != "" ]; then
            # Figure out if we can skip docs and/or ftp
            DOCS=$(echo $R | cut -f2 -d_)
            FTP=$(echo $R | cut -f3 -d_)

            PRM=""
            [ "$DOCS" == "0" ] && PRM="$PRM -nodocs"
            [ "$FTP" == "0" ] && PRM="$PRM -noftp"

            ${BS} $PRM

            # Ok, site is built. Log this fact, and remove the queued
            # jobs for it.
            ${PSQL} -q -c "INSERT INTO sync_log VALUES \
                             (CURRENT_TIMESTAMP, 'sync', 'wwwmaster', '$TIME'); \
                           UPDATE sync_request SET completed=CURRENT_TIMESTAMP \
                             WHERE completed IS NULL AND t <= '$TIME';"


            T=$(date "+%Y-%m-%d %H:%M:%S")
            # Now fire off mirroring tasks as subcommands
            for IP in $(${PSQL} -At -c "SELECT ip FROM frontends") ; do
                (
                    # Push the sync through
                    ${RSYNC} -azH --delete --exclude=.svn ${WWWROOT} ${IP}::pgsql-www
                    ${PSQL} -q -c "INSERT INTO sync_log VALUES \
                                   (CURRENT_TIMESTAMP, 'push', '${IP}', '$TIME')"
                ) &
            done
            wait
        fi
    fi
#else
    # Script is already running. Just don't do anything,
    # if we log something here it's going to end up spamming
    # the list...
fi