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.
comments powered by Disqus