Skip to content

Instantly share code, notes, and snippets.

@DmytroVasin
Last active December 23, 2017 21:22
Show Gist options
  • Save DmytroVasin/dd9bc9c5e0df67b07571e9e55a3902f7 to your computer and use it in GitHub Desktop.
Save DmytroVasin/dd9bc9c5e0df67b07571e9e55a3902f7 to your computer and use it in GitHub Desktop.
Threads + Mutext
desc 'Video thumbnails'
task video_thumbnails_ping: :environment do
p 'video_thumbnails_ping START'
t1 = Time.now
THREAD_COUNT = 8 # tweak this number for maximum performance.
MAX_COUNT = 5000
thumbnails = Thumbnail.last(MAX_COUNT).pluck(:id, :combined_url_2, :combined_url_1)
tags = []
mutex = Mutex.new
threads = Array.new(THREAD_COUNT).map do
Thread.new(thumbnails, tags) do |urls, tags|
while thumbnail = mutex.synchronize { urls.pop }
id = thumbnail[0]
_url_1 = thumbnail[1]
_url_2 = thumbnail[2]
length = nil
correct_url = nil
conn = Faraday.head(_url_1)
if conn.status == 200
length = conn.headers['content-length'].to_i
correct_url = _url_1
else
conn = Faraday.head(_url_2)
if conn.status == 200
length = conn.headers['content-length'].to_i
correct_url = _url_2
end
end
mutex.synchronize do
tags << { id: id, length: length, url: correct_url }
end
end
end
end
threads.each(&:join)
t2 = Time.now
p '---------------'
p t2 - t1
p '---------------'
tags.each do |hash|
thumbnail = Thumbnail.find_by(id: hash[:id])
if thumbnail.blank?
p "ERROR: #{ hash[:id] } NOT FOUND"
next
end
thumbnail.update(length: hash[:length], file_name: hash[:url])
end
end
# Thumbnail.where(file_name: nil)
# Thumbnail.where("length > 10_000_000") # 10 MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment