view s4-world.sh @ 811:52c249ab4ca6 feature-world

[TEST]Set timeout of account sync to 1000ms
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 15 Jun 2020 14:31:57 +0900
parents ba11fd987baf
children d2c4c473e08e
line wrap: on
line source

#!/bin/sh

# Setup variables for running time
if $isCGI; then
  case "$S4WORLDLIST" in
    *:*:*)
      worldlistfile=cache/worldlist
      worldgrpfile=cache/worldgrps
      worldoptionfile=cache/worldoption
      worldnamefile=cache/worldname
      if [ ! -e $worldlistfile -o $worldlistfile -ot s4-config.sh \
	     -o ! -e $worldoptionfile -o $worldoptionfile -ot s4-config.sh \
	     -o s4-world.sh -nt $worldoptionfile ]
      then
	echo S4MASTERURL=$S4MASTERURL >> tmp/debug.out
	cat <<-EOF > $worldlistfile
	<div><table>
	 <tr><th>World List</th></tr>
	 <tr><td title="Base World$nl拠点となるWorldです">&rArr;
	  <a href="${S4MASTERURL:-$URL}">Base</a></td></tr>
	EOF
	true > $worldoptionfile
	for i in $S4WORLDLIST; do
	  echo $i | {
	    IFS=: read name short d
	    cgi="s4-world-$short$cgiext"
	    conf="s4-config-$short.sh"
	    cat<<-EOF >>$worldlistfile
	    <tr><td title="$d">&rArr; <a href="$cgi">$name</a></td></tr>
		EOF
	    cat<<-EOF >>$worldoptionfile
	    <option title="$d" value="$conf">$name</option>
		EOF
	    echo "$name" > $worldnamefile.$short
	  }
	done
	if [ -s "$worldoptionfile" ]; then
	  echo "<option value=\"s4-config.sh\">Base</option>" >> $worldoptionfile
	  echo "</table>$nl</div>" >> $worldlistfile
	  sed 's/href="\([^>]*\)"/href="\1?grps"/' $worldlistfile \
	      > $worldgrpfile
	else
	  true > $worldoptionfile; true > $worldlistfile	# Remove contents
	  rm -f ${worldnamefile}.*
	fi
      fi
      if [ -s "$worldlistfile" ]; then
	S4WORLDS="▼spaste(\`$worldlistfile')"
	S4WORLDNAME=${S4WORLD:+`cat $worldnamefile.$S4WORLD`}
	S4WORLDGRPS="▼spaste(\`$worldgrpfile')"
      fi
      ;;
  esac
fi

err "db=$db mas=$S4MASTERDB sessdb=$sessdb"
# If in parent world, no need to do rest of jobs
if [ -z "$S4MASTERDB" -o ! -s "$S4MASTERDB" ]; then
  return
fi
# Confim child
if [ "$db" -ef "$S4MASTERDB" ]; then
  return		# Points to the same file
fi

# Now Another world is ACTIVE
# sessdb=`dirname $S4MASTERDB`/sess.sq3
## skey="skey-`basename $mydir`"
syncflag=${db%.*}.synctime
runflag=${db%.*}.run
userupdateflag=`dirname $S4MASTERDB`/`basename $userupdateflag`
test ! -e "$userupdateflag"		&& return
test "$syncflag" -nt "$userupdateflag"	&& return
if [ -s "$runflag" ]; then
  limit=`cat $runflag|tr -c -d 0-9`
  if [ -n "$limit" -a "$limit" -gt `date +%s` ]; then
    err "World $S4WORLD account sync withholded by process $$"
    return		# Running sync by other process not leaching timeout
  fi
fi
echo $((`date +%s` + 10)) > $runflag	# Setting running flag by timeout 10s

#  for sub.sq3
# 
#  main: user: 'taro', 'hanako', 'shige'
#  sub:  user: 'taro', 'hanako', 'shige'
#  sub2: user_s: ('taro', 't'), ('hanako', 'h'), ('shige', 's')
#  then update
# 

## sqlite3 -cmd '.timer 1' -cmd '.echo 1' $db <<EOF
err "`gdate +%S.%3N` Starting account synchronization[$$]"
## cat > tmp/sql <<EOF
num=$(sqlite3 -bail -cmd 'PRAGMA FOREIGN_KEYS=on' $db <<EOF
.timeout 1000
ATTACH DATABASE "$S4MASTERDB" AS m;
CREATE TABLE IF NOT EXISTS user(name, primary key(name));
BEGIN;
DElETE FROM main.user WHERE rowid NOT IN (SELECT rowid FROM m.user);
INSERT INTO main.user(rowid, name)
       SELECT rowid, name FROM m.user
       WHERE m.user.rowid NOT IN (SELECT rowid FROM user);
UPDATE user SET name = (SELECT name FROM m.user WHERE main.user.rowid=m.user.rowid);
DELETE FROM main.user_s WHERE rowid NOT IN (SELECT rowid FROM m.user_s);
REPLACE INTO main.user_s(rowid, name, key, type, val, bin)
	SELECT rowid,* FROM m.user_s;
DELETE FROM main.user_m WHERE rowid NOT IN (SELECT rowid FROM m.user_m);
REPLACE INTO main.user_m(rowid, name, key, type, val, bin)
	SELECT rowid,* FROM m.user_m
	WHERE key NOT LIKE '%cache%';
END;

/* Compare user tables */
WITH master AS (
  SELECT p.rowid,* FROM m.user p
  	 NATURAL LEFT JOIN m.user_s
	 NATURAL LEFT JOIN m.user_m
), thisworld AS (
  SELECT p.rowid,* FROM user p
  	 NATURAL LEFT JOIN user_s
 	 NATURAL LEFT JOIN user_m
), m_a AS (
  SELECT * FROM master EXCEPT SELECT * FROM thisworld
), a_m AS (
  SELECT * FROM thisworld EXCEPT SELECT * FROM master
) SELECT (SELECT count(*) FROM m_a) + (SELECT count(*) FROM a_m);
DETACH DATABASE m;
EOF
)
### num=$(sqlite3 -bail -cmd 'PRAGMA FOREIGN_KEYS=on' $db < tmp/sql )
if [ -n "$num" -a $num -eq 0 ]; then
  err "`gdate +%S.%3N` Account synchronization[$$] done in difference $num"
  echo "`date '+%F %T'`: Sync done by process $$" >> $syncflag
else
  err "Account synch[$$] failed or bailed with num=[$num]"
fi
test -e "$runflag" && rm -f "$runflag"
return $num

yatex.org