bullet-train-pro / bullet_train-action_models

Other
5 stars 1 forks source link

Scheduled action models run immediately when scheduled in the past on create but not when updated #81

Open BuddyLReno opened 1 year ago

BuddyLReno commented 1 year ago

If a model includes Actions::SupportsScheduling, you can give it a scheduled_for date in the past and the action model will perform the action immediately when creating the record.

However, if you give it a date in the future and save it, updating the model with a date in the past will not run the scheduled action immediately. I assume it gets "scheduled" for the past date which means it never runs.

Updating should operate the same way as creating.

BuddyLReno commented 1 year ago

Some notes from debugging:

Actions::Base has an included do block that contains after_commit :dispatch, on [:create]. Actions::SupportsScheduling's dispatch function looks like the following

def dispatch
    if scheduled_for.present?
      self.sidekiq_jid = Actions::ScheduledActionWorker.perform_at(scheduled_for, self.class.name, id)
      save
    else
      super
    end
  end

My assumption is that on create, the Actions::ScheduledActionWorker is sending the scheduled_for date to sidekiq, and sidekiq sees that date occurs in the past so it runs it immediately. This happens because of the after_commit hook in base.rb This hook doesn't occur when the model gets updated with a new scheduled_for date.

However Actions::Legacy::Base doesn't seem to have this issue despite looking quite similar in functionality.