How to run a Haskell program endlessly using only Haskell?
Asked Answered
M

1

7

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?

Mooneye answered 13/10, 2015 at 11:40 Comment(6)
CRON, nohup or set it up as a service that can be started.Venetis
What does via key in CLI mean?Reformation
@Venetis I want it in pure Haskell, user might change time, like 3 min, 10min so I have keys in CLI to do so, but don't know how to run it properly. In C you could do while(1) ...Mooneye
@BartekBanachewicz ./app -e -t 5 , e key for endless run, t- time intervalMooneye
@FilipvanHoft so you essentially want a CLI thread for setup and another thread for the program itself? Or are you going to restart the program? What does "endless run" mean? Do you want to start the app once but then allow the interval of processing to be changed?Reformation
@BartekBanachewicz CLI is set at first run. how can I run thread with logic endlessly?Mooneye
R
9

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.

Reformation answered 13/10, 2015 at 11:54 Comment(9)
thanks! That is what I was looking for, I didn't know about foreverMooneye
@FilipvanHoft forever's implementation is pretty straighforward, but just calling e.g. process var at the end of process would make it infinitely recursive (thus looped) as well.Reformation
Any reasons for choosing TVars instead of, say, IORefs ? Is there some atomicity issue in the code above?Lepidosiren
@Lepidosiren I plainly forgot about them. I almost never use "raw" IORefs, but of course the code shown has no possible races. I'll edit it.Reformation
I'm not sure why you'd use an IORef or a TVar at all. Just pass 5000 to process directly,Juncture
@ReinHenrichs And how do you propose the changed value of the interval is propagated to the process thread then?Reformation
I don't see a requirement to do so. OP said "CLI is set at first run", which I take to mean that the timeout value does not need to be changed after the program starts.Juncture
@ReinHenrichs it's in the comments.Reformation
@BartekBanachewicz Can you be specific? I don't see any comment to that effect but I see multiple comments that indicate otherwise, like "./app -e -t 5 , e key for endless run, t- time interval " where it is set via a command line flag. In fact, I see you suggesting it and OP disagreeing with you.Juncture

© 2022 - 2024 — McMap. All rights reserved.