How to reschedule job with ActiveJob every end of month plus X days without crontab?

#1

I a’m trying to rechedule job automatically. For example

  1. On January 1st, an admin activates the monthly auto-wire at +15 days for client John
  2. We schedule a job in sidekiq to run on January 31st + 15 days so the ~ February 15th
  3. On February 15, sidekiq performs the job alone, without cron or intervention on our part, he sends the invoice
  4. After sending the invoice, we re-schedule a job in sidekiq to run on February 31 + 15 days, so March 15
  5. 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?

#2

Time class.

Example:

If Time.now.hour == 00 && Time.now.min ==00 && Time.now.sec == 00
sudo apt-get update.
end

#3

There’s also Time.now.month and Time.now.day.

#4

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.

.bash_alias
Ruby /home/$USER/path/to/script/task_manager.rb &

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 &.