Skip to content

Instantly share code, notes, and snippets.

@luciotbc
Last active February 1, 2023 15:29
Show Gist options
  • Save luciotbc/dde402bcba57135a085ddee4636392e7 to your computer and use it in GitHub Desktop.
Save luciotbc/dde402bcba57135a085ddee4636392e7 to your computer and use it in GitHub Desktop.
sidekiq hacks

Sidekiq

Infra

Parando o sidekiq usando o rails (recomendado)

ps -ef | grep sidekiq | grep busy | grep -v grep | awk '{print $2}' > tmp/sidekiq.pid
cat tmp/sidekiq.pid
bundle exec sidekiqctl stop tmp/sidekiq.pid

Parando usando o sistema operacional

ps -ef | grep sidekiq | grep busy | grep -v grep | awk '{print $2}'
kill -9 $(ps -ef | grep sidekiq | grep busy | grep -v grep | awk '{print $2}')

Iniciando o sidekiq

bundle exec sidekiq -d -L log/sidekiq.log -C config/sidekiq.yml

Coding

Status do sidekiq

stats = Sidekiq::Stats.new
stats.processed
stats.failed
stats.scheduled_size
stats.retry_size
stats.dead_size
stats.processes_size
stats.default_queue_latency
stats.workers_size
stats.enqueued

Listando o total de Jobs em uma fila por classes

queue_name = 'default'
Sidekiq::Queue.new(queue_name).map{|j| j.klass}.group_by{|e| e}.map{|k, v| [k, v.length]}.to_h

Listando o total de jobs em retry por classes

Sidekiq::RetrySet.new.map{|j| j.klass}.group_by{|e| e}.map{|k, v| [k, v.length]}.to_h

Listando o total de mortos por classes

Sidekiq::DeadSet.new.map{|j| j.klass}.group_by{|e| e}.map{|k, v| [k, v.length]}.to_h

Selecionando jobs em retry por class name

job_class_name = 'SidekiqTest::SidekiqTestWorker'
jobs = Sidekiq::RetrySet.new.select { |job| job.klass == job_class_name }; nil;
jobs.size

Selecionando jobs mortos por class name

job_class_name = 'SidekiqTest::SidekiqTestWorker'
jobs = Sidekiq::DeadSet.new.select { |job| job.klass == job_class_name }; nil;
jobs.size

Selecionando jobs enfileirados em uma fila pelo class name

queue_name = 'default'
job_class_name = 'SidekiqTest::SidekiqTestWorker'
jobs = Sidekiq::Queue.new(queue_name).select {|job| job.klass == job_class_name };nil;
jobs.count

Movendo 1000 jobs de uma classe em uma fila para outra fila

queue_name = 'default'
new_queue_name = 'funnels_test_worker'
queue = Sidekiq::Queue.new(queue_name)

queue.first(1000).each do |job|
  if job.klass == "SidekiqTest::SidekiqTestWorker"
    SidekiqTest::SidekiqTestWorker.set(queue: new_queue_name).perform_async(*job.args)
    job.delete
  end
end;nil

Adicionando jobs na fila de retry

jobs.each(&:retry)

Deletando jobs selecionados

jobs.each(&:delete)

Listando informações de todos os processos local

ps = Sidekiq::ProcessSet.new
ps.size
ps.each do |process|
  p "pid: #{process['pid']} hostname: #{process['hostname']} busy: #{process['busy']} quiet: #{process['quiet']} queues: #{process['queues'].length} #{process['queues'].length} [#{process['queues'].sort.join(', ')}]"
end

Pausando os processos local

ps = Sidekiq::ProcessSet.new
ps.each(&:quiet!)

Parando os processos local

ps = Sidekiq::ProcessSet.new
ps.each(&:stop!)

Parando os workers local

workers = Sidekiq::Workers.new
workers.size
workers.each do |process_id, thread_id, work|
  p "process_id: #{process_id} thread_id: #{thread_id} queue: #{work['queue']} retry: #{work['payload']['retry']} class: #{work['payload']['class']}"
end

Clear sidekiq redis - APAGA TUDO!!!

Sidekiq.redis { |conn| conn.flushdb }

Scripts para melhorar a navegação**

Url para exibir mais jobs

http://localhost:3000/sidekiq/retries?count=100000

Adiciona ordenação na tabela de retry

$('.container') .css({
'max-width': '100%',
'width': 'auto'
});

$('.table td') .css({
'overflow': 'auto'
})

$('.table td pre').css({
'overflow': 'auto',
'border': '1px solid black',
'margin': '10px 0'
});

$('.table th') .eq(1) .width('');
$('.table th') .eq(2) .width('');
$.getScript( "//cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.13.3/jquery.tablesorter.min.js" )
.done(function( script, textStatus ) {
  $('table') .tablesorter();
});

Seleciona jobs em retry por texto

// Usa o nome da fila funnels_test_worker
$("td:contains('funnels_test_worker')").parent().find('td input').prop('checked', true)

// Usa o erro ZeroDivisionError: divided by 0
$("td:contains('ZeroDivisionError: divided by 0')").parent().find('td input').prop('checked', true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment