LiveScript icon indicating copy to clipboard operation
LiveScript copied to clipboard

CLI --watch option should kill previous process to be useful for long running scripts

Open ceremcem opened this issue 5 years ago • 3 comments

The --watch switch in CLI is intended for progressive script compilation and works well for that case. This feature is very useful while developing a long-running server side script as well.

However, it behaves unexpected when executing a long-running script, like with lsc -w myscript.ls. That starts just another instance before killing previous instance.

Expected

lsc -w should kill previous instance (if exists) and then start the new process.

Test case

sleep = (ms, f) -> set-timeout f, ms

i = 0 
console.log "script started."
<~ :lo(op) ~> 
    console.log "Hello #{i++}"
    <~ sleep 1000ms
    if i > 10
        return op!
    lo(op)
console.log "script ended."

Output is:

$ lsc -w test.ls 
script started.
Hello 0
Hello 1
Hello 2
script started. <--- Here the file is saved again
Hello 0
Hello 3
Hello 1
Hello 4
Hello 2
Hello 5
Hello 3
Hello 6
Hello 4
Hello 7
Hello 5
Hello 8
Hello 6
Hello 9
Hello 7
Hello 10
Hello 8
script ended.
Hello 9
Hello 10
script ended.

ceremcem avatar Sep 02 '20 22:09 ceremcem

FYI, there's no child process involved. lsc evaluates your code in its own process. If you want this behavior, you can modify your own script to cancel any scheduled work whenever the top level runs again, as long as you store the necessary IDs or callbacks on the global object and not just in a variable.

rhendric avatar Sep 02 '20 23:09 rhendric

If you want this behavior,

FYI: Currently I'm starting LiveScript files via the following Bash script:

#!/bin/bash

watch=
if [[ "$1" = "-w" ]]; then
    echo "Watch mode enabled."
    watch=true
    shift
fi

lsFile="$1"
shift

if [[ -z $watch ]]; then
    lsc $lsFile "$@"
else
    LTIME=
    CMD_PID=

    cleanup(){
        [[ -n $CMD_PID ]] && kill $CMD_PID
    }
    trap cleanup EXIT

    while :; do
       ATIME=`stat -c %Z "$lsFile"`
       if [[ "$ATIME" != "$LTIME" ]]; then
            if [[ -n $CMD_PID ]]; then
                echo "---------------------------------------"
                echo "|             Restarting...           |"
                echo "---------------------------------------"
                kill $CMD_PID
            fi
            lsc $lsFile "$@" &
            CMD_PID=$!
            LTIME=$ATIME
       fi
       sleep 2
    done
fi

Which:

  • Starts a LiveScript process in the background
  • Periodically checks if the source file is changed
  • When source script is changed, kills the previous process and starts another one.

ceremcem avatar Sep 03 '20 21:09 ceremcem

@ceremcem watch processes are quite complicated, unfortunately.

My humble advice to use https://github.com/remy/nodemon.

When source script is changed, kills the previous process and starts another one.

Even nodemon struggles here, so something like livescript would definitely not be expected to handle the issue.

The only lib that fixes many of the cross-platform problem here is https://github.com/paulmillr/chokidar.

but you still have to manually tweak the options for different OS and hardware.

Good Luck !

sourcevault avatar Dec 31 '20 15:12 sourcevault