s4

changeset 641:a1bcb043589e

Cache file creation cares concurrent creation by other processes
author HIROSE Yuuji <yuuji@gentei.org>
date Sun, 03 May 2020 19:08:07 +0900
parents b7aeab02d242
children 209d74364eca
files s4-blog.sh
diffstat 1 files changed, 20 insertions(+), 6 deletions(-) [+]
line diff
     1.1 --- a/s4-blog.sh	Sun May 03 19:06:59 2020 +0900
     1.2 +++ b/s4-blog.sh	Sun May 03 19:08:07 2020 +0900
     1.3 @@ -370,6 +370,8 @@
     1.4         : Nothing to do
     1.5      else
     1.6        {		######## New ROW creation begins here ######## >$cachefile
     1.7 +	cachestamp=$tmpd/cache.$$.stamp
     1.8 +	touch $cachestamp
     1.9  	tdcls="__NEWCLS__repatt"
    1.10  	if [ -s "$icon" ]; then
    1.11  	  icfn=`echo "$icon"|htmlescape`
    1.12 @@ -420,16 +422,21 @@
    1.13  	      else
    1.14  		query "SELECT hex(bin) FROM article_m WHERE rowid=$mrid;" \
    1.15  		      > $mf2  # Stop query here 2020-04-23
    1.16 -		cat $mf2| unhexize \
    1.17 +		if cat $mf2 | unhexize \
    1.18  		    | convert -define ${fmt}:size=100x100 -resize 100x100'>' \
    1.19 -			      - ${fmt}:- \
    1.20 -		    | tee "$outfile" \
    1.21 +			      - ${fmt}:- > $outfile
    1.22 +		then
    1.23 +		  cat "$outfile" \
    1.24  		    | hexize \
    1.25  		    | sed -e 's/\(..\)/%\1/g' \
    1.26  	    		  -e "s|^|<a href=\"$catlink+$mrid\"><img src=\"data:image/$fmt,|" \
    1.27  			  -e "s|\$|\">$fnb</a>|"
    1.28 -		unset stampfile # img data stream is not suitable to cache
    1.29 -		echo $tm > $tsfile
    1.30 +		  unset stampfile # img data stream is not suitable to cache
    1.31 +		  echo $tm > $tsfile
    1.32 +		else	# Failed to convert
    1.33 +		  rm -f $outfile
    1.34 +		  echo "<a href=\"$catlink+$mrid\">$fnb</a>"
    1.35 +		fi
    1.36  	      fi
    1.37  	      ;;
    1.38  	    *)
    1.39 @@ -438,7 +445,14 @@
    1.40  	  esac
    1.41  	done
    1.42  	echo "</td></tr>"
    1.43 -      } > "$cachefile"	######## New ROW Creation Ends here ########
    1.44 +      } > "$cachefile.$$"	######## New ROW Creation Ends here ########
    1.45 +      # Care about race condition
    1.46 +      if [ -s $cachefile -a $cachefile -nt $cachestamp ]; then
    1.47 +	# If other process have created cache, give up to serve our file
    1.48 +	rm -f $cachefile.$$
    1.49 +      else
    1.50 +	mv -f $cachefile.$$ $cachefile
    1.51 +      fi
    1.52        test -n "$stampfile" && date "+%F %T" > $stampfile
    1.53      fi
    1.54      # Printing a cached row