adnjoo / dueltasks

Gamify Your Tasks 👾
https://dueltasks.com/
MIT License
0 stars 0 forks source link

reflect updated deadline #118

Closed adnjoo closed 3 weeks ago

adnjoo commented 4 weeks ago

To ensure that the PenaltyJob reflects the updated deadline for a note, you’ll need to:

  1. Cancel the existing job if it’s already scheduled for the old deadline.
  2. Schedule a new job based on the updated deadline.

Sidekiq doesn’t natively support removing or updating scheduled jobs directly, so you’ll need to manage this by either using the sidekiq-status gem or by setting up job scheduling in your model logic to replace the existing job.

Here’s an approach that updates the scheduled job when the deadline changes:

1. Add a schedule_penalty_job method in your Note model

Define a method in your Note model to handle canceling and rescheduling the job whenever the deadline changes.

class Note < ApplicationRecord
  belongs_to :user

  after_save :schedule_penalty_job, if: :saved_change_to_deadline?

  def schedule_penalty_job
    # Cancel any existing job for this note's penalty
    if self.penalty_job_id
      Sidekiq::ScheduledSet.new.find_job(self.penalty_job_id)&.delete
    end

    # Schedule a new job with the updated deadline
    job = PenaltyJob.set(wait_until: self.deadline).perform_later(self.id)

    # Store the new job ID to cancel or reschedule in the future if needed
    update_column(:penalty_job_id, job.job_id)
  end
end

2. Update the PenaltyJob to accept dynamic deadlines

The PenaltyJob will be scheduled based on the note.deadline dynamically, ensuring that the job only runs once the deadline has passed.

3. Add a penalty_job_id column to the notes table

Add a column to store the job ID so that it can be used to cancel the scheduled job if the deadline changes.

class AddPenaltyJobIdToNotes < ActiveRecord::Migration[7.0]
  def change
    add_column :notes, :penalty_job_id, :string
  end
end

Explanation

This approach ensures that only the latest deadline triggers a penalty job, effectively keeping it in sync with user updates.

adnjoo commented 3 weeks ago

dup of #119