ruby
42 lines · 7 steps
A thread pool for thumbnail jobs in Ruby
A fixed set of worker threads pull image jobs off a bounded queue and generate thumbnails concurrently.
Explained by
highlit
1class ThumbnailPool
2 def initialize(worker_count: 4, capacity: 100)
3 @queue = SizedQueue.new(capacity)
4 @running = true
5 @workers = Array.new(worker_count) { spawn_worker }
6 end
7
8 def enqueue(image_id, sizes:)
9 raise "pool shut down" unless @running
10 @queue.push(image_id: image_id, sizes: sizes)
11 end
12
13 def shutdown
14 @running = false
15 @workers.size.times { @queue.push(:stop) }
16 @workers.each(&:join)
17 end
18
19 private
20
21 def spawn_worker
22 Thread.new do
23 loop do
24 job = @queue.pop
25 break if job == :stop
26
27 process(job)
28 end
29 end
30 end
31
32 def process(job)
33 image = Image.find(job[:image_id])
34 job[:sizes].each do |size|
35 image.generate_thumbnail(size)
36 end
37 rescue ActiveRecord::RecordNotFound
38 Rails.logger.warn("skipping missing image #{job[:image_id]}")
39 rescue => e
40 Rails.logger.error("thumbnail failed for #{job[:image_id]}: #{e.message}")
41 end
42end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A bounded SizedQueue applies backpressure so producers block instead of overwhelming memory.
- 2Poison-pill sentinels like :stop let each worker exit cleanly without killing threads mid-job.
- 3Isolating error handling inside the worker loop keeps one bad job from taking down the whole pool.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
go
func (w *Watcher) resetDebounce(d time.Duration) { if !w.timer.Stop() { select { case <-w.timer.C:
Debouncing a stream of events in Go
debounce
timers
channels
Advanced
7 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-thread-pool-for-thumbnail-jobs-in-ruby-explained-ruby-75fd/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.