Skip to content

Instantly share code, notes, and snippets.

@jonschoning
Last active January 5, 2023 18:20
Show Gist options
  • Save jonschoning/5d391b3c24e90300e7bd8473e964f9f9 to your computer and use it in GitHub Desktop.
Save jonschoning/5d391b3c24e90300e7bd8473e964f9f9 to your computer and use it in GitHub Desktop.
import Control.Exception
import Control.Concurrent
import System.IO (hSetBuffering, BufferMode(NoBuffering), stdout)
main = do
hSetBuffering stdout NoBuffering
bracket (return "ending")
(\x -> putStrLn x)
(\_ -> threadDelay 10000000000)
import System.Posix.Signals
import Control.Concurrent (threadDelay)
import Control.Concurrent.MVar
termHandler :: MVar () -> Handler
termHandler v = CatchOnce $ do
putStrLn "Caught SIGTERM"
putMVar v ()
loop :: MVar () -> IO ()
loop v = do
putStrLn "Still running"
threadDelay 1000000
val <- tryTakeMVar v
case val of
Just _ -> putStrLn "Quitting" >> return ()
Nothing -> loop v
main = do
v <- newEmptyMVar
installHandler sigTERM (termHandler v) Nothing
loop v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment