Trouble with crontab and using whenever gem - ruby-on-rails

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

Related

Run capistrano tasks locally

In my deploy.rb file I have a lot of tasks, one of which restarts the unicorn and some services. For example:
namespace :deploy do
task :restart do
invoke 'unicorn:restart' # using capistrano3-unicorn gem
invoke 'some_service:restart'
...
end
end
Now I need to run the same task locally through console on production server. I don't want to duplicate my code creating the same task as a rake task. I want to run this task, which is already exists and defined in the deploy.rb file, but I want to do it locally, not remotely. Is it possible ?
If I understand correctly, you have your Capistrano tasks which you run from your dev box as part of your deployment, and they execute on the server. You now want to run a specific command on the server, and you are logged into the server.
I'm not sure how to do it that way, but I'd suggest an alternative which might get you what you want. If you, on your dev box, run a specific task such as bundle exec cap production unicorn:restart, it will execute just that task on the server.

Scheduling rake task with cron

I'm trying to set daily cron job to update my site stats, but it looks like it doesn't work.
Cron entry (for deployer user):
0 0 * * * cd /var/www/my_site/current && rake RAILS_ENV=production stats:update
I'm running ubuntu server, with rbenv.
Any idea what's wrong?
Many times $PATH is defined differently when cron runs compared to when you are working in your own shell. Do "whereis rake" to find the full path to rake and then replace "rake" with its full path. (I am assuming that the "cd" command is working, so I am focusing on whether "rake" is found / running properly.)
Has cron sent you any emails with error messages after you added your command to your crontab?
You might want to run "crontab -l" under the proper user account to make sure that your cron command is actually registered within the crontab, especially if you aren't receiving any emails.
The presence of a Gemfile can also affect the ability to properly run rake. See, for example, Error: "Could not find rake", yet Rake is installed

How to run a task rake on ruby on rails with a crontab ? (which works manually, without crontab)

i deployed (with capistrano) a ruby on rails project on an aws micro server.
I'm on ruby 1.9.2-290 and rails 3.2.6 and i also use bundler.
I developed a task rake in my opt/rails-project/lib/tasks/tasks.rake
namespace :myclass do
task "my-task" => :environment do
# do the stuff which work nicely if i enter my command line manually
end
end
This is how i call it in my crontab :
*/3 * * * * cd /opt/rails-project/current && /opt/rails-project/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/bin/rake myclass:my-task RAILS_ENV=production >> ~/logs-my-task.txt
The file ~/logs-my-task.txt is created and updated every 3min as it does. This file only contains info of the version release from capistrano but nothing from my task rake.
As i said in my comment in my task rake, if i launch this command directly in the server via ssh, my task rake does its job...
I searched the web all day/night long and can not figure it out.
I tried to remove the http_basic auth from rails but same problem.
Hope you have a idea,
Thanks for help !
Try to put this part
cd /opt/rails-project/current && /opt/rails-project/shared/bundle/ruby/1.9.1/gems/rake-0.9.2.2/bin/rake myclass:my-task RAILS_ENV=production >> ~/logs-my-task.txt
inside some file, somescript.sh, give execution permissions:
chmod +x somescript.sh
and try to run it manually:
/path/to/somescript.sh
If it works, try to put it into crontab:
*/3 * * * * /path/to/somescript.sh
It often helps to put complex stuff inside script to run in from crontab.
Next step, ensure that you PATH environment variable the same for your shell and for cron. You can set it inside crontab or inside your script.
After I used a shell script as recommended by denis.peplin and launched it manually, I got the problem described here: Ruby on Rails and Rake problems: uninitialized constant Rake::DSL.
I included the following line in my Rakefile and let my crontab as it was before:
require 'rake/dsl_definition'

How to execute a command on the server with Capistrano?

I have a very simple task called update_feeds:
desc "Update feeds"
task :update_feeds do
run "cd #{release_path}"
run "script/console production"
run "FeedEntry.update_all"
end
Whenever I try to run this task, I get the following message:
[out :: mysite.com] sh: script/console: No such file or directory
I figured it's because I am not in the right directory, but trying
run "cd ~/user/mysite.com/current"
instead of
run "cd #{release_path}"
Also fails. Running the exact same commands manually (through ssh) works perfectly.
Why can't capistrano properly cd (change directory) into the site directory to run the command?
Thanks!
Update: Picked an answer, and thank you so much to all who replied.
The best answer may actually be the one on server fault, though the gist of both (the one on server fault and the one on stack overflow) is the same.
You want to use script/runner. It starts an instance of the app to execute the method you want to call. Its slow though as it has to load all of your rails app.
~/user/mysite.com/current/script/runner -e production FeedEntry.update_all 2>&1
You can run that from the capistrano task.
I cannot imagine that you would be able to remotely log into rails console from capistrano. I suggest you call your model method from a rake task.
How do I run a rake task from Capistrano?
As for the latter part of your question, are you logging into the server with the same user account as capistrano?

How can I run a Rake task as a Windows "scheduled task"?

I have a rake task I need to run as a daily job on a Windows XP machine. I discovered the Windows "scheduled tasks" control panel. I'm trying to figure out how to get it to run my Rake task.
Two of the fields are "run" (with a browse button) and "start in". I tried to enter rake mycategory:mytask into "run" and my Rails project dir containing the Rake task into "start in". The result was a message saying the task "could not start".
How can I get set up a Windows "scheduled task" to run a Rake task?
If you can build a batch file that can execute it properly I would do so, and then you can direct the batch file to run with the task.
Something like this should work:
:: Run task every hour
#call rake stats RAILS_ENV="production"
Also, aside from the (correct) batch file advice above, AFAIK, you may need to run the task on an account that has a non-empty password set. Property of Scheduler.
Just adding an updated/fleshed out answer for those interested...
Create a file called rake.bat - make sure to save with ANSI encoding (the default with Windows Notepad). You can save this file anywhere, but I put it in C:\ror\rake.bat
rake.bat
#call bundle exec rake %*
Now when you create a scheduled task, you can set it to run the .bat file and the arguments are simply what comes after rake. Set it to run inside the directory of your choice. Run whether or not the user is logged in, but do NOT run with the highest privileges. Screenshots below for clarity; my rake task is namespaced manager, and the task itself is sync:

Resources