@@ -141,7 +141,7 @@ private function cronAutoPurge() {
141141 *
142142 * export sessions
143143 */
144- public function cronAutoExport () {
144+ public function cronAutoExport (): \ Generator {
145145 $ dtz = ini_get ('date.timezone ' );
146146 if ($ dtz === '' ) {
147147 $ dtz = 'UTC ' ;
@@ -223,10 +223,24 @@ public function cronAutoExport() {
223223 $ phonetrackUserIds = $ this ->sessionMapper ->getUserIds ();
224224
225225 foreach ($ phonetrackUserIds as $ userId ) {
226+ $ userSessions = $ this ->sessionMapper ->findByUser ($ userId );
227+ $ candidateUserSessions = array_filter ($ userSessions , static function (Session $ session ) {
228+ return $ session ->getAutoexport () !== 'no ' ;
229+ });
230+ $ candidateUserSessions = array_values ($ candidateUserSessions );
231+ // if there is no session with autoexport enabled: skip this user
232+ if (count ($ candidateUserSessions ) === 0 ) {
233+ continue ;
234+ }
235+
236+ // if the user does not exist: skip this this user
237+ if (!$ this ->userManager ->userExists ($ userId )) {
238+ continue ;
239+ }
240+
226241 $ userFolder = $ this ->root ->getUserFolder ($ userId );
227- $ sessions = $ this ->sessionMapper ->findByUser ($ userId );
228242
229- foreach ($ sessions as $ session ) {
243+ foreach ($ candidateUserSessions as $ session ) {
230244 $ dbname = $ session ->getName ();
231245 $ dbtoken = $ session ->getToken ();
232246 $ dbexportType = $ session ->getAutoexport ();
@@ -248,14 +262,22 @@ public function cronAutoExport() {
248262 $ rel_path = str_replace ($ userFolder ->getPath (), '' , $ dir ->getPath ());
249263 $ exportPath = $ rel_path . '/ ' . $ exportName ;
250264 if (!$ dir ->nodeExists ($ exportName )) {
251- $ this ->exportSession ($ dbname , $ dbtoken , $ exportPath , $ userId , $ filters );
265+ [$ done , $ warning ] = $ this ->exportSession ($ dbname , $ dbtoken , $ exportPath , $ userId , $ filters );
266+ if ($ warning === 1 ) {
267+ yield "Not exporting session $ dbname of $ userId in ' $ exportPath', no points " ;
268+ } else {
269+ yield "Exporting session $ dbname of $ userId in ' $ exportPath' " ;
270+ }
271+ } else {
272+ yield "Cannot export session $ dbname of $ userId in ' $ exportPath', file already exists " ;
252273 }
253274 }
254275 }
255276 }
256277 // we run the auto purge method AFTER the auto export
257278 // to avoid deleting data before it has been eventually exported
258279 $ this ->cronAutoPurge ();
280+ return [];
259281 }
260282
261283 /**
@@ -298,7 +320,7 @@ public function exportDevice(Device $device, string $userId, string $target): vo
298320 $ file ->touch ();
299321 }
300322
301- public function exportSession (string $ name , string $ token , string $ target , string $ username = '' , ?array $ filters = null ) {
323+ public function exportSession (string $ name , string $ token , string $ target , string $ username = '' , ?array $ filters = null ): array {
302324 date_default_timezone_set ('UTC ' );
303325 $ done = false ;
304326 $ warning = 0 ;
@@ -1191,4 +1213,11 @@ public function deleteSession(Session $session): void {
11911213 }
11921214 $ this ->sessionMapper ->deleteSession ($ session ->getUser (), $ session ->getId ());
11931215 }
1216+
1217+ public function cleanupUser (string $ userId ): void {
1218+ $ sessions = $ this ->sessionMapper ->findByUser ($ userId );
1219+ foreach ($ sessions as $ session ) {
1220+ $ this ->deleteSession ($ session );
1221+ }
1222+ }
11941223}
0 commit comments