Getting Whenever to Work with RVM and Capistrano - ruby-on-rails

I'm deploying an app that uses capistrano/whenever/rvm. Apparently, the whenever/rvm combo has a gotcha, as documented here. The wiki describes three ways of overcoming this. I've tried all of them and to no avail. These solutions include setting rvm_trust_rvmrcs_flag=1 in ~/.rvmrc, configuring schedule.rb to use the proper capistrano route, and using an alternative whenever gem. Again, none of these solutions have worked for me.
Checking crontab -e, whenever is successfully creating the cron jobs. According to the system logs, they're even being executed. However, they're not actually running in Rails. One debugging task reads like this:
every 1.minute do
runner "Company::Category.create(name: 'Shoe Cleaning')"
end
However, no Company::Category is created. Any advice for better debugging this? I've been on it for nearly 7 hours now and I'm struggling to move forward.

Related

How do I get the Rails (javan) whenever gem to only write the crontab on production (and NOT staging) during a Capistrano deploy?

The whenever gem has worked great for me, for years now, in production. My project is now big enough to need a staging environment, and Capistrano has made this effortless. However, the one thing that I cannot sort out is preventing the whenever gem from writing cron entries on the staging server.
I've been searching for an answer for literally weeks, off and on, among doing other things, and I've tried about 6 different hacks from blogs and other SO posts. I've created an issue on GitHub about the one thing that the documentation suggests: "namespacing" the schedule.rb file. Nothing about my interpretation of that statement works. And all the references here on SO seem to be about the gem NOT writing crontab entries. My problem is that it writes too many!
Rather than going down the rabbit hole about that particular issue, I thought maybe I would try asking the question directly. Surely, people are doing this. It seems like an obvious and basic thing to want from such a process, e.g., to tie jobs to various environments. But, like I said, everything I've tried has failed, and I still wind up with only-production-appropriate crontab entries in staging.
What's the cleanest way of implementing environment-specific jobs, that works?
We pass an environment variable to segment which jobs run on which server environment.
env = #environment
if env == 'production'
# a bunch of scheduled tasks
end

Ruby scaffolding link not working

So I believe that I have searched on here and not found any question that pertains to my issue so hopefully someone can guide me. I was required (for a course) to create a Ruby scaffolding "products" page. My page links all worked on the local host rails server but when I tried it on Heroku (also required) all the links work EXCEPT the new product page.
I have exhaustively googled but I must admit that all the computer lingo is new to me so I am unsure what any of it means and of what steps are safe. For the reason that if I make things worse, I will never know what to look for to reverse anything. I have included the error that shows up in Heroku. When I searched that I found many different answers causing me to be unsure which is correct.
If anything else is needed just let me know. Many thanks to any and all that reply!
Error on Heroku
The error says, "relation products" does not exist.
Did you make sure to run heroku run rake db:migrate ? After you migrate you'll need to heroku restart
This gets me a lot as part of my Heroku deployments, so I make sure to write a script or rake task which does the git push to deploy to Heroku, then runs these two commands. Then I always use that script to deploy, instead of manually doing a push.

Is there a Secret_Key solution for both Heroku prod'n & local dev't envs?

I am trying to find the best solution for storing secret keys (specifically S3 and devise keys are the ones i'm trying to set up) which will enable my app to run both locally and on Heroku. My research so far (almost a whole day on this :( ) shows me a number of options, however i've had issues with each, which i outline below:
Option 1) Using the .gitignored rails 4.1 provided secrets.yml file is lovely in development, you simply use Rails.application.secrets._secret_ in the s3 credentials in your model and all is fine. But when pushing to Heroku you need to heroku config:set key=value for each variable, which in itself is fine but then you need to replace the Rails.application.secrets... syntax with ENV['key'] and this renders the rails intended secrets.yml and the corresponding nice syntax unusable. The only way i can see this working is with ENV's set both locally and on heroku, a bit tedious and it wouldn't be utilising the rails intended secrets.yml file.
Option 2) Using the gem 'figaro' is all well and good, so the gem sets up an application.yml file for you and .gitignores it for you and provides a handy rake command to bulk create Heroku ENV's. But i cannot see how you are meant to use the same app version for both local development and heroku production environments without again having to set ENV's on each environment. Ok, figaro makes setting ENV's quicker on heroku through its rake command, but it still renders secrets.yml useless and involves setting of ENV's for every key in each environment.
Option 3) I found another gem; gem 'heroku_secrets', github: 'alexpeattie/heroku_secrets' which is designed to make our lives easier. This gem, akin with figaro, provides a rake commend to bulk set Heroku ENVs. But i guess it still leaves us with the same issue of ENV's set everywhere. However, i couldn't even get this far with this gem, when attempting to use it I ran into this error message;
git://github.com/alexpeattie/heroku_secrets.git (at master) is not yet checked out. Run bundle install first.
Neither bundle nor bundle install do anything here and searching on the issue has me trying bundle install --deployment which rather than identifying and solving the problem, it provides a workaround solution through placing all of my gems in the vendor directory, not how id like thing set up, so i scrapped that change.
Other Options) dotenv-not tried, foreman-not tried, pre loading a file containing vars before rails.initialize, tried but didn't like.
After all of today searching, playing, trying to implement and re-implement variations of the above and more I am still at a loss as to how to get this app to run on both heroku and local dev environments in a decent way. I would of course like to achieve this whilst using the secrets.yml rails intended set up with without too much customisation, but i will do what i need to do to get this working. I am very much a junior at this developer thing so it is taking me longer than most to get my head around things, but this has really stumped me and has my head firmly in my hands asking for some help, please show me the light oh programming genii.
Here are some of the other links that i found, read and with some tried to implement if of any help with this subject:
Heroku's config man pages: https://devcenter.heroku.com/articles/config-vars#setting-up-config-vars-for-a-deployed-application
A closely related question; How do you manage secret keys and heroku with Ruby on Rails 4.1.0beta1?
Article on various solutions; http://www.gotealeaf.com/blog/managing-environment-configuration-variables-in-rails
Another article on various solutions; http://tammersaleh.com/posts/managing-heroku-environment-variables-for-local-development/
Bundle 'not checked out' error; is not checked out... bundle install does NOT fix help!
Yet despite all of this discussion I can't seem to find a way to get this to work in both development local and heroku production environments without manually setting ENVs. Is there a way and if so would you please enlighten me.

Rails execute script

I am building a script in on of my controllers to fill a database with an excel files data. I would build the function, then access it through a route. (That i guess i can protect with cancan) But i thought about it, and it doesn't seem very ... 'Railsy'.
I know the scripts folder exists, and it is probably for these kinds of tasks. I've tried googling stuff like 'rails execute script' and other stuff, but i can't find any good advice for what to do next.
I'm sorry if this seems kind of stupid, but in my apps i've been kind of hacking around stuff to make it work, so any advice on this task would be appreciated.
If you need to upload the file in the app and process it, it should probably go in the "lib"directory and be accessed like any other Ruby library/module/etc.
If it's something you need to run locally, "on demand", "scripts" is fine. If you need access to your rails environment when running it like any Rails models, you can run it from "rails console" or "rails runner".
As Aln said, there are a variety of ways it could be scheduled as well.
You could simply do
#!/usr/bin/env ruby
require 'rubygems'
# regular ruby code here
and have it running just like any other util. Of course you can always call any *.rb with simply
ruby somescript.rb
If you need some scheduled script, check into rufus-scheduler gem.

Old ActionMailer templates being cached?

We're running a Rails 3.0.7 app with Passenger standalone + nginx and with ruby 1.9.2-p290, and we're seeing the strangest thing happen: After deploying some updates to a mailer template, new mails coming from the app sometimes have the updates applied, and sometimes the old template is being used.
I can't find anything about cached ActiomMailer templates online, and running rake tmp:cache:clear does not address the issue either, fwiw.
Has anyone ever experienced such a thing? Is there a definitive solution for this problem?
thanks,
Jacob
I've run into similar situations in our application, and we use Resque.
If I'm changing anything regarding mailers (which the Resque workers handle), I need to restart the workers.
Kill the workers and respawn them, it sounds like some of them might have the old environment loaded and others do not.
I was having this issue as well, although I wasn't using Resque to send mail. After staring at the offending templates for a while, I realized some of my inline CSS was incorrectly formatted (improper font-family and padding declarations) and that the template may have been ignoring everything else next to those declarations. As a result, it looked like it was pulling the old templates rather than rendering the new rules. Not sure if this is documented anywhere..
So, basically, I was lazy and it cost me a few hours of the day.

Resources