Skip to content

Instantly share code, notes, and snippets.

@eparreno
Last active May 31, 2024 20:18
Show Gist options
  • Save eparreno/9f82d6012e6585a9346b to your computer and use it in GitHub Desktop.
Save eparreno/9f82d6012e6585a9346b to your computer and use it in GitHub Desktop.

Revisions

  1. eparreno revised this gist Oct 29, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sidekiq_delete_jobs.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # How to delete Sidekiq jobs from a Rails app using ActiveJobs
    # How to delete Sidekiq jobs in a Rails app using ActiveJobs

    Sidekiq jobs can be enqueued or scheduled. Enqueued means that they are gonna be picked up as soon as possible,
    scheduled jobs will be enqueued at some specific time.
  2. eparreno revised this gist Oct 29, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions sidekiq_delete_jobs.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # How to delete Sidekiq jobs from a Rails app using ActiveJobs

    Sidekiq jobs can be enqueued or scheduled. Enqueued means that they are gonna be picked up as soon as possible,
    scheduled jobs will be enqueued at some specific time.

  3. eparreno renamed this gist Oct 29, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. eparreno created this gist Oct 29, 2015.
    73 changes: 73 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    Sidekiq jobs can be enqueued or scheduled. Enqueued means that they are gonna be picked up as soon as possible,
    scheduled jobs will be enqueued at some specific time.

    ## job_id and jid
    When using ActiveJobs, Rails will return a `job_id` after sending the job to ActiveJobs

    ```
    job = UserMailer.send_invite(params).deliver_later
    job.job_id => "9fb7b1f3-0159-49dd-b886-dfb8df991cd6"
    ```

    Keep in mind that this is not the id Sidekiq uses to reference jobs internally. To get the Sidekiq `jid`

    ```
    # enqueued jobs
    job = UserMailer.send_invite(params).deliver_later
    Sidekiq::Queue.new("mailers").find { |job_id| job.job_id }.item["jid"]

    # scheduled jobs
    job = UserMailer.send_invite(params).deliver_later(wait: 1.hour)
    Sidekiq::ScheduledSet.new.find { |job_id| job.job_id }.item["jid"]
    ```

    ## Enqueued jobs

    Here's a typical example in a Rails app

    ```
    job = UserMailer.send_invite(params).deliver_later
    ```

    That jobs will go straight to the queue and will get processed as soon as possible.
    Keep in mind that Sidekiq is fast as hell so if there are enough resources that job will be executed with virtually no delay, so it's
    almost impossible to delete a job from the queue unless Sidekiq is busy processing other jobs, that will give you some time to
    delete the job.

    ```
    queue = Sidekiq::Queue.new("mailer")
    queue.each do |job|
    # using jid
    job.delete if job.jid == 'sidekiq_job_id'
    # using job_id
    job.delete if job.job_id == 'rails_job_id'
    end
    ```

    ## Scheduled jobs

    In that case you'll have a bit more time to delete the job ;)

    ```
    job = UserMailer.send_invite(params).deliver_later(wait: 1.hour)
    ```

    ```
    ss = Sidekiq::ScheduledSet.new
    ss.select { |job_id| job.job_id }.each(&:delete)
    ```

    or using the `jid`

    ```
    jid = Sidekiq::ScheduledSet.new.find { |job_id| job.job_id }.item["jid"]
    Sidekiq::ScheduledSet.new.find_job(jid).delete
    ```

    or matching jobs by class instead of by ID

    ```
    ss = Sidekiq::ScheduledSet.new
    jobs = ss.select {|job| job.klass == 'SomeWorker' }
    jobs.each(&:delete)
    ```