What's up guys? This episode I wanted to talk about a feature of HatchBox that has caught a lot of people's eyes and they have asked me to talk about how it works, so what I'm talking about here in this episode is the real time SSH logs that happen in your browser, so this is pretty straight forward actually but when you click deploy and your app begins deploying, you can see a live output of the logs as it's running, so it's doing all of this and updating these logs and generating all of this output and sending it back over from the server to the HatchBox server to your browser using ActionCable.
tail -n 300 /home/deploy/errbit/current/logs/production.log
This command if you go into your server and run it, it will just print out the last 300 lines of that, so what I'm doing is I create an SSH connection in the background job, to talk to your server just like you do in your terminal, and then I tell it hey run this command just like you do when you type it in, hit enter, and then you immediately get the output in your console, in your terminal and I will get effectively the same thing but I'll get it in chunks of text in the SSH connection which will call a little ruby callback that I say: Hey, go pipe this over to the browser with ActionCable, and that's exactly what we do, so that is all that's really going on here, and we have this interesting modification for the NET SSH connection session, this is the class that is the one actually executing commands on a live SSH session or connection, and so we have this method that I've taken from a Stack Overflow post, that added this
exec_sc method and so this will retrive, or it will execute a command and retreive the logs as well as check for exit codes and exit signals which is really important to determine if the command failed or not, so what happens is it creates that channel, and it sits here and loops after it has set up these callbacks, so what happens is it created this SSH connection, it is sending and receiving messages back and forth and doing all of this as part of this loop, and whenever it receives a message of data, it will call this block, and so you're free to do anything you want in there, and so for me, if you passed in a script, I will call
So that's it for this episode, basically the important piece here was that callback method on data for our NET SSH connection that gives us periodic status updates for the long running job which is really really nice, so anytime that you want to implement something like this to show progress to your users in real time, you're gonna want to do basically the same thing, create some sort of callback that your other code can use to pipe it over ActionCable. Now if you're interested in talking about sockets work, so those connections like the SSH connection, or just your browser connecting to your rails app or your rails app listening for connections, we can talk about sockets in the guture, and build our own web server and all of that good stuff, so if you're interested in that, let me know in the comments below and we'll dive into that in the future if you guys want. Until then I will talk to you guys in the next episode, Peace
Transcript written by Miguel
Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.