Reputation: 83
In a controller for a rails project I have, there is a helper function that sends an e-mail containing all the changes to the database done in the past week. I also have a cron job on Unix that runs a number of bash scripts every weekend related to my web application. Is there an easy method to run a controller's helper function from a standalone .rb file or bash script?
Upvotes: 1
Views: 509
Reputation: 35350
If you're actually wanting to make the web request from within your crontab, you can just call wget
or curl
on the target URL.
0 * * * * wget http://yourdomain.com/path/to/request -o /dev/null
If you're wanting to execute this mailer outside the context of a web request, this sort of behavior should not be in your controller. This is what lib/
is for.
You can create a file like lib/my_helpers.rb
containing
module MyHelpers
extend self
def your_mailer(some, params, here)
# runthe mailer
end
end
You can include this in whatever controller you want, or even ApplicationController
include ::MyHelpers
Wherever it is included, you can then simply call your_mailer(param1, param2, param3)
to execute it.
You would then include this from within a rake task, and execute the rake task from your crontab. Create a file in lib/tasks/your_mailer.rake
with something like this
include MyHelpers
namespace :myapp do
desc "Runs your_mailer"
task :mailer => :environment do
your_mailer(param1, param2, param3)
end
end
Upvotes: 2