2014-12-01 112 views
1

我有一個Yesod應用程序與購物車,它的效果很好。我現在想要在外部應用程序(腳手架中的「app/tasks.hs」)中清除過期的購物車,這些應用程序將使用cron運行。下面的代碼有效,但每條日誌消息後面都有一個空白行。難道我做錯了什麼?旁邊的問題:我怎麼能把它轉換成快速記錄器?我讀過的腳手架Application.hs但我沒有管理如何避免基礎的創作...如何在沒有Foundation的情況下記錄空行?

import Control.Monad.Logger     (runStdoutLoggingT, LoggingT) 
import Database.Persist.Sqlite    (runSqlPool) 
import Data.Text       (append) 
import Import 
import qualified Database.Esqueleto as E 

runQueries :: UTCTime -> NominalDiffTime -> SqlPersistT (ResourceT (LoggingT IO))() 
runQueries now expiration = do 
    $(logInfo) "Delete expired shopping carts." 
    carts <- 
     E.select $ 
     E.from $ \(c, u) -> do 
     E.where_ ( c E.^. CartUpdated E.<. E.val (addUTCTime (- expiration) now) 
       E.&&. c E.^. CartCustomer E.==. u E.^. UserId 
       ) 
     return (c, u) 
    forM_ carts $ \(cart, user) -> do 
     cartitems <- selectList [ CartItemCart ==. entityKey cart ] [] 
     forM_ cartitems $ \ci -> do 
      update (cartItemItem $ entityVal ci) [ItemStock +=. (cartItemQuantity $ entityVal ci)] 
      delete $ entityKey ci 
     delete $ entityKey cart 
     $(logInfo) $ "Deleted cart: " `append` (userEmail $ entityVal user) 

main :: IO() 
main = do 
    -- Get the settings from all relevant sources 
    settings <- loadAppSettingsArgs 
     -- fall back to compile-time values, set to [] to require values at runtime 
     [configSettingsYmlValue] 

     -- allow environment variables to override 
     useEnv 

    now <- getCurrentTime 

    pool <- createPoolConfig (appDatabaseConf settings) 

    runStdoutLoggingT $ runResourceT $ runSqlPool (runQueries now $ appCartExpiration settings) pool 

回答

3

良好的漁獲物,這實際上是在單子記錄器中的錯誤。我已經發布瞭解決它的版本0.3.10.1。

編輯下面是使用快速記錄器與單子記錄器的一個例子:

​​
+0

大就知道了!所以(因爲你沒有對我的代碼做任何評論;-))我推斷它並不是太糟糕。關於快速記錄器,在這種情況下它有意義嗎? – gueux 2014-12-02 12:00:13

+0

出於好奇,爲什麼這個bug不影響Yesod? – gueux 2014-12-02 12:05:06

+0

Yesod使用'messageLoggerSource'來格式化日誌消息,因此它沒有碰到monad-logger的越野車部分。關於快速記錄器:您可以嘗試切換,看看它是否會提高速度。我會想象它會的,但我無法肯定地預測這一點。 – 2014-12-02 12:12:42

相關問題