'Scala Play Framework: How to send console output of command to http response and display line by line in textarea

I want to display a console output using textarea in browser, upon button click, I run process on server and send the output to the textarea.

So far I managed to do the following:

on server:

def restartPipeline = authenticationAction { implicit  request =>
 val cmd = Seq("ssh", "[email protected]", "cd my_folder; ./restartPipeline.sh;")
 val lazyLines = cmd.lineStream
 Ok.chunked(Source.apply(lazyLines))
}

in html:

function restartPipeline() {
            var last_response_len = false;
            $.ajax('/restartPipeline', {
                xhrFields: {
                    onprogress: function(e)
                    {
                        console.log("partial response = " + e.currentTarget.response);
                        //update my textarea with this line
                    }
                }
            })
                    .done(function(data)
                    {
                        console.log('Complete response = ' + data);
                    })
                    .fail(function(data)
                    {
                        console.log('Error: ', data);
                    });
        }

Since my ./restartPipeline.sh takes quite some to run and outputs lots of lines in console, my idea is to get each line and append to the textarea so that it looks like a console printing out lines.

But the result of the code above is that all output are sent together. I never get them line by line although lineStream is supposed to return Stream[String].

I looked into documentation and found out that lineStream will block until the next lines comes, but then I don't know how I can achieve what I want. Please offer some hints or sample code.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source