따라서 전체 rebake가 완료되기를 기다리지 않고 라이브로 전환해야 했습니다. 라이브 전환 후에도 rebake를 실행할 수 없었으며, 그렇게 하면 서버가 중단되기 때문입니다. 또한 기본 큐에서 rebake를 실행하면 알림과 같은 다른 모든 프로세스가 지연되므로 이 역시 불가능했습니다.
그래서 저는 이 스크립트를 사용하여 모든 rebake 작업을 ultra-low 우선순위 sidekiq 큐에 등록(enqueue)했습니다. 다른 분들도 필요할 수 있으니 여기에 공유합니다.
# slow_rebake_resumer_ultralow.rb
# 특정 지점부터 rebake 작업의 등록을 재개하고, 해당 작업을
# 직접 :ultra_low 우선순위 큐로 전송합니다.
# 컨테이너 내에서 -> rails runner slow_rebake_resumer_ultralow.rb 로 실행
# --- 설정 ---
BATCH_SIZE = 500
SLEEP_DURATION = 5
# --- 마지막 업데이트에서의 시작 지점 ---
last_processed_id = 0
# --------------------
start_time = Time.now
total_posts = Post.count
puts "#{total_posts}개의 게시물을 대상으로 :ultra_low 큐에 rebake 작업 등록을 재개하고 있습니다..."
puts "게시물 ID #{last_processed_id} 이후부터 시작합니다."
puts "---"
loop do
# 처리할 다음 배치의 게시물을 효율적으로 찾습니다
posts_to_enqueue = Post.where("id > ?", last_processed_id).order(id: :asc).limit(BATCH_SIZE)
# 더 이상 게시물이 없으면 루프를 종료합니다
break if posts_to_enqueue.empty?
posts_to_enqueue.each do |post|
begin
# 대상 큐를 포함한 모든 옵션을 단일 해시로 정의합니다
options = {
post_id: post.id,
cook: true,
bypass_bump: true,
queue: :ultra_low
}
# 올바른, 검증된 구문으로 작업을 등록합니다
Jobs.enqueue(:process_post, options)
rescue => e
puts "!!! 게시물 ID #{post.id} 작업 등록 중 오류 발생: #{e.message}"
end
end
# 현재 배치의 마지막 게시물 ID로 위치를 업데이트합니다
last_processed_id = posts_to_enqueue.last.id
# 진행 상황 보고를 위해 더 정확한 카운트를 가져옵니다
enqueued_count = Post.where("id <= ?", last_processed_id).count
# --- 진행 상황 보고 ---
percentage = (enqueued_count.to_f / total_posts * 100).round(2)
elapsed_minutes = ((Time.now - start_time) / 60).round(2)
puts "게시물 ID #{last_processed_id}까지 등록 완료. 총 등록 수: ~#{enqueued_count} / #{total_posts} (#{percentage}%))"
puts "경과 시간 (이번 세션): #{elapsed_minutes}분."
puts "작업들이 :ultra_low 큐로 전송되고 있습니다."
# 부하를 낮게 유지하기 위해 배치 사이에 잠시 대기합니다
if enqueued_count < total_posts
puts "#{SLEEP_DURATION}초 동안 대기 중..."
sleep(SLEEP_DURATION)
puts "---"
end
end
puts "\n나머지 모든 rebake 작업이 :ultra_low 큐에 성공적으로 등록되었습니다!"