I a’m trying to rechedule job automatically. For example
On January 1st, an admin activates the monthly auto-wire at +15 days for client John
We schedule a job in sidekiq to run on January 31st + 15 days so the ~ February 15th
On February 15, sidekiq performs the job alone, without cron or intervention on our part, he sends the invoice
After sending the invoice, we re-schedule a job in sidekiq to run on February 31 + 15 days, so March 15
and so on
class ClientProfile
include Mongoid::Document
def set_auto_wire_sending_date
AutoWireJob.set(wait_until: (today.end_of_month + 15.days).to_time).perform_later(self.user.id.to_s)
end
def today
Time.now.utc.to_date
end
end
class AutoWireJob < ActiveJob::Base
queue_as :autowire
after_perform :reschedule_job
def perform(user_id)
user = User.find(user_id)
# send the invoice
::PaymentJobs::ExecuteAutoWire.perform_later(user)
end
def reschedule_job
user = User.find(self.arguments.first)
client = user.client_profile
self.class.set(wait_until: ).perform_later(user.id.to_s)
end
def today
Time.now.utc.to_date
end
end
I expect to have recurring job but it executes only one time. What is wrong?
I did this on the go. Now that I have some time to explain it.
With the Time methods you can automate practically anything. I never liked Cron and never use it.
task_manager.rb
def auto_update
If Time.now.day == 31 && Time.now.hour == 23 && Time.now.min == 00 ;
Kernal.system “sudo apt-get update”
end
end
auto_update
By creating a .rb file with all of your tasks like so, or even individual files for each task, you can start them automatically two ways.
.rc_local (this starts all scripts at boot level, or when the bootloader starts the computer)
.bash_rc (this starts all scripts at the user login level. Meaning the files won’t start until that user logs in)
Using .bash_rc is a easier route and can be found in the /home/$USER Folder. I recommend this for newer users. Also you don’t have to edit the .bash_rc file directly. You can create a new file in your user folder as .bash_elias to load all your scripts at.
The & at the end is very important. It throws the task manager.rb into a big process. The same line would be added to the .rc_local file including the &.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.