Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ceremcem opened this issue Sep 2, 2020 · 3 comments
Labels

Comments

@ceremcem
Copy link

ceremcem commented Sep 2, 2020

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.
@rhendric
Copy link
Collaborator

rhendric commented Sep 2, 2020

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.

@ceremcem
Copy link
Author

ceremcem commented Sep 3, 2020

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.

@sourcevault
Copy link

@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 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants