# HG changeset patch # User HIROSE Yuuji # Date 1588500487 -32400 # Node ID a1bcb043589e3eb9da1c836753f098cad9d7d841 # Parent b7aeab02d2425014dc5a48b78ccf6a8348cbe469 Cache file creation cares concurrent creation by other processes diff -r b7aeab02d242 -r a1bcb043589e s4-blog.sh --- a/s4-blog.sh Sun May 03 19:06:59 2020 +0900 +++ b/s4-blog.sh Sun May 03 19:08:07 2020 +0900 @@ -370,6 +370,8 @@ : Nothing to do else { ######## New ROW creation begins here ######## >$cachefile + cachestamp=$tmpd/cache.$$.stamp + touch $cachestamp tdcls="__NEWCLS__repatt" if [ -s "$icon" ]; then icfn=`echo "$icon"|htmlescape` @@ -420,16 +422,21 @@ else query "SELECT hex(bin) FROM article_m WHERE rowid=$mrid;" \ > $mf2 # Stop query here 2020-04-23 - cat $mf2| unhexize \ + if cat $mf2 | unhexize \ | convert -define ${fmt}:size=100x100 -resize 100x100'>' \ - - ${fmt}:- \ - | tee "$outfile" \ + - ${fmt}:- > $outfile + then + cat "$outfile" \ | hexize \ | sed -e 's/\(..\)/%\1/g' \ -e "s|^|$fnb|" - unset stampfile # img data stream is not suitable to cache - echo $tm > $tsfile + unset stampfile # img data stream is not suitable to cache + echo $tm > $tsfile + else # Failed to convert + rm -f $outfile + echo "$fnb" + fi fi ;; *) @@ -438,7 +445,14 @@ esac done echo "" - } > "$cachefile" ######## New ROW Creation Ends here ######## + } > "$cachefile.$$" ######## New ROW Creation Ends here ######## + # Care about race condition + if [ -s $cachefile -a $cachefile -nt $cachestamp ]; then + # If other process have created cache, give up to serve our file + rm -f $cachefile.$$ + else + mv -f $cachefile.$$ $cachefile + fi test -n "$stampfile" && date "+%F %T" > $stampfile fi # Printing a cached row