Multiple schedule.rb files with whenever gem - ruby

Is it possible to have multiple schedule.rb files when using the whenever gem in rails to setup cron jobs? I am looking to have a regular schedule.rb file and also a reports_schedule.rb file that is going to be deployed to a different server and it has its own specific reports environment.
How does whenever use the schedule.rb file? Is this possible?

It looks like it is possible if a bit ugly. Looking at the source code on job_list.rb:25 whenever just does an instance eval. So you can do something like the following.
schedule.rb
#Load reporting schedules
instance_eval(File.read('reporting_schedule.rb'), 'reporting_schedule.rb')
# All your regular jobs
# ...
reporting_schedule.rb
#Need some way to know if you are on the reporting server
if `hostname` =~ /reporting_server/
# All your reporting jobs
# ...
end
Worked for me doing some quick tests with the whenever command. Have not tried using it in a deploy though.

Not sure whether this was added after the question was asked, but you seem to be able to do role-based scheduling now: See README at https://github.com/javan/whenever

Related

Can't save a temporary csv file to /home/app directory on heroku using R shiny app [duplicate]

I published my first simple app on Heroku with a free dyno. This app writes a simple .txt file, that seems to be correctly written because my API services are working fine.
But if I try to check this file by entering in the file system using "heroku run bash -a MYAPP", I can't see that file in the folder I thought to see. It is like the file is not existing. Can someone tell me why?
Thanks.
I found this on https://devcenter.heroku.com/articles/active-storage-on-heroku:
In addition, any files stored on disk will not be visible from one-off dynos such as a heroku run bash instance or a scheduler task because these commands use new dynos.
It is still not so clear to me, but at least I know it is a normal (but strange) behaviour of Heroku!

How to get stand-alone ohai to recognize custom plugin_path?

I have chef configured to add "/etc/chef/ohai_plugins" to Ohai::Config[:plugin_path]. However, the Chef documentation says:
"The Ohai executable ignores settings in the client.rb file when Ohai is run independently of the chef-client."
So, how can I get a stand-alone run of ohai to load and use the plugins in that custom path?
(Background: I have a custom plugin that reports some information that we keep track of for a fleet of servers, like whether a server has been patched for heartbleed or shellshock. I want to be able to run "ssh somehost ohai", parse the JSON that gets sent back, and extract the information I need.)
Thanks.
Outside of chef, you can add an additional plugin path using the -d switch, e.g.
$ ohai -d /etc/chef/ohai_plugins
The relevant source code is at:
https://github.com/chef/ohai/blob/master/lib/ohai/application.rb#L25-L28
https://github.com/chef/ohai/blob/master/lib/ohai/application.rb#L78-L80
The option to specify a custom config file for Ohai was sadly removed last year with https://github.com/chef/ohai/commit/ebabd088673cf3e36d600bd96aeba004077842f1
Hope this answers your question.
This will be possible soon via the implementation of Chef RFC 53: https://github.com/chef/chef-rfc/blob/master/rfc053-ohai-config.md

How can I get Heroku's database information in a ruby script?

I created a rails app that is hosted on Heroku. Now I want to upload a ruby script that will populate my database.
This script already exists and is using
PGconn.connect( :hostaddr=>"127.0.0.1", :port=>5432, :dbname=>"myDB", :user=>"MyUser", :password=>'MyPass');
This will obviously not work on Heroku.
I tried to get it using:
Rails.configuration.database_configuration
But I get
uninitialized constant Rails
How can I get the information so I can connect to my Heroku DB? Is there any way to make the same code wotk both on my localhost and on Heroku?
(ps: I am using foreman and the .ENV file, if it helps)
Edit:
The solution I took:
require 'yaml'
require 'erb'
config = YAML.load(ERB.new(File.read(File.join("config","database.yml"))).result)
In config I have all the information I need to perform the DB access.
Why would you not just access ENV['DATABASE_URL'] and break it up into the constituent parts? To see how this is done, if you do heroku run bash and do cat config\database.yml you will see how Heroku does this itself.
To ensure that locally it works as well if you execute via foreman run <Scriptname> then it will be executed and the contents of your .env will be loaded.

What's the simplest way to run a cron job on a standalone Ruby gem?

I have a gem that packages one .rb file containing my class and associated methods as well as a corresponding .bin file.
Locally, I can run everything just fine like so:
command_to_bin input_file output_file
I don't want to run this manually every day so I'm considering using cron on a server, but I'm a little unsure how to proceed.
Do I throw everything into a directory (.gem file, input file, output file) and just point the above cron command at the directory?
I've looked at this and sort of understand what's going on. I guess what confuses me the most is that when I look at all the web hosting providers, they mention domains and applications, but I just want to know how to have the standalone script run by itself without it being built into a web application or associated with a domain.
Check out the whenever gem. It's a wonderful gem to abstract all the nastiness of cron. Just include the command as you have written above and it should be fine.
You don't need to install Rails. After you wheneverize . the directory and set the schedule in your schedule.rb file, you need to run whenever --update-crontab to set everything to the system. Otherwise your cron jobs never get converted to Unix Cron

Adding ruby code to script/rails that only executes in rails development environment

I am using Rails 3 and have a need to run WEBrick with SSL support during development. To achieve this, I've followed this guide:
http://www.nearinfinity.com/blogs/chris_rohr/configuring_webrick_to_use_ssl.html
This works well, however, I want to ensure that these settings do no affect my rails application when run in production mode. We are currently using Apache/Passenger, and the project appears to still run fine. Is there a clean way, however, to make sure that this code isn't even executed? I'm thinking a possible answer could be an if/end block around the code, or perhaps a built-in rails facility that allows development-only code to be placed in a separate file or something similar.
Looks like ENV['RAILS_ENV'] is your friend. The ENV hash shows you the Unix environment the app is being run under and Rails itself will look at RAILS_ENV to decide in which mode to run. You could do something like this:
if ENV['RAILS_ENV'].to_s == 'development' || ENV['RAILS_ENV'].to_s == ''
# do your thing here
end
You can also make sure that you run webrick with that environment:
#> RAILS_ENV=development /path/to/webrick/script
Hope it helps.

Resources