From 782a06d13f474681a0f7cd9e912f22bb4fb909a7 Mon Sep 17 00:00:00 2001 From: Kai Hendry Date: Tue, 24 Jul 2012 17:23:31 +0200 Subject: [PATCH] Avoid httpd killing fetchtweets early by executing it off in the background TODO: some UI if lock is around, to tell folks a fetch is underway --- create.cgi | 40 +++++----------------------------------- expand-urls.sh | 2 +- fetch-tweets.sh | 11 ++++++++++- 3 files changed, 16 insertions(+), 37 deletions(-) diff --git a/create.cgi b/create.cgi index 59eb0b0..fc029cd 100755 --- a/create.cgi +++ b/create.cgi @@ -28,7 +28,7 @@ else exit fi -if test "${parm[2]}" == "o" +if test "${parm[2]}" == "o" # unused then test "${parm[3]}" && old=1 fi @@ -45,23 +45,17 @@ cat <
- -

Fetching upto 3200 tweets from $id

- -

Please be patient. Closing this page prematurely can limit the tweets fetch-tweets.sh gets and trigger a locking bug.

- -
 END
 
 if test -d u/$id
 then
-	echo Attempting an update
+	echo "

Attempting to update $id

" else if curl -sI http://api.twitter.com/1/users/lookup.xml?screen_name=${id} | grep -q "Status: 404 Not Found" then - echo "$id does not exist on twitter.com :(" + echo "

$id does not exist on twitter.com :(

" exit fi @@ -69,27 +63,11 @@ else fi - cd u/$id ln -sf ../../index.html || true ln -sf ../../grep.php || true -if ! test -f lock -then - touch lock - ../../fetch-tweets.sh $id - rm lock -else - echo Fetching already! Locks are cleared daily -fi - -echo "
" - -# Clean up in case it went wrong (e.g. trying to retrieve from an account with protected tweets) -if test -s "$oldpwd/u/$id/$id.txt" -then - if echo $id | grep -q -v '_' # Underscores in domain names is a no no then mkdir /srv/www/$id.greptweet.com 2> /dev/null || true @@ -107,14 +85,6 @@ ln -sf $id.txt tweets.txt test -h greptweet.appcache && rm -f greptweet.appcache sed -e "s,TIMESTAMP,$(date)," ../../greptweet.appcache > greptweet.appcache -else - rm -rf $oldpwd/u/$id - echo '

Sorry the Twitter API is failing. Try again later.

' -fi +echo "
"
 
-cat <
-
- - -END +../../fetch-tweets.sh $id & disown diff --git a/expand-urls.sh b/expand-urls.sh index 6526c1a..90ef972 100755 --- a/expand-urls.sh +++ b/expand-urls.sh @@ -5,7 +5,7 @@ test -s "$1" || exit test "${1##*.}" = 'txt' || exit temp=$(mktemp "$1.XXXX") -trap "rm -f $temp" INT +trap "rm -f $temp" EXIT IFS='|' while read -r id date text diff --git a/fetch-tweets.sh b/fetch-tweets.sh index 08f0ed3..b531ee6 100755 --- a/fetch-tweets.sh +++ b/fetch-tweets.sh @@ -1,6 +1,16 @@ #!/usr/bin/env bash # vim: set ts=4 sw=4 +if test -d lock +then + echo "Already running" + exit 1 # trap goes below in order not to remove the lock +else + mkdir lock +fi + +trap "rm -vrf $temp $temp2 lock; exit" EXIT + umask 002 api="http://api.twitter.com/1/statuses/user_timeline.xml?" @@ -42,7 +52,6 @@ echo Trying to get $(($twitter_total - $saved)) temp=$(mktemp "$1.XXXX") temp2=$(mktemp "$1.XXXX") -trap "rm -f $temp $temp2; exit" INT EXIT url="${api}screen_name=${1}&count=200&page=${page}${since}&include_rts=true&trim_user=1&include_entities=1"