Skip to content

Commit f6bc2c1

Browse files
author
mgsnova
committed
perform fetch_feeds with thread pool
1 parent 0d35ec2 commit f6bc2c1

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

‎Gemfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ gem "feedbag", github: "dwillis/feedbag"
1919
gem "coveralls", require: false
2020
gem "uglifier"
2121
gem "highline", require: false
22+
gem "thread"
2223

2324
group :production do
2425
gem "unicorn"

‎Gemfile.lock‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ GEM
133133
slop (3.4.4)
134134
sqlite3 (1.3.7)
135135
thor (0.18.1)
136+
thread (0.0.8)
136137
tilt (1.4.1)
137138
tzinfo (0.3.37)
138139
uglifier (2.1.0)
@@ -173,6 +174,7 @@ DEPENDENCIES
173174
sinatra-contrib!
174175
sinatra-flash
175176
sqlite3
177+
thread
176178
uglifier
177179
unicorn
178180
will_paginate

‎app/tasks/fetch_feeds.rb‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'thread/pool'
12
require_relative "fetch_feed"
23

34
class FetchFeeds
@@ -6,12 +7,18 @@ def initialize(feeds)
67
end
78

89
def fetch_all
10+
pool = Thread.pool(10)
11+
912
@feeds.each do |feed|
10-
FetchFeed.new(feed).fetch
13+
pool.process do
14+
FetchFeed.new(feed).fetch
15+
end
1116
end
17+
18+
pool.shutdown
1219
end
1320

1421
def self.enqueue(feeds)
1522
self.new(feeds).delay.fetch_all
1623
end
17-
end
24+
end

0 commit comments

Comments
 (0)