The 'command' method is being ignored by the whenever scheduling gem - ruby-on-rails

I am using the whenever gem to schedule tasks on OSX Mojave. In the scheduling file, I have
#config/schedule.rb
set :output, "log/cron.log"
every 1.day, at: '23:00' do
rake 'util:something'
runner 'User.something_else'
command "echo 'I just want to see something happen'"
end
The rake, and runner methods work fine and I see output in the cron.log. However, there is no output from the command method, it just seems to be ignored. It may be something to do with user permissions, but I am running it from the base directory of my ruby on rails app, so I would have thought I would have the correct permissions.
How do I fix this?

For the 'command' method, it is necessary to specify the full path to the log files, so the output should something like:
set :output, 'user/sites/my_app/log/cron.log'

Related

Trouble with crontab and using whenever gem

Hi I am using the whenever gem and trying to send a daily email. For testing I set it to 2 minutes, and I have it in my schedule.rb file. It calls a task I have in a rake file. When I run bundle exec rake task_to_be_called, it runs and works. But the actual scheduling does not work. When I try to run things to find out crontab it says no such file or directory. Is there some way to get a crontab file, or do I make it? How do I test or get my scheduler to run that task?
EDIT: Thanks for the advice on sharing code and error.
In my lib/tasks/daily_email.rake I have
desc 'Daily email rake task test'
task daily_email_call: :environment do
ReportMailer.with(email: "email#email.com").daily_summary_report.deliver_now
end
Then in my config/schedule.rb I have
every 2.minute do
rake 'daily_email_call'
end
When I run bundle exec rake daily_email_call it functions correctly and does the send email task. My question is how to get it to do it on the schedule. I have no crontab file. Am I even able to do this locally or would it need to be on a running server. I am using windows not Linux when I run mine locally.
There is a typo in the file name,
lib/tasks/darily_email.rake => lib/tasks/daily_email.rake
I guess this is causing the error, no such file or directory

Rails Whenever gem and cron job not working on development environment

so this is like my first time doing a cron job, and also using a whenever gem.
this is what's in my schedule.rb
set :environment, :development
every 1.minute do
command "rails runner /home/ec2-user/projectA/scripts/download.rb"
end
It's running a script where it download's a file and does a few more other thing.
However, for some reason , the job is not running ? nothing is happening, the results are not returning. why is that?
UPDATE#############
i checked my cron log file, and it seems that it was running the script every minute as intended. however my results of the script is not producing as i would to run it manually.. why is that?

Running Rails Task From Cron

I have a Rails runner task that I want to run from cron, but of course cron runs as root and so the environment is set up improperly to get RVM to work properly. I've tried a number of things and none have worked thus far. The crontab entry is:
* 0 * * * root cd /home/deploy/rails_apps/supercharger/current/ && /usr/local/rvm/wrappers/ruby-1.9.3-p484/ruby bundle exec rails runner -e production "Charger.start"
Apologies for the super long command line. Anyhow, the error I'm getting from this is:
ruby: No such file or directory -- bundle (LoadError)
So ruby is being found in the RVM directory, but again, the environment is wrong.
I tried rvm alias delete [alias_name] and it seemed to do something, but darn if I know where the wrapper it generated went. I looked in /usr/local/rvm/wrappers and didn't see one with the name I had specified.
This seems like a common problem -- common enough that the whenever gem exists. The runner command I'm using is so simple, it seemed like a slam dunk to just put this entry in the crontab and go, but not so much...
Any help with this is appreciated.
It sounds like you could use a third-party tool to tether your Rails app to cron: Whenever. You already know about it, but it seems you never tried it. This gem includes a simple DSL that could be applied in your case like:
every :day # Or specify another period, or something else, see README
runner "Charger.start"
end
Once you've defined your schedule, you'll need to write it into crontab with whenever command line utility. See README file and whenever --help for details.
It should not cause any performance impact at runtime since all it does is conversion into crontab format upon deployment or explicit command. It's not needed, once the server is running, everything is done by cron after that.
If you don't want an extra gem anyway, you might as well check what command does it issue for executing your task. Still, an automated way of adding a cron task is easier to maintain and to deploy. Sure, just tossing a line into the crontab is easier — just for you and just this once. Then it starts to get repetitive and tiring, not to mention confusion for other potential developers who will have to set up something similar on their own machines.
You can run cron as different user than root. Even in your example the task begins with
* 0 * * * root cd
root is the user that runs the command. You can edit it with crontab -e -u username.
If you insist on running cron task as root or running as other user does not work for some reason, you can switch user with su. For example:
su - username -c "bundle exec rails runner -e production "Charger.start"

Delayed_job and Prawn scripts

How do I run Prawn scripts with Delayed_job.
(Currently using Bj but not supported in Rails3)
This code does not work.
/lib/report_job.rb
class ReportJob < Struct.new(:prawn_script_name , :account_id )
def perform
bundle exec rails runner "#{Rails.root}/jobs/#{prawn_script_name}.rb #{#current_user.account_id} "
end
/reports_controller.rb
def generate_report(prawn_script_name)
Delayed::Job.enqueue(ReportJob.new("#{prawn_script_name}.rb","#{#current_user.account_id}"))
end
delayed_job table is populated as expected.
--- !ruby/struct:ReportJob
prawn_script_name: statements.rb
account_id: '18'
Error in last_error field.
{undefined method `runner' for ReportJob:0xc28f080
Any suggestions?
I think there are several misunderstandings here:
you meant to call runner from outside your app, e.g., in a shell script or command line. in other words, bundle exec rails runner are all commands and arguments of commands, not ruby methods or variables. runner is the first expression that is eval'd inside your perform method, hence your error.
rails runner just brings up your apps environment and evals the string or path argument given.
note account_id within the perform task, another mistake in your code I guess.
What you wanted to do could be a simple system call.
It seems your prawn script needs the environment, so simply calling
system "ruby #{Rails.root}/jobs/#{prawn_script_name}.rb #{account_id}"
won't work.
Now you could surely execute the script with runner from your project directory.
system "bundle exec rails runner #{Rails.root}/jobs/#{prawn_script_name}.rb #{account_id}"
but doing this via a system call within your environment is quite redundant. Delayed jobs already have access to your rails environment. so just simply load them.
class ReportJob < Struct.new(:prawn_script_name , :account_id )
def perform
load "#{Rails.root}/jobs/#{prawn_script_name}.rb"
end
end
hope this helps

Clearing crontab changed format of terminal cron messaging

I'm using the latest version of the whenever gem with Rails 3.1.1 for cron tasks. After I used the whenever command on the terminal
whenever -c
to clear the crontab, whenever I type in
crontab -l
whereas it used to say something like "there are no cron tasks" (this is not verbatim) now it just displays a blank space about the size of two empty lines. Also if I have a cron task setup and I type the same command into the terminal again, those two empty lines come up before it shows the cron tasks. I'm sorry if this is a minor issue. Everything appears to be working fine but I just want to make sure I didn't screw anything up that'll come back to haunt me somewhere down the line. Thanks!
You need to change the task in the schedule.rb file which is generated by whenever gem.
After changing your cron task in the schedule.rb you have to update your crontab file and you can use this command to do that :-
whenever --update-crontab f(here f is your application name)
crontab -l is used to see your current crontab file.
Hope it helps

Resources