@@ -163,11 +163,11 @@ smpServer :: TMVar Bool -> ServerConfig -> Maybe AttachHTTP -> M ()
163163smpServer started cfg@ ServerConfig {transports, transportConfig = tCfg} attachHTTP_ = do
164164 s <- asks server
165165 pa <- asks proxyAgent
166- msgStats <- processServerMessages
166+ msgStats_ <- processServerMessages
167167 ntfStats <- restoreServerNtfs
168- liftIO $ printMessageStats " messages" msgStats
168+ liftIO $ mapM_ ( printMessageStats " messages" ) msgStats_
169169 liftIO $ printMessageStats " notifications" ntfStats
170- restoreServerStats msgStats ntfStats
170+ restoreServerStats msgStats_ ntfStats
171171 raceAny_
172172 ( serverThread s " server subscribedQ" subscribedQ subscribers subClients pendingSubEvents subscriptions cancelSub
173173 : serverThread s " server ntfSubscribedQ" ntfSubscribedQ Env. notifiers ntfSubClients pendingNtfSubEvents ntfSubscriptions (\ _ -> pure () )
@@ -385,12 +385,15 @@ smpServer started cfg@ServerConfig {transports, transportConfig = tCfg} attachHT
385385 threadDelay' interval
386386 old <- expireBeforeEpoch expCfg
387387 now <- systemSeconds <$> getSystemTime
388- Sum deleted <- withActiveMsgQueues ms $ expireQueueMsgs now ms old
389- atomicModifyIORef'_ (msgExpired stats) (+ deleted)
390- logInfo $ " STORE: expireMessagesThread, expired " <> tshow deleted <> " messages"
388+ msgStats@ MessageStats {storedMsgsCount = stored, expiredMsgsCount = expired} <-
389+ withActiveMsgQueues ms $ expireQueueMsgs now ms old
390+ atomicWriteIORef (msgCount stats) stored
391+ atomicModifyIORef'_ (msgExpired stats) (+ expired)
392+ printMessageStats " STORE: messages" msgStats
391393 where
392- expireQueueMsgs now ms old rId q =
393- either (const 0 ) Sum <$> runExceptT (idleDeleteExpiredMsgs now ms rId q old)
394+ expireQueueMsgs now ms old rId q = fmap (fromRight newMessageStats) . runExceptT $ do
395+ (expired_, stored) <- idleDeleteExpiredMsgs now ms rId q old
396+ pure MessageStats {storedMsgsCount = stored, expiredMsgsCount = fromMaybe 0 expired_, storedQueues = 1 }
394397
395398 expireNtfsThread :: ServerConfig -> M ()
396399 expireNtfsThread ServerConfig {notificationExpiration = expCfg} = do
@@ -1731,26 +1734,26 @@ exportMessages tty ms f drainMsgs = do
17311734 exitFailure
17321735 encodeMessages rId = mconcat . map (\ msg -> BLD. byteString (strEncode $ MLRv3 rId msg) <> BLD. char8 ' \n ' )
17331736
1734- processServerMessages :: M MessageStats
1737+ processServerMessages :: M ( Maybe MessageStats )
17351738processServerMessages = do
17361739 old_ <- asks (messageExpiration . config) $>>= (liftIO . fmap Just . expireBeforeEpoch)
17371740 expire <- asks $ expireMessagesOnStart . config
17381741 asks msgStore >>= liftIO . processMessages old_ expire
17391742 where
1740- processMessages :: Maybe Int64 -> Bool -> AMsgStore -> IO MessageStats
1743+ processMessages :: Maybe Int64 -> Bool -> AMsgStore -> IO ( Maybe MessageStats )
17411744 processMessages old_ expire = \ case
17421745 AMS SMSMemory ms@ STMMsgStore {storeConfig = STMStoreConfig {storePath}} -> case storePath of
1743- Just f -> ifM (doesFileExist f) (importMessages False ms f old_) (pure newMessageStats )
1744- Nothing -> pure newMessageStats
1746+ Just f -> ifM (doesFileExist f) (Just <$> importMessages False ms f old_) (pure Nothing )
1747+ Nothing -> pure Nothing
17451748 AMS SMSJournal ms
1746- | expire -> case old_ of
1749+ | expire -> Just <$> case old_ of
17471750 Just old -> do
17481751 logInfo " expiring journal store messages..."
17491752 withAllMsgQueues False ms $ processExpireQueue old
17501753 Nothing -> do
17511754 logInfo " validating journal store messages..."
17521755 withAllMsgQueues False ms $ processValidateQueue
1753- | otherwise -> logWarn " skipping message expiration" $> newMessageStats
1756+ | otherwise -> logWarn " skipping message expiration" $> Nothing
17541757 where
17551758 processExpireQueue old rId q =
17561759 runExceptT expireQueue >>= \ case
@@ -1887,8 +1890,8 @@ saveServerStats =
18871890 B. writeFile f $ strEncode stats
18881891 logInfo " server stats saved"
18891892
1890- restoreServerStats :: MessageStats -> MessageStats -> M ()
1891- restoreServerStats msgStats ntfStats = asks (serverStatsBackupFile . config) >>= mapM_ restoreStats
1893+ restoreServerStats :: Maybe MessageStats -> MessageStats -> M ()
1894+ restoreServerStats msgStats_ ntfStats = asks (serverStatsBackupFile . config) >>= mapM_ restoreStats
18921895 where
18931896 restoreStats f = whenM (doesFileExist f) $ do
18941897 logInfo $ " restoring server stats from file " <> T. pack f
@@ -1897,9 +1900,11 @@ restoreServerStats msgStats ntfStats = asks (serverStatsBackupFile . config) >>=
18971900 s <- asks serverStats
18981901 AMS _ st <- asks msgStore
18991902 _qCount <- M. size <$> readTVarIO (activeMsgQueues st)
1900- let _msgCount = storedMsgsCount msgStats
1903+ let _msgCount = maybe statsMsgCount storedMsgsCount msgStats_
19011904 _ntfCount = storedMsgsCount ntfStats
1902- liftIO $ setServerStats s d {_qCount, _msgCount, _ntfCount, _msgExpired = _msgExpired d + expiredMsgsCount msgStats, _msgNtfExpired = _msgNtfExpired d + expiredMsgsCount ntfStats}
1905+ _msgExpired' = _msgExpired d + maybe 0 expiredMsgsCount msgStats_
1906+ _msgNtfExpired' = _msgNtfExpired d + expiredMsgsCount ntfStats
1907+ liftIO $ setServerStats s d {_qCount, _msgCount, _ntfCount, _msgExpired = _msgExpired', _msgNtfExpired = _msgNtfExpired'}
19031908 renameFile f $ f <> " .bak"
19041909 logInfo " server stats restored"
19051910 compareCounts " Queue" statsQCount _qCount
0 commit comments