devinus / execjs

Run JavaScript code from Elixir
https://github.com/devinus/execjs
Creative Commons Zero v1.0 Universal
151 stars 22 forks source link

Any way to run asynchronously? #13

Open alexdovzhanyn opened 6 years ago

alexdovzhanyn commented 6 years ago

I'm currently looking at trying to use this library to add javascript as a contracting language to the UltraDark Blockchain, and I've gotten it to fit quite nicely!

The one thing I'm worried about is having someone write an infinite loop as part of their contract, because this would essentially lock all nodes that receive the contract. I was thinking of having each node validate the contract by running all the functions defined in the javascript, and if any of the functions take longer than 30 seconds to return, just kill the transaction.

There are is an issue with my approach (that I was hoping you could help solve): When running an infinite loop within a separate process, I can't kill the Execjs process by doing Process.exit(pid, :reason). This returns true, but running ps aux | grep execjs reveals that the process is still running. I'm not sure of any other good alternative to testing against infinite loops without disabling a looping mechanism altogether. Any suggestions would be very greatly appreciated!

vans163 commented 6 years ago

Probably needs a simple patch to bootstrap the js process launcher. I remember doing this before.. also Raik did this afaik.. its more complex then it seems, but it like involves a shell script being held open and BEAM opens the port to hold it. When elixir proc dies, port closes, shell script gets STDOUT closed, then shell script is responsible to kill the JS proc (shell script obv launches JS proc in nonblocking way).

Cool project BTW.

alexdovzhanyn commented 6 years ago

Thanks! I'll have to check out how they do this and implement it. Currently the process blocks until javascript processing is completed so thats not ideal