s4

view s4-world.sh @ 817:d2c4c473e08e

Account synchronization more prudent
author HIROSE Yuuji <yuuji@gentei.org>
date Wed, 17 Jun 2020 13:54:01 +0900
parents 52c249ab4ca6
children e64d18e78507
line source
1 #!/bin/sh
3 autoremovestop=2
5 # Setup variables for running time
6 if $isCGI; then
7 case "$S4WORLDLIST" in
8 *:*:*)
9 worldlistfile=cache/worldlist
10 worldgrpfile=cache/worldgrps
11 worldoptionfile=cache/worldoption
12 worldnamefile=cache/worldname
13 if [ ! -e $worldlistfile -o $worldlistfile -ot s4-config.sh \
14 -o ! -e $worldoptionfile -o $worldoptionfile -ot s4-config.sh \
15 -o s4-world.sh -nt $worldoptionfile ]
16 then
17 echo S4MASTERURL=$S4MASTERURL >> tmp/debug.out
18 cat <<-EOF > $worldlistfile
19 <div><table>
20 <tr><th>World List</th></tr>
21 <tr><td title="Base World$nl拠点となるWorldです">&rArr;
22 <a href="${S4MASTERURL:-$URL}">Base</a></td></tr>
23 EOF
24 true > $worldoptionfile
25 for i in $S4WORLDLIST; do
26 echo $i | {
27 IFS=: read name short d
28 cgi="s4-world-$short$cgiext"
29 conf="s4-config-$short.sh"
30 cat<<-EOF >>$worldlistfile
31 <tr><td title="$d">&rArr; <a href="$cgi">$name</a></td></tr>
32 EOF
33 cat<<-EOF >>$worldoptionfile
34 <option title="$d" value="$conf">$name</option>
35 EOF
36 echo "$name" > $worldnamefile.$short
37 }
38 done
39 if [ -s "$worldoptionfile" ]; then
40 echo "<option value=\"s4-config.sh\">Base</option>" >> $worldoptionfile
41 echo "</table>$nl</div>" >> $worldlistfile
42 sed 's/href="\([^>]*\)"/href="\1?grps"/' $worldlistfile \
43 > $worldgrpfile
44 else
45 true > $worldoptionfile; true > $worldlistfile # Remove contents
46 rm -f ${worldnamefile}.*
47 fi
48 fi
49 if [ -s "$worldlistfile" ]; then
50 S4WORLDS="▼spaste(\`$worldlistfile')"
51 S4WORLDNAME=${S4WORLD:+`cat $worldnamefile.$S4WORLD`}
52 S4WORLDGRPS="▼spaste(\`$worldgrpfile')"
53 fi
54 ;;
55 esac
56 fi
59 syncaccount_1() {
61 n_m=`query "SELECT printf('%d:%d',\
62 (SELECT count(*) FROM m.user), \
63 (SELECT count(*) FROM user));"`
64 n=${n_m%:*}; m=${n_m#*:} # n:m
65 if [ -z "$n" -o "$n" -lt 2 ]; then
66 err "Skipping account sync because m.user cannot be reached[n=$n]."
67 return -3
68 fi
69 if [ -z "$forceusersync" -a ! -f db/forceusersync ]; then
70 if [ $((m-n)) -le $((autoremovestop+0)) ]; then
71 # If if-condition evaluation fails, fall through to else-caluse
72 rm -f db/forceusersync
73 else
74 err "More than $autoremovestop users vanished($((m-n)))."
75 err "Automatic removal canceled. If you want to sync user accounts"
76 err "forcibly, touch db/forceusersync file each time of world update."
77 return -4
78 fi
79 fi
80 err "`gdate +%S.%3N` Starting account synchronization[$$]"
82 ## num=$(sqlite3 -bail -cmd 'PRAGMA FOREIGN_KEYS=on' $db <<EOF
83 num=$(query <<EOF
84 BEGIN;
85 DElETE FROM main.user WHERE rowid NOT IN (SELECT rowid FROM m.user);
86 INSERT INTO main.user(rowid, name)
87 SELECT rowid, name FROM m.user
88 WHERE m.user.rowid NOT IN (SELECT rowid FROM user);
89 UPDATE user SET name = (SELECT name FROM m.user WHERE main.user.rowid=m.user.rowid);
90 DELETE FROM main.user_s WHERE rowid NOT IN (SELECT rowid FROM m.user_s);
91 REPLACE INTO main.user_s(rowid, name, key, type, val, bin)
92 SELECT rowid,* FROM m.user_s;
93 DELETE FROM main.user_m WHERE rowid NOT IN (SELECT rowid FROM m.user_m);
94 REPLACE INTO main.user_m(rowid, name, key, type, val, bin)
95 SELECT rowid,* FROM m.user_m
96 WHERE key NOT LIKE '%cache%';
97 END;
99 /* Compare user tables */
100 WITH master AS (
101 SELECT p.rowid,* FROM m.user p
102 NATURAL LEFT JOIN m.user_s
103 NATURAL LEFT JOIN m.user_m
104 ), thisworld AS (
105 SELECT p.rowid,* FROM user p
106 NATURAL LEFT JOIN user_s
107 NATURAL LEFT JOIN user_m
108 ), m_a AS (
109 SELECT * FROM master EXCEPT SELECT * FROM thisworld
110 ), a_m AS (
111 SELECT * FROM thisworld EXCEPT SELECT * FROM master
112 ) SELECT (SELECT count(*) FROM m_a) + (SELECT count(*) FROM a_m);
113 EOF
114 )
115 if [ -n "$num" -a "$num" -eq 0 ]; then
116 err "`gdate +%S.%3N` Account synchronization[$$] done in difference $num"
117 echo "`date '+%F %T'`: Sync done by process $$" >> $syncflag
118 else
119 err "Account synch[$$] failed or bailed with num=[$num]"
120 fi
121 return $num
122 }
124 syncaccount() {
125 forceusersync=$1
126 err "db=$db mas=$S4MASTERDB sessdb=$sessdb"
127 # If in parent world, no need to do rest of jobs
128 if [ -z "$S4MASTERDB" -o ! -s "$S4MASTERDB" ]; then
129 return
130 fi
131 # Confim child
132 if [ "$db" -ef "$S4MASTERDB" ]; then
133 return # Points to the same file
134 fi
135 # File based sync check precedes to DB count check for performance issue
136 syncflag=${db%.*}.synctime
137 runflag=${db%.*}.run
138 userupdateflag=`dirname $S4MASTERDB`/`basename $userupdateflag`
139 test ! -e "$userupdateflag" && return
140 [ -z "$forceusersync" -a "$syncflag" -nt "$userupdateflag" ] && return
141 if [ -s "$runflag" ]; then
142 limit=`cat $runflag|tr -c -d 0-9`
143 if [ -n "$limit" -a "$limit" -gt `date +%s` ]; then
144 err "World $S4WORLD account sync withholded by process $$"
145 return # Running sync by other process not leaching timeout
146 fi
147 fi
148 echo $((`date +%s` + 10)) > $runflag # Setting running flag by timeout 10s
149 query<<-EOF
150 .bail ON
151 ATTACH DATABASE "$S4MASTERDB" AS m;
152 EOF
153 syncaccount_1
154 rc=$?
155 test -e "$runflag" && rm -f "$runflag"
156 query<<-EOF
157 DETACH DATABASE m;
158 .bail OFF
159 EOF
160 return $rc
161 }