Neil
Neil

Reputation: 359

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)

Upvotes: 2

Views: 127

Answers (2)

Neil
Neil

Reputation: 359

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)

Upvotes: 0

typedfern
typedfern

Reputation: 1257

How do you shut down the server? If it is by killing the process, you will need to catch the ThreadKilled and/or UserInterrupt exceptions, as documented here

(Snap does not appear to have an "in case of forceful shutdown" hook, nor is it really its obligation)

Otherwise, you'll have to add the checkpointing instruction as part of whatever shutdown procedure you have.

Upvotes: 0

Related Questions