Project: a link checker

The checker

The second project checks a list of URLs and fails when one of them is broken, which makes it a pipeline step. Each URL gets a thread, so a slow host does not hold up the rest, and the results are collected by position so the report keeps the order of the file.

bin/linkcheck

#!/usr/bin/env ruby require "net/http" urls = File.readlines(ARGV[0], chomp: true).reject(&:empty?) results = Array.new(urls.length) threads = urls.each_with_index.map do |url, index| Thread.new do results[index] = begin Net::HTTP.get_response(URI(url)).code rescue StandardError => error error.class.name end end end threads.each(&:join) urls.each_with_index { |url, index| puts "#{results[index]} #{url}" } failures = results.count { |code| code != "200" } exit(failures.zero? ? 0 : 1)

The rescue turns a dead host into a result instead of a crash: a SocketError is printed like any other answer, and the exit code still counts it as a failure.

A good URL and a bad one

The project uses a local server so the test does not depend on the internet. Two routes, one real and one missing:

server.rb

require "webrick" server = WEBrick::HTTPServer.new(Port: 8000) server.mount_proc("/ok") { |_req, res| res.body = "ok" } server.start

WEBrick is a bundled gem in Ruby 3.4, so gem install webrick comes before this file runs. Start it with ruby server.rb, then run the checker in another terminal.

urls.txt

http://localhost:8000/ok
http://localhost:8000/missing

ruby bin/linkcheck urls.txt

200 http://localhost:8000/ok
404 http://localhost:8000/missing

The exit code

The report is for a person; the exit code is for the pipeline. With one broken URL the command exits 1:

ruby bin/linkcheck urls.txt; echo $?

200 http://localhost:8000/ok
404 http://localhost:8000/missing
1

Point it at two working URLs and the same command exits zero, which is the only difference a pipeline sees. A good.txt repeats the working URL:

good.txt

http://localhost:8000/ok
http://localhost:8000/ok

ruby bin/linkcheck good.txt; echo $?

200 http://localhost:8000/ok
200 http://localhost:8000/ok
0

What to watch for

Do not start one thread per URL for a list of a thousand. A small pool of workers pulling from a Queue keeps the load on the target host sane and the memory flat; the pattern is in lesson 12.15.

A checker that follows redirects can loop. Net::HTTP does not follow them by default, so a 301 in the report is a real answer, and a link that moved is worth seeing once.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why does the checker give each URL its own thread?

    Answer

    A slow host does not hold up the other requests

    The threads wait at the same time, so one slow answer costs one wait, not one wait per URL.

  2. Why write each result to results[index] instead of appending to an array?

    Answer

    The report keeps the order of the input file

    The index belongs to the input line, so the report is readable whatever order the threads finish in.

  3. What makes the checker a pipeline step?

    Answer

    Its exit code: zero when every URL works, one when one does not

    A pipeline reads the status, not the table, so the exit call is the interface that matters.

  4. What does the rescue StandardError inside the thread add?

    Answer

    A dead host becomes a result line instead of a crash

    The error class name is stored like any other answer, and the exit code still counts it as a failure.

  5. What does this program print? results = Array.new(3) threads = ["a", "b", "c"].each_with_index.map do |value, index| Thread.new { results[index] = value.upcase } end threads.each(&:join) p results

    Answer

    ["A", "B", "C"]

    Each thread writes its own index, so the order of the array is the order of the input.

  6. What does this program print? codes = ["200", "404", "500"] failures = codes.count { |code| code != "200" } p failures

    Answer

    2

    Two codes are not 200, which is the number the exit call turns into status 1.

  7. What does this program print? File.write("list.txt", "a\n\nb\n") urls = File.readlines("list.txt", chomp: true).reject(&:empty?) p urls

    Answer

    ["a", "b"]

    chomp removes the newlines and reject drops the empty line, which is why a blank line in urls.txt is harmless.

  8. What does this program print? require "uri" uri = URI("http://localhost:8000/ok") p uri.path

    Answer

    "/ok"

    URI splits the address into its parts, and path is the part after the host and port.

  9. A URL list is read without chomp: true. What goes wrong?

    Answer

    The trailing newline travels with the URL and the request fails

    readlines keeps the line ending, so the last character of the URL is a newline unless chomp removes it.

  10. The results are collected with results << code instead of by index. What is the problem?

    Answer

    The lines come out in the order the threads finish, not the file order

    An append records the finish order, which changes between runs and no longer lines up with urls.txt.

Article author: Arthur Isaev