Skip to content

Instantly share code, notes, and snippets.

@shekibobo
Last active February 24, 2016 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shekibobo/a89330109d960b168c50 to your computer and use it in GitHub Desktop.
Save shekibobo/a89330109d960b168c50 to your computer and use it in GitHub Desktop.
Sidekiq+Redis JobManager to allow querying and deleting jobs
class JobManager
attr_reader :queue_adapter
def initialize(queue_adapter = Sidekiq)
@queue_adapter = queue_adapter
end
def jobs(queue: nil)
scheduled = queue_adapter.redis { |c| c.zrange("schedule", 0, -1) }
.map { |j| Job.new(j) }
scheduled.select! { |j| j.queue_name == queue } if queue
scheduled
end
def job(job_id, queue: nil)
jobs(queue: queue).find { |j| j.job_id == job_id }
end
def delete_job(job_id, queue: nil)
j = job(job_id, queue: queue)
j if queue_adapter.redis { |c| c.zrem("schedule", j.json) }
end
def delete_queue(queue_name)
jobs(queue: queue_name).map { |j| delete_job(j.job_id) }
end
class Job
attr_reader :json, :hash, :job_id, :jid, :queue_name, :queue_adapter
def initialize(json)
@json = json
@hash = JSON.parse(json).with_indifferent_access
@args = @hash.fetch(:args).first
@queue_name = @args.fetch(:queue_name)
@job_id = @args.fetch(:job_id)
@jid = @hash.fetch(:jid)
end
end
end
require "rails_helper"
RSpec.describe JobManager, type: :model do
before do
ActionMailer::Base.deliveries = []
Sidekiq.redis(&:flushdb)
end
after do
ActionMailer::Base.deliveries = []
Sidekiq.redis(&:flushdb)
end
let!(:user) { create(:user) }
let(:mail) { UserMailer.welcome(user) }
let(:job_manager) { JobManager.new }
let(:queue_name) { "user_mailer:welcome:#{user.id}:#{user.updated_at.to_i}" }
describe '#jobs' do
let!(:jobs) { Array.new(2) { mail.deliver_later queue: queue_name, wait: 10.minutes } }
let!(:other_jobs) { Array.new(2) { |i| mail.deliver_later queue: "#{queue_name}--#{i}", wait: 10.minutes } }
it "returns the jobs for a given queue" do
expect(job_manager.jobs(queue: queue_name).size).to eq jobs.size
expect(job_manager.jobs(queue: queue_name).map(&:job_id)).to match_array jobs.map(&:job_id)
end
it "returns all jobs if no queue is specified" do
expect(job_manager.jobs.size).to eq jobs.size + other_jobs.size
expect(job_manager.jobs.map(&:job_id)).to match_array (jobs + other_jobs).map(&:job_id)
end
end
describe '#job' do
let!(:job) { mail.deliver_later queue: queue_name, wait: 10.minutes }
it "finds the matching job" do
new_job = job_manager.job(job.job_id)
expect(new_job).to be_a JobManager::Job
expect(new_job.job_id).to eq job.job_id
end
end
describe '#delete_job' do
let!(:job) { mail.deliver_later queue: queue_name, wait: 10.minutes }
let(:queued_job) { job_manager.job(job.job_id, queue: queue_name) }
it "deletes the job" do
expect { expect(job_manager.delete_job(job.job_id, queue: queue_name)).to be_a JobManager::Job }
.to change { job_manager.jobs(queue: job.queue_name).size }.by(-1)
end
end
describe "#delete_queue" do
let!(:jobs) { Array.new(2) { mail.deliver_later queue: queue_name, wait: 10.minutes } }
let!(:other_jobs) { Array.new(2) { |i| mail.deliver_later queue: "#{queue_name}--#{i}", wait: 10.minutes } }
it "deletes the items scheduled for the given queue" do
expect { job_manager.delete_queue(queue_name) }
.to change { job_manager.jobs.size }.by(-jobs.size)
expect(job_manager.jobs(queue: queue_name)).to be_empty
expect(job_manager.jobs.map(&:job_id)).to match_array other_jobs.map(&:job_id)
end
end
end

The MIT License (MIT)

Copyright (c) 2016 Joshua Kovach

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment