Contract Work

Friday, February 26, 2016

Rails Runner to the Rescue

Yes! I finally have something good and technical that I can blog about!!

This week, I was working on one of our smaller applications. At GA, we send out surveys to students in order to get feedback on courses and programs and I was looking to add a new survey template. After becoming familiar with the codebase, I discovered that new surveys have previously been added by creating a bin file and then running that script since new surveys require tons of validations and therefore it is too complex to just add a survey via the console. And so that’s what I set off to do. Now, the actual creation of the survey wasn’t so difficult. The app is very modular and pretty clean and easy to understand. I finished up that task (as well as updated the readme to find this sort of information a bit easier) and then I said, ok great, well, now I have to deploy it to our staging environment. And so I sat for a moment and thought, well, how do I run a bin script only in staging. Enter rails runner. Rails Runner is an easy way to execute 1 file in an environment of your choosing. You can find a tiny bit more about rails runner in the rails docs here.

So based on this post I ran what I thought was the correct command…

heroku run bundle exec rails runner ./bin/file_name -r staging

and I got an error

/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/commands/runner.rb:62:in `eval': /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/commands/runner.rb:62: syntax error, unexpected '.' (SyntaxError)
./bin/file-name


WHAT?!

okay well, maybe I don’t need that ‘.’ in front of /bin and that is the issue. and so I ran

heroku run bundle exec rails runner bin/file_name -r staging

and got another error

/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/commands/runner.rb:62:in `': undefined local variable or method `bin' for main:Object (NameError)

well, now I was confused. I obviously needed the ‘.’ there and the first error was pointing to a syntax error… was there a typo in my code? I looked (and had a colleague look as well) and couldn’t find anything. I searched and searched (there really isn’t much about rails runner, which is why I decided to write this blog post!). okay, a little while passed and so I said to myself “it’s time to pair!”

I grabbed a colleague who suggested to see if we could even just get into the shell and then run the command from there, so I entered heroku run bash. Success! I’m in. Then we just did an ‘ls’ to make sure we were in the right place. Yup, right place. So then we did ls bin and the file wasn’t in the bin folder! Here’s where I had my moment… I thought that I needed to run the bin scripts BEFORE deploying the PR to staging so that the files would exist and the surveys would appear in our staging environment. HOWEVER, rails runner was looking into the staging code base in order to find the files but of course, they weren’t there because I hadn’t merged the branch in.

Merged the branch in, and then ran

heroku run bundle exec rails runner ./bin/file_name -r staging

and the problem was solved. Just a simple order of operations issue.

Monday, February 1, 2016

On what the heck to blog about


A few months ago, I made a commitment to start blogging again at least once per month. I keep this reminder on my to-do list and try to keep a running list of topics that I can write on. This WAS great when I had a lot of hours to put into extra learning, extra reading, and all those other great things you do when you’ve got more time. I blogged about side projects, issues I faced, interesting blog posts I’d read that week and a whole bunch of other things that I thought were pretty interesting.

Now that I have a small kiddo who periodically requires some attention (and by periodically I mean I looked away for like 5 seconds the other day to finish something and he totally face-planted), even more of my learning is done on the job. What I do at work is really interesting. The app is fairly complicated and so I new and different things every week. From feature builds to bug fixes to investigations, every day is varied. However, with all this learning i’m doing at work, and all the notes I take in my notebook so I can study and remember everything in the future, it’s hard to blog about these things. Why? Because there’s so much contextual knowledge around the problem and the solution. Aside from the fact that a lot of things are proprietary, you need to know a lot about the application, data structure, and history of development in order to understand why we need to solve some of the problems that we do. Furthermore, oftentimes what I am doing is a small piece of a larger puzzle since my tasks are often more digestible than whole large-scale tasks and to blog about this small piece would require a significant understanding of the larger component I’m working on. Finally, I still find it difficult to figure out which solutions are only applicable to that specific problem I’m solving and which are applicable to many different situations.


So, what do you blog about when you’re not working on a side project? How do you come up with ideas? I wish I could blog more often but I’m currently feeling like it just takes so long to figure out what to talk about!