Git Product home page Git Product logo

Comments (6)

agocorona avatar agocorona commented on July 23, 2024

Hi,
For what purpose you need it? Probably to save state and continue execution later.

I have to create a basic save-stop-recovery-continue execution functionality.

The structure is:
Log (recovery mode or not) (pointer to the remaining elements not read) (all elements in reverse order)

Really only is necessary to persist the last element since it has all the log (reversed)

to save the state:

saveAndStop file= do
   Log recovery _ log <- getData
   if recovery then return () else do
        writeFile file log
        empty

loadAndRestart file  proc= do
     log <- readFile file
     setData True (reverse log) log
     proc

main= keep $ loadAndRestart  "file" $ do
     logged dothis
     logged dothat
     saveAndStop "file"
     logged doOther
     ....

May execute, stop and continue if re-executed.

But I have to test it. the problem is that since TransIO can execute many threads, saveAndStop my write the log many times, one for each thread. Tomorrow I will try to make it work.

from transient.

habbler avatar habbler commented on July 23, 2024

Hi Alberto,
Thankyou for the speed response.
Yes to stop and continue execution later. Basically suspend and resume.
No hurry though. I am just playing around.

I would have thought that when we have
f = do
logged f1
logged f2
logged f3
suspend
later steps

That there would be three entries in the log, and that we need all three, as I will be running f again and each logged statement will read one of the log entries each?

Rene.

from transient.

agocorona avatar agocorona commented on July 23, 2024

Yes the file will have three entries.
In a general suspend primitive for the TransIO monad, the file may have different many logs. For example :

f = do 
    logged $ f1
    r <-  logged $ choose [1.. 100]
    logged $ print r
    suspend
    later steps

then the file will have 100 logs!!! of three elements each. but the first element is common to all the 100 logs. Since logs are generally very short, repetitions may not be a big problem in principle.

The logs are short since logged erases the log steps that are inside. For example if f1 has dozens of log steps inside, it does not matter, there will be three entries in each log when suspend is executed.

Then the restart would need to re-execute the 100 logs in 100 threads.

from transient.

agocorona avatar agocorona commented on July 23, 2024

Hi Rene:

I solved the single threaded case that may be useful for you:
suspend, checkpoint and restore defined here, with an example:

{-# LANGUAGE   ScopedTypeVariables #-}

import Transient.Base
import Control.Applicative
import Control.Monad.IO.Class
import Transient.Internals
import Transient.Logged
import Control.Exception

suspend :: String -> a -> TransIO a
suspend file x = do
   Log recovery _ log <- getData `onNothing` return (Log False [] [])
   if recovery then return x else do
        liftIO $ writeFile file $ show log
        exit x

checkpoint :: String -> TransIO ()
checkpoint file = do
   Log recovery _ log <- getData  `onNothing` return (Log False [] [])
   if recovery then return () else do
        liftIO $ writeFile file  $ show log
        return ()

restore :: String -> TransIO a -> TransIO a
restore file  proc = do
     log <- liftIO $ (readFile file >>= return . read) `catch` (\(e ::SomeException) -> return [])
     setData $ Log True (reverse log) log
     proc

main = keep $ restore "file" $ do
     logged $ liftIO $ print "hello"
     suspend "file" ()
     logged $ liftIO $ print "world"

from transient.

habbler avatar habbler commented on July 23, 2024

Thankyou!

from transient.

agocorona avatar agocorona commented on July 23, 2024

Added restore, suspend and checkpoint for the general case:

b3a8b6d

If you can test that it works for you, it would be wonderful

from transient.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.