haskellhaskell-snap-framework

how to run cleanup on snap shutdown


I'm using serversession-backend-acid-state for sessions, along with serversession-frontend-snap.

If I run with the memory version of acid-state, everything works fine:

-- create state container in memory from initial state
acidMem :: IO (AcidStorage SessionMap)
acidMem = AcidStorage <$> openMemoryState emptyState

app :: SnapletInit App App
app = makeSnaplet "app" "An snaplet example application." Nothing $ do
  conf <- getSnapletUserConfig
  h <- nestSnaplet "" heist $ heistInit "templates"
  s <- nestSnaplet "sess"sess $ SS.simpleServerSessionManager acidDisk id

I can successfully use the disk version of acid-state by using the following:

acidDisk :: IO (AcidStorage SessionMap)
acidDisk = AcidStorage <$> openLocalState emptyState

My problem arises when I shut down snap; I don't know where I can properly close acid-state via (createCheckpointAndClose . acidState). Without a proper shutdown, I will get an error when restarting snap.

I see the cleanup function in Main.hs, but I don't understand how I can use this to close acid-state. What is the best approach for this?

Edit: I've discovered onUnload, but can't wrap the simpleServerSessionManager with it.

Edit #2: I've determined how to use onUnload to get it working with acidDisk:

    ad <- liftIO $ fmap opts . createState =<< acidDisk
    s <- nestSnaplet "sess"sess $ 
        SS.initServerSessionManager (return ad)
    onUnload (createCheckpointAndClose $ acidState $ storage ad)

Solution

  • For reference, I solved it by getting a reference to the acid-state, and then using onUnload to close it:

    acidDisk :: IO (AcidStorage SessionMap)
    acidDisk = AcidStorage <$> openLocalState emptyState
    
    app :: SnapletInit App App
    app = makeSnaplet "app" "An snaplet example application." Nothing $ do
      conf <- getSnapletUserConfig
      h <- nestSnaplet "" heist $ heistInit "templates"
    
      ad <- liftIO $ fmap opts . createState =<< acidDisk
      s <- nestSnaplet "sess"sess $ 
            SS.initServerSessionManager (return ad)
      onUnload (createCheckpointAndClose $ acidState $ storage ad)