Rails when a stone executing a command only if a certain condition is met.

We use whenever a gem with rails in our project. I understand that we can plan a team using whenever this happens.

every 1.day, :at => '6:00 am' do command "echo 'hello'" end 

but my problem is that I want to execute this command only if some condition is met. something like that.

 every 1.day, :at => '6:00 am' do if User.any_new_user? command "echo 'hello'" end end 

how can we achieve this every time and with rails? One of the possible solutions that I can think of is that I create a runner for this and check this condition. Sort of:

 every 1.day, :at => '6:00 am' do runner "User.say_conditional_hello" end 

and inside my user model:

 def self.say_conditional_hello `echo "Hello"` end 

Any suggestions for this approach or any new approach will be really helpful.

Thanks in advance!

+4
source share
1 answer

if you want to only schedule the task, if the condition is true, then I don’t think it is possible, you can schedule the task when it is time to start the task, the code can decide whether it should be run or not, depending on your conditions.

One of the possible solutions that I can think of is that I create a runner for this and check this condition. Sort of:

Yes, this is one way to handle this, however IMO - the best behavior when using it ever creates a rake task that checks the conditions, then executes your code or does your work.

Sort of:

Rake challenge

 namespace :user do desc "description" task check_for_new: :environment do if User.any_new_user? # code end end end 

in your schedule.rb file

 every 1.day, :at => '6:00 am' do rake "user:check_for_new" end 
+2
source

Source: https://habr.com/ru/post/1497957/


All Articles