126126}
127127
128128#######################################################################################################
129- // Cleanup logs in database
129+ // Cleanup logs in master database (only the "master-server")
130130#######################################################################################################
131- //* Keep 7 days in sys_log
132- $ tstamp = time () - (60 *60 *24 *7 );
133- $ sql = "DELETE FROM sys_log WHERE tstamp < $ tstamp AND server_id != 0 " ;
134- $ app ->db ->query ($ sql );
135-
136- //* Keep 7 days in sys_datalog
137- $ tstamp = time () - (60 *60 *24 *7 );
138- $ sql = "DELETE FROM sys_datalog WHERE tstamp < $ tstamp AND server_id != 0 " ;
139- $ app ->db ->query ($ sql );
140131
132+ if ($ app ->dbmaster == $ app ->db ) {
133+ /** 7 days */
134+ $ tstamp = time () - (60 *60 *24 *7 );
135+
136+ /*
137+ * Keep 7 days in sys_log
138+ * (we can delete the old items, because if they are OK, they don't interrest anymore
139+ * if they are NOT ok, the server will try to process them in 1 minute and so the
140+ * error appears again after 1 minute. So it is no problem to delete the old one!
141+ */
142+ $ sql = "DELETE FROM sys_log WHERE tstamp < $ tstamp AND server_id != 0 " ;
143+ $ app ->dbmaster ->query ($ sql );
144+
145+ /*
146+ * The sys_datalog is more difficult.
147+ * 1) We have to keet ALL entries with
148+ * server_id=0, because they depend on ALL servers (even if they are not
149+ * actually in the system (and will be insered in 3 days or so).
150+ * 2) We have to keey ALL entries which are not actually precessed by the
151+ * server never mind how old they are!
152+ */
153+
154+ /* First we need all servers and the last sys_datalog-id they processed */
155+ $ sql = "SELECT server_id, updated FROM server ORDER BY server_id " ;
156+ $ records = $ app ->dbmaster ->queryAllRecords ($ sql );
157+
158+ /* Then delete server by server */
159+ foreach ($ records as $ server ) {
160+ $ sql = "DELETE FROM sys_datalog WHERE tstamp < " . $ tstamp .
161+ " AND server_id != 0 " . // to be more secure!
162+ " AND server_id = " . intval ($ server ['server_id ' ]) .
163+ " AND datalog_id < " . intval ($ server ['updated ' ]);
164+ // echo $sql . "\n";
165+ $ app ->dbmaster ->query ($ sql );
166+ }
167+ }
141168
142169die ("finished. \n" );
143170?>
0 commit comments