Using Whenever with RedMine - ruby-on-rails

I am trying to set up a cron task for a model in a redmine plugin. The Whenever gem looked like the best option. Alas, it does not seem to do anything.
I have set up the gem, both in the config folder of the plugin and the primary one. I added it to the crontab (using the whenever -i command) have reset the server.
The action was not run, and no log was generated
here is the schedule.rb file.
set :output, "/path/to/my/cron_log.log"
every 1.day, :at => '9:00 am' do
runner "pluginMailMethod.send_emails"
end
every :thursday, :at => '4:20 pm' do
runner "pluginMailMethod.send_emails"
end

Related

The 'command' method is being ignored by the whenever scheduling gem

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'

A cron job that creates objects - rails

I am working on an event app, that showcases the events near my area.
A Boolean is_weekly_event is set for each entry.
If it is set to true I need to show that event in my app every week on the same data and time.
Therefore, I need to write a cron job, that re-creates this event every time it passes it's end_time, for the same time next week!
Thanks in advance!
you can use whenever gem to add cron job. I have used this in one of my projects and works very fine.
You can define rake tasks in lib/ and in your schedule.rb file which will be generated when you will do whenervize your project.
every 3.hours do # 1.minute 1.day 1.week 1.month 1.year is also supported
# your task here
end
every 1.day, :at => '4:30 am' do
# your task here
end
After finishing all you need is to update your crontab file for your machine using this command.
$ whenever --update-crontab
Note - just a take care to load proper environment(dev, prod) for the rake tasks, it took me hard to find out when i was trying to run the cron.
Thanks

How to put filename in cron job (rails)?

I am using whenever gem. I am calling runner to run job. I called runner for the same job by 3 type. I am not sure which one working here.
I used command whenever --update-crontab project_name
then crontab -l
Schedule.rb
set :output, 'log/whenever.log'
every 1.days , :at => '03:51 pm' do
runner "SomeJob.perform_later", filename: '/app/jobs/some_job.rb'
end
every 1.days , :at => '03:51 pm' do
runner "SomeJob.perform_later", filename: './app/jobs/some_job.rb'
end
every 1.days , :at => '03:51 pm' do
runner "SomeJob.perform_later"
end
Also these ran only once. I am having difficult time to debug here.
Can anyone tell which one is correct? Also what is the correct way to debug in this scenario?
Running crontab -l gives me this -
51,51,51 16 * * * /bin/bash -l -c 'cd /home/rahul/orthoweb && bin/rails runner -e production '\''InvestigationStopJob.perform_later'\'' >> log/whenever.log 2>&1
To run in development environment I ran this command -
whenever --update-crontab --set environment='development'
But this gives me this message -
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
There are some issues with running the jobs in development mode.
What works is that you have to close all tabs before running your job and scheduler.
There is no need to change setup of your env in schedule file.
To run in development use this
whenever --update-crontab --set environment='development'
Without specifying filename jobs does not run properly. The following line works correctly.
every 1.days , :at => '03:51 pm' do
runner "SomeJob.perform_later", filename: '/app/jobs/some_job.rb'
end
I think, that the resulting crontab contains only one of those tasks, because they were overwritten.
To be sure, check out the resulting crontab by typing whenever. I think that there will be only one entrance.

How to schedule whenever task based on a table column?

I am using whenever gem to run rake tasks. My code in schedule.rb is as follow.
every 1.day, :at => '11:00 am' do
rake "notifications:run_mailer"
end
Above code is executing the rake task on 11am of every day. Now i want to change this. I want to run this rake task based on a table column. For that i have created a table called scheduler.rb and a column run_at. based on run_at column date and time i need to run that rake task. How to do that?
Step 1:
First we have to load all the files in the schedule.rb Then only we can execute the ActiveRecord queries.
That could be done by the below command
require File.expand_path(File.dirname(__FILE__) + "/environment")
Step 2:
Execute the ActiveRecord query and get the time.
run_at=Scheduler.last.run_at
time=[run_at.hour.to_s,run_at.min.to_s].join(":")
every 1.day, :at => time do
rake "notifications:run_mailer"
end
step 3:
We have to update the whenever crontab using below command.
system "whenever --update-crontab"
system keyword is used to run linux terminal commands inside rails code.
Assuming the table name is Scheduler:
Scheduler.find(:all).each { |scheduler|
every 1.day, :at => scheduler.run_at do
rake scheduler.task_to_run
end
}
Please note, that this code will add cron tasks on every run. This will lead to enormously huge amount of cronjobs after a while. The additional check for “was the task already scheduled” must be added as well.
Hope it helps.

Run cron jobs on rails (deployed over several servers)

What is the best way to run cron jobs on rails, when different machines have different jobs to do?
For example, server 1 runs cron job A, while server 2 runs cron job B
Is there a way to deploy the cron files along when we do a regular cap deploy?
take a look at the whenever gem, http://github.com/javan/whenever
It is great for automating cron tasks with rails with a clear DSL. We have been using it production for several months now and it just works and is very lightweight. Some examples from their README:
every 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
every 1.day, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end
every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
runner "SomeModel.ladeeda"
end
every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
runner "Task.do_something_great"
end
The README is very thorough, but there is also a good screencast on railscasts: http://railscasts.com/episodes/164-cron-in-ruby
It easily integrates with capistrano with the following code (copied from README):
after "deploy:symlink", "deploy:update_crontab"
namespace :deploy do
desc "Update the crontab file"
task :update_crontab, :roles => :db do
run "cd #{release_path} && whenever --update-crontab #{application}"
end
end
As far as machine specific, you could use a local config file or even symlink the config/schedule.rb file on deploy. I think I would include a local file that would be symlinked on deploy local_schedule.rb and then put this at the top of the config/schedule.rb
if File.exists?(File.dirname(__FILE__) + '/config/local_schedule.rb')
require File.dirname(__FILE__) + '/local_schedule.rb'
end
Your schedule would run but then include anything local, just make sure it is symlinked before the cap task above is run and you should be good to go.
I hope this helps!

Resources