From a040fa65bbdb10a99d7756bbbd00432016235526 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Sat, 14 Jan 2023 19:21:10 +0400 Subject: [PATCH] core: run cleanup for all users (#1746) --- src/Simplex/Chat.hs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index b673c3349d..c05534f7f7 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -208,7 +208,7 @@ startChatController currentUser subConns enableExpireCIs = do cleanupAsync <- asks cleanupManagerAsync readTVarIO cleanupAsync >>= \case Nothing -> do - a <- Just <$> async (void . runExceptT $ cleanupManager currentUser) + a <- Just <$> async (void $ runExceptT cleanupManager) atomically $ writeTVar cleanupAsync a _ -> pure () startExpireCIs users = @@ -1882,15 +1882,20 @@ subscribeUserConnections agentBatchSubscribe user = do cleanupManagerInterval :: Int cleanupManagerInterval = 1800 -- 30 minutes -cleanupManager :: forall m. ChatMonad m => User -> m () -cleanupManager user = do +cleanupManager :: forall m. ChatMonad m => m () +cleanupManager = do forever $ do - flip catchError (toView . CRChatError (Just user)) $ do + flip catchError (toView . CRChatError Nothing) $ do waitChatStarted - cleanupTimedItems + users <- withStore' getUsers + let (us, us') = partition activeUser users + forM_ us cleanupUser + forM_ us' cleanupUser threadDelay $ cleanupManagerInterval * 1000000 where - cleanupTimedItems = do + cleanupUser user = + cleanupTimedItems user `catchError` (toView . CRChatError (Just user)) + cleanupTimedItems user = do ts <- liftIO getCurrentTime let startTimedThreadCutoff = addUTCTime (realToFrac cleanupManagerInterval) ts timedItems <- withStore' $ \db -> getTimedItems db user startTimedThreadCutoff