javan / whenever

Cron jobs in Ruby
MIT License
8.83k stars 728 forks source link

Do you have some idea, what is the best way for whenever with multi tenant ? #720

Open bpdarlyn opened 6 years ago

bpdarlyn commented 6 years ago

I am using the gem apartment but I need run a scheduler by every tenant, whenever support this ?

ghost commented 6 years ago

Hi @bpdarlyn i am also using apartment in my application, its simple just create a rake task and get all tenants and perform any operation you want, this is how i have done it..

my_rake.rake

tenants = Tenant.all
tenants.each do |tenant|
      begin
        Apartment::Tenant.switch tenant.name
        #perform any operation
       rescue
       end
end

And in schedule.rb file, just write this..

every :hour do
  rake "my_app:my_rake_task"
end

to check your cron syntax use whenever command in terminal and for the last step update your crontab if you want to test it on local setup.

P.S make sure you set you environment in schedule.rb to development when testing on local set :environment, 'development'

I hope this helps you..