Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a Haskell program endlessly using only Haskell?

I have small program that need to be executed every 5 minutes.

For now, I have shell script that perform that task, but I want to provide for user ability to run it without additional scripts via key in CLI.

What is the best way to achieve this?

like image 876
Filip van Hoft Avatar asked Oct 13 '15 11:10

Filip van Hoft


1 Answers

I presume you'll want something like that (more or less pseudocode):

import Control.Concurrent (forkIO, threadDelay)
import Data.IORef
import Control.Monad (forever)

main = do
    var <- newIORef 5000
    forkIO (forever $ process var)
    forever $ readInput var

process var = do
    doActualProcessing

    interval <- readIORef var
    _ <- threadDelay interval

readInput var = do
    newInterval <- readLn
    writeIORef var newInterval

If you need to pass some more complex data from the input thread to the processing thread, MVars or TVars could be a better choice than IORefs.

like image 159
Bartek Banachewicz Avatar answered Nov 11 '22 23:11

Bartek Banachewicz