Skip to content

Facilitate the spawning, maintenance and resolution of asynchronous operations running concurrently

License

Notifications You must be signed in to change notification settings

dmjio/supervise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

supervise

This library facilitates the spawning, maintenance and resolution of asynchronous operations running concurrently. See example below for more info:

module Main where

import Concurrency.Supervise ( supervise, ThreadCount(..), RespawnTime(..), Supervise(..), poll )
import Control.Concurrent
import Control.Exception
import Control.Monad
import System.Exit
import System.Random

secs :: Int -> Int
secs = (*1000000)

main :: IO ()
main = do
  Supervise
    threadcount -- MVar to change the current amount of running threads
    respawnTime -- MVar to change the respawn time of asynchronous actions
    adminThread -- Admin thread for monitoring
        <-
      supervise -- * Main API function we care about
        (ThreadCount 5)   -- This means 5 threads will run 'action' concurrently
        (RespawnTime 0)   -- Time elapsed before the next thread respawns
        action            -- IO action to run
        handleExceptions  -- Exception handler
        handleCompletions -- Completion handler

  -- Monitor the admin thread
  forever $ do
    res <- poll adminThread
    case res of
      Nothing        -> putStrLn "adming running..."
      Just (Right x) -> putStrLn "admin completed.."
      Just (Left x)  -> putStrLn "admin error.."
    threadDelay (secs 2)

  -- Example
  where handleExceptions    = print
        handleCompletions x = print "finished action.."
        action              = do r <- randomRIO (1,10)
                                 threadDelay (secs r)
                                 putStrLn $ "running action..." ++ show r ++ " secs"

About

Facilitate the spawning, maintenance and resolution of asynchronous operations running concurrently

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published