How can I run a Rake task as a Windows "scheduled task"? - ruby-on-rails

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:

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

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.

How to write and execute an individual rake task outside the Rails application folder

How do you write a rake task outside the Rails application directory and make it run.
For example, say write a sample rake task
task :dummy do
puts User.first.first_name
end
this task is under ~/fun/sample.rake
And my rails application is located at ~/my_app/
Now I need to run the sample.rake. I know i need to load the environment, DB etc., etc., how do i do that? Stuck at this for the past hour.
I tried the one below, obviously it did not work because it did not know how to build it.
rake -f ~/my_app/Rakefile dummy
Note: I should not touch the files inside the Rails application but I can write whatever I want inside the fun directory
You need to create a Rakefile and load the rake gem. See this answer: Ruby: Accessing rake task from a gem without Rails

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 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?

Resources