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 wrap: on
line diff
--- 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|^|<a href=\"$catlink+$mrid\"><img src=\"data:image/$fmt,|" \
 			  -e "s|\$|\">$fnb</a>|"
-		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 "<a href=\"$catlink+$mrid\">$fnb</a>"
+		fi
 	      fi
 	      ;;
 	    *)
@@ -438,7 +445,14 @@
 	  esac
 	done
 	echo "</td></tr>"
-      } > "$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

yatex.org