@@ -163,11 +163,11 @@ smpServer :: TMVar Bool -> ServerConfig -> Maybe AttachHTTP -> M ()
163
163
smpServer started cfg@ ServerConfig {transports, transportConfig = tCfg} attachHTTP_ = do
164
164
s <- asks server
165
165
pa <- asks proxyAgent
166
- msgStats <- processServerMessages
166
+ msgStats_ <- processServerMessages
167
167
ntfStats <- restoreServerNtfs
168
- liftIO $ printMessageStats " messages" msgStats
168
+ liftIO $ mapM_ ( printMessageStats " messages" ) msgStats_
169
169
liftIO $ printMessageStats " notifications" ntfStats
170
- restoreServerStats msgStats ntfStats
170
+ restoreServerStats msgStats_ ntfStats
171
171
raceAny_
172
172
( serverThread s " server subscribedQ" subscribedQ subscribers subClients pendingSubEvents subscriptions cancelSub
173
173
: serverThread s " server ntfSubscribedQ" ntfSubscribedQ Env. notifiers ntfSubClients pendingNtfSubEvents ntfSubscriptions (\ _ -> pure () )
@@ -385,12 +385,15 @@ smpServer started cfg@ServerConfig {transports, transportConfig = tCfg} attachHT
385
385
threadDelay' interval
386
386
old <- expireBeforeEpoch expCfg
387
387
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
391
393
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 }
394
397
395
398
expireNtfsThread :: ServerConfig -> M ()
396
399
expireNtfsThread ServerConfig {notificationExpiration = expCfg} = do
@@ -1731,26 +1734,26 @@ exportMessages tty ms f drainMsgs = do
1731
1734
exitFailure
1732
1735
encodeMessages rId = mconcat . map (\ msg -> BLD. byteString (strEncode $ MLRv3 rId msg) <> BLD. char8 ' \n ' )
1733
1736
1734
- processServerMessages :: M MessageStats
1737
+ processServerMessages :: M ( Maybe MessageStats )
1735
1738
processServerMessages = do
1736
1739
old_ <- asks (messageExpiration . config) $>>= (liftIO . fmap Just . expireBeforeEpoch)
1737
1740
expire <- asks $ expireMessagesOnStart . config
1738
1741
asks msgStore >>= liftIO . processMessages old_ expire
1739
1742
where
1740
- processMessages :: Maybe Int64 -> Bool -> AMsgStore -> IO MessageStats
1743
+ processMessages :: Maybe Int64 -> Bool -> AMsgStore -> IO ( Maybe MessageStats )
1741
1744
processMessages old_ expire = \ case
1742
1745
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
1745
1748
AMS SMSJournal ms
1746
- | expire -> case old_ of
1749
+ | expire -> Just <$> case old_ of
1747
1750
Just old -> do
1748
1751
logInfo " expiring journal store messages..."
1749
1752
withAllMsgQueues False ms $ processExpireQueue old
1750
1753
Nothing -> do
1751
1754
logInfo " validating journal store messages..."
1752
1755
withAllMsgQueues False ms $ processValidateQueue
1753
- | otherwise -> logWarn " skipping message expiration" $> newMessageStats
1756
+ | otherwise -> logWarn " skipping message expiration" $> Nothing
1754
1757
where
1755
1758
processExpireQueue old rId q =
1756
1759
runExceptT expireQueue >>= \ case
@@ -1887,8 +1890,8 @@ saveServerStats =
1887
1890
B. writeFile f $ strEncode stats
1888
1891
logInfo " server stats saved"
1889
1892
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
1892
1895
where
1893
1896
restoreStats f = whenM (doesFileExist f) $ do
1894
1897
logInfo $ " restoring server stats from file " <> T. pack f
@@ -1897,9 +1900,11 @@ restoreServerStats msgStats ntfStats = asks (serverStatsBackupFile . config) >>=
1897
1900
s <- asks serverStats
1898
1901
AMS _ st <- asks msgStore
1899
1902
_qCount <- M. size <$> readTVarIO (activeMsgQueues st)
1900
- let _msgCount = storedMsgsCount msgStats
1903
+ let _msgCount = maybe statsMsgCount storedMsgsCount msgStats_
1901
1904
_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'}
1903
1908
renameFile f $ f <> " .bak"
1904
1909
logInfo " server stats restored"
1905
1910
compareCounts " Queue" statsQCount _qCount
0 commit comments