hackernews script: running in separate thread now to make backtracing easier

This commit is contained in:
HoneyryderChuck 2018-03-26 14:37:58 +01:00
parent ec17795dbe
commit d02e328077

View File

@ -1,21 +1,39 @@
require "httpx"
require "oga"
frontpage = HTTPX.get("https://news.ycombinator.com").to_s
html = Oga.parse_html(frontpage)
links = html.css('.itemlist a.storylink').map{|link| link.get('href') }
links = links.select {|l| l.start_with?("https") }
puts links
responses = HTTPX.get(*links)
links.each_with_index do |l, i|
puts "#{responses[i].status}: #{l}"
def print_status
# thread backtraces
Thread.list.each do |th|
# next if th == Thread.current
id = th.object_id
puts "THREAD ID: #{id}"
th.backtrace.each do |line|
puts "\t#{id}: " + line
end if th.backtrace
puts "#" * 20
end
end
Signal.trap("INFO") { print_status } unless ENV.key?("CI")
Thread.start do
frontpage = HTTPX.get("https://news.ycombinator.com").to_s
html = Oga.parse_html(frontpage)
links = html.css('.itemlist a.storylink').map{|link| link.get('href') }
links = links.select {|l| l.start_with?("https") }
puts links
responses = HTTPX.get(*links)
links.each_with_index do |l, i|
puts "#{responses[i].status}: #{l}"
end
end.join