s4
changeset 817:d2c4c473e08e feature-world
Account synchronization more prudent
author | HIROSE Yuuji <yuuji@gentei.org> |
---|---|
date | Wed, 17 Jun 2020 13:54:01 +0900 |
parents | ed2c8aab742d |
children | de988b0a7afa |
files | s4-funcs.sh s4-newworld.sh s4-world.sh |
diffstat | 3 files changed, 93 insertions(+), 65 deletions(-) [+] |
line diff
1.1 --- a/s4-funcs.sh Wed Jun 17 08:16:59 2020 +0900 1.2 +++ b/s4-funcs.sh Wed Jun 17 13:54:01 2020 +0900 1.3 @@ -1448,6 +1448,7 @@ 1.4 exit 0 1.5 } 1.6 dologin() { 1.7 + test -n "$S4WORLD" && syncaccount 1.8 checkauth 1.9 st=$? 1.10 if [ $st != 0 ]; then
2.1 --- a/s4-newworld.sh Wed Jun 17 08:16:59 2020 +0900 2.2 +++ b/s4-newworld.sh Wed Jun 17 13:54:01 2020 +0900 2.3 @@ -5,11 +5,6 @@ 2.4 # $1 = World Display Name in UTF-8 2.5 # $2 = World ShortName in alpha-numeric 2.6 # $3 = World Description in UTF-8 2.7 -. `dirname $0`/s4-config.sh 2.8 -if ! type htmlescape >/dev/null 2>&1; then 2.9 - . `dirname $0`/s4-funcs.sh ### > /dev/null 2>&1 2.10 - trap 'exit 1' INT QUIT 2.11 -fi 2.12 dispname=$1 2.13 shortname=$2 2.14 desc=$3 2.15 @@ -19,8 +14,7 @@ 2.16 read $1 2.17 } 2.18 2.19 -echo db=$db URL=$URL isCGI=$isCGI 2.20 -if ! $isCGI; then 2.21 +if [ -z "$HTTP_HOST" ]; then 2.22 while true; do 2.23 dispname=`echo $dispname | tr -d ': \t\n"' | fold -w 28 | head -1` 2.24 test -n "$dispname" && break 2.25 @@ -39,17 +33,30 @@ 2.26 fi 2.27 echo "wl=$S4WORLDLIST" 2.28 echo "$dispname:$shortname:$desc" 2.29 +# Create config 2.30 +DB=db/$shortname.sq3; export DB 2.31 +`dirname $0`/s4-init.sh 2.32 +. `dirname $0`/s4-config.sh 2.33 +if ! type htmlescape >/dev/null 2>&1; then 2.34 + . `dirname $0`/s4-funcs.sh ### > /dev/null 2>&1 2.35 + trap 'exit 1' INT QUIT 2.36 +fi 2.37 + 2.38 newworld=$( 2.39 - { echo "$S4WORLDLIST" | tr ' ' '\n' \ 2.40 + { echo $S4WORLDLIST | tr ' ' '\n' \ 2.41 | awk -F: "\$2 != \"$shortname\"{print}" 2.42 echo "$dispname:$shortname:$desc" 2.43 - } | tr '\n' ' ' | tr -d '"' 2.44 + } | tr '\n' ' ' | sed 's/ $//' | tr -d '"' 2.45 ) 2.46 if [ -z "$newworld" ]; then 2.47 exit 2.48 fi 2.49 2.50 -# Create config 2.51 +masterdb=`(unset DB; . ./s4-config.sh; echo $DB)` 2.52 +S4MASTERDB=${masterdb:-db/cgi.sq3} 2.53 +echo Synching account: db=$db URL=$URL isCGI=$isCGI mas=$S4MASTERDB 2.54 +. ./s4-world.sh && syncaccount 'force' 2.55 +echo Synching done 2.56 bgcolor=$( 2.57 od -An -tu1 -N3 < /dev/urandom \ 2.58 | { read r g b 2.59 @@ -75,12 +82,9 @@ 2.60 # Update s4-config.sh 2.61 cat<<-EOF | ed s4-config.sh 2.62 g/^S4WORLDLIST=/d 2.63 - \$a 2.64 - S4WORLDLIST="`echo $newworld`" 2.65 - . 2.66 wq 2.67 EOF 2.68 -DB=db/$shortname.sq3 `dirname $0`/s4-init.sh 2.69 -(S4MASTERDB=$db; db=db/$shortname.sq3; . ./s4-world.sh) 2.70 +echo "S4WORLDLIST=\"$newworld\"" >> s4-config.sh 2.71 + 2.72 (cd `dirname $0`; ln -s s4$cgiext s4-world-$shortname$cgiext) 2.73 echo $newworld added
3.1 --- a/s4-world.sh Wed Jun 17 08:16:59 2020 +0900 3.2 +++ b/s4-world.sh Wed Jun 17 13:54:01 2020 +0900 3.3 @@ -1,5 +1,7 @@ 3.4 #!/bin/sh 3.5 3.6 +autoremovestop=2 3.7 + 3.8 # Setup variables for running time 3.9 if $isCGI; then 3.10 case "$S4WORLDLIST" in 3.11 @@ -53,48 +55,32 @@ 3.12 esac 3.13 fi 3.14 3.15 -err "db=$db mas=$S4MASTERDB sessdb=$sessdb" 3.16 -# If in parent world, no need to do rest of jobs 3.17 -if [ -z "$S4MASTERDB" -o ! -s "$S4MASTERDB" ]; then 3.18 - return 3.19 -fi 3.20 -# Confim child 3.21 -if [ "$db" -ef "$S4MASTERDB" ]; then 3.22 - return # Points to the same file 3.23 -fi 3.24 3.25 -# Now Another world is ACTIVE 3.26 -# sessdb=`dirname $S4MASTERDB`/sess.sq3 3.27 -## skey="skey-`basename $mydir`" 3.28 -syncflag=${db%.*}.synctime 3.29 -runflag=${db%.*}.run 3.30 -userupdateflag=`dirname $S4MASTERDB`/`basename $userupdateflag` 3.31 -test ! -e "$userupdateflag" && return 3.32 -test "$syncflag" -nt "$userupdateflag" && return 3.33 -if [ -s "$runflag" ]; then 3.34 - limit=`cat $runflag|tr -c -d 0-9` 3.35 - if [ -n "$limit" -a "$limit" -gt `date +%s` ]; then 3.36 - err "World $S4WORLD account sync withholded by process $$" 3.37 - return # Running sync by other process not leaching timeout 3.38 +syncaccount_1() { 3.39 + 3.40 + n_m=`query "SELECT printf('%d:%d',\ 3.41 + (SELECT count(*) FROM m.user), \ 3.42 + (SELECT count(*) FROM user));"` 3.43 + n=${n_m%:*}; m=${n_m#*:} # n:m 3.44 + if [ -z "$n" -o "$n" -lt 2 ]; then 3.45 + err "Skipping account sync because m.user cannot be reached[n=$n]." 3.46 + return -3 3.47 fi 3.48 -fi 3.49 -echo $((`date +%s` + 10)) > $runflag # Setting running flag by timeout 10s 3.50 + if [ -z "$forceusersync" -a ! -f db/forceusersync ]; then 3.51 + if [ $((m-n)) -le $((autoremovestop+0)) ]; then 3.52 + # If if-condition evaluation fails, fall through to else-caluse 3.53 + rm -f db/forceusersync 3.54 + else 3.55 + err "More than $autoremovestop users vanished($((m-n)))." 3.56 + err "Automatic removal canceled. If you want to sync user accounts" 3.57 + err "forcibly, touch db/forceusersync file each time of world update." 3.58 + return -4 3.59 + fi 3.60 + fi 3.61 + err "`gdate +%S.%3N` Starting account synchronization[$$]" 3.62 3.63 -# for sub.sq3 3.64 -# 3.65 -# main: user: 'taro', 'hanako', 'shige' 3.66 -# sub: user: 'taro', 'hanako', 'shige' 3.67 -# sub2: user_s: ('taro', 't'), ('hanako', 'h'), ('shige', 's') 3.68 -# then update 3.69 -# 3.70 - 3.71 -## sqlite3 -cmd '.timer 1' -cmd '.echo 1' $db <<EOF 3.72 -err "`gdate +%S.%3N` Starting account synchronization[$$]" 3.73 -## cat > tmp/sql <<EOF 3.74 -num=$(sqlite3 -bail -cmd 'PRAGMA FOREIGN_KEYS=on' $db <<EOF 3.75 -.timeout 1000 3.76 -ATTACH DATABASE "$S4MASTERDB" AS m; 3.77 -CREATE TABLE IF NOT EXISTS user(name, primary key(name)); 3.78 +## num=$(sqlite3 -bail -cmd 'PRAGMA FOREIGN_KEYS=on' $db <<EOF 3.79 + num=$(query <<EOF 3.80 BEGIN; 3.81 DElETE FROM main.user WHERE rowid NOT IN (SELECT rowid FROM m.user); 3.82 INSERT INTO main.user(rowid, name) 3.83 @@ -124,15 +110,52 @@ 3.84 ), a_m AS ( 3.85 SELECT * FROM thisworld EXCEPT SELECT * FROM master 3.86 ) SELECT (SELECT count(*) FROM m_a) + (SELECT count(*) FROM a_m); 3.87 -DETACH DATABASE m; 3.88 EOF 3.89 -) 3.90 -### num=$(sqlite3 -bail -cmd 'PRAGMA FOREIGN_KEYS=on' $db < tmp/sql ) 3.91 -if [ -n "$num" -a $num -eq 0 ]; then 3.92 - err "`gdate +%S.%3N` Account synchronization[$$] done in difference $num" 3.93 - echo "`date '+%F %T'`: Sync done by process $$" >> $syncflag 3.94 -else 3.95 - err "Account synch[$$] failed or bailed with num=[$num]" 3.96 -fi 3.97 -test -e "$runflag" && rm -f "$runflag" 3.98 -return $num 3.99 + ) 3.100 + if [ -n "$num" -a "$num" -eq 0 ]; then 3.101 + err "`gdate +%S.%3N` Account synchronization[$$] done in difference $num" 3.102 + echo "`date '+%F %T'`: Sync done by process $$" >> $syncflag 3.103 + else 3.104 + err "Account synch[$$] failed or bailed with num=[$num]" 3.105 + fi 3.106 + return $num 3.107 +} 3.108 + 3.109 +syncaccount() { 3.110 + forceusersync=$1 3.111 + err "db=$db mas=$S4MASTERDB sessdb=$sessdb" 3.112 + # If in parent world, no need to do rest of jobs 3.113 + if [ -z "$S4MASTERDB" -o ! -s "$S4MASTERDB" ]; then 3.114 + return 3.115 + fi 3.116 + # Confim child 3.117 + if [ "$db" -ef "$S4MASTERDB" ]; then 3.118 + return # Points to the same file 3.119 + fi 3.120 + # File based sync check precedes to DB count check for performance issue 3.121 + syncflag=${db%.*}.synctime 3.122 + runflag=${db%.*}.run 3.123 + userupdateflag=`dirname $S4MASTERDB`/`basename $userupdateflag` 3.124 + test ! -e "$userupdateflag" && return 3.125 + [ -z "$forceusersync" -a "$syncflag" -nt "$userupdateflag" ] && return 3.126 + if [ -s "$runflag" ]; then 3.127 + limit=`cat $runflag|tr -c -d 0-9` 3.128 + if [ -n "$limit" -a "$limit" -gt `date +%s` ]; then 3.129 + err "World $S4WORLD account sync withholded by process $$" 3.130 + return # Running sync by other process not leaching timeout 3.131 + fi 3.132 + fi 3.133 + echo $((`date +%s` + 10)) > $runflag # Setting running flag by timeout 10s 3.134 + query<<-EOF 3.135 + .bail ON 3.136 + ATTACH DATABASE "$S4MASTERDB" AS m; 3.137 + EOF 3.138 + syncaccount_1 3.139 + rc=$? 3.140 + test -e "$runflag" && rm -f "$runflag" 3.141 + query<<-EOF 3.142 + DETACH DATABASE m; 3.143 + .bail OFF 3.144 + EOF 3.145 + return $rc 3.146 +}