93 lines
2.5 KiB
Ruby
93 lines
2.5 KiB
Ruby
# typed: false
|
|
class Domain::Fa::PostEnqueuer
|
|
include HasBulkEnqueueJobs
|
|
include HasColorLogger
|
|
include HasMeasureDuration
|
|
include Domain::Fa::HasCountFailedInQueue
|
|
|
|
def initialize(
|
|
reverse_scan_holes:,
|
|
start_at:,
|
|
low_water_mark:,
|
|
high_water_mark:
|
|
)
|
|
@low_water_mark = low_water_mark
|
|
@high_water_mark = high_water_mark
|
|
raise if @high_water_mark <= @low_water_mark
|
|
@post_iterator =
|
|
Enumerator.new do |e|
|
|
if reverse_scan_holes
|
|
while start_at > 0
|
|
if !Domain::Fa::Post.exists?(fa_id: start_at)
|
|
e << [nil, start_at, nil]
|
|
end
|
|
start_at -= 1
|
|
end
|
|
else
|
|
Domain::Fa::Post
|
|
.where("id >= ?", start_at)
|
|
.where("file_id is null")
|
|
.where(state: "ok")
|
|
.find_each { |p| e << [p.id, p.fa_id, p.file_url_str] }
|
|
end
|
|
end
|
|
end
|
|
|
|
def run_once
|
|
already_enqueued = enqueued_count
|
|
if already_enqueued <= @low_water_mark
|
|
to_enqueue = @high_water_mark - already_enqueued
|
|
logger.info(
|
|
"enqueuing #{to_enqueue.to_s.bold} more posts - #{already_enqueued.to_s.bold} already enqueued",
|
|
)
|
|
rows =
|
|
measure(
|
|
proc do |p|
|
|
p && "gathered #{p.length.to_s.bold} posts" || "gathering posts..."
|
|
end,
|
|
) do
|
|
to_enqueue
|
|
.times
|
|
.map do
|
|
@post_iterator.next
|
|
rescue StopIteration
|
|
nil
|
|
end
|
|
.reject(&:nil?)
|
|
end
|
|
|
|
measure("enqueue jobs") do
|
|
rows.each do |post_id, fa_id, file_url_str|
|
|
if file_url_str.nil?
|
|
Domain::Fa::Job::ScanPostJob.perform_later({ fa_id: fa_id })
|
|
action = "page scan"
|
|
else
|
|
Domain::Fa::Job::ScanFileJob.perform_later({ fa_id: fa_id })
|
|
action = "file scan"
|
|
end
|
|
|
|
if post_id.nil?
|
|
model_info = "(not seen)"
|
|
else
|
|
model_info = "post #{post_id.to_s.bold}"
|
|
end
|
|
logger.info "[#{action}] [#{model_info}] (fa_id: #{fa_id.to_s.bold})"
|
|
end
|
|
end
|
|
raise StopIteration if rows.empty?
|
|
else
|
|
logger.info(
|
|
"#{already_enqueued.to_s.bold} already enqueued (max #{@high_water_mark.to_s.bold}) - " +
|
|
"waiting to fall below #{@low_water_mark.to_s.bold}",
|
|
)
|
|
:sleep
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def enqueued_count
|
|
count_failed_in_queue(%w[static_file fa_post])
|
|
end
|
|
end
|