Skip to content

Commit be44d2d

Browse files
nuxllifearenales
authored andcommitted
Passing only feed id instead of the whole object for Delayed Job
While running Stringer in a Docker container, there was an issue on passing the Feed data to the Delayed Job. The whole object was serialized by the application (FetchFeeds class) and deserialized by the Delayed Job. That's not a good practice and can cause several issues, as you can see in collectiveidea/delayed_job_active_record#101 To solve this, instead of passing the whole Feed object to the Delayed Job, only its id attribute is sent. Then, the Feed is get from the database and its content is fetched normally. Plus, the @pool object (a pool of threads) was also serialized and sent, causing issues. If no pool is passed to the job, it should be instantiated from inside the job itself, avoiding the serialize/unserialize issues. Keeping the pool argument in the FetchFeeds#initialize method ensures the tests can still mock it and run the defined expect clauses.
1 parent 0dd251c commit be44d2d

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

app/tasks/fetch_feeds.rb

+14-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
require_relative "fetch_feed"
44

55
class FetchFeeds
6-
def initialize(feeds, pool = Thread.pool(10))
6+
def initialize(feeds, pool = nil)
7+
@pool = pool
78
@feeds = feeds
8-
@pool = pool
9+
@feeds_ids = []
910
end
1011

1112
def fetch_all
13+
@pool ||= Thread.pool(10)
14+
15+
@feeds = FeedRepository.fetch_by_ids(@feeds_ids) if @feeds.blank? && !@feeds_ids.blank?
16+
1217
@feeds.each do |feed|
1318
@pool.process do
1419
FetchFeed.new(feed).fetch
@@ -20,7 +25,13 @@ def fetch_all
2025
@pool.shutdown
2126
end
2227

28+
def prepare_to_delay
29+
@feeds_ids = @feeds.map(&:id)
30+
@feeds = []
31+
self
32+
end
33+
2334
def self.enqueue(feeds)
24-
new(feeds).delay.fetch_all
35+
new(feeds).prepare_to_delay.delay.fetch_all
2536
end
2637
end

0 commit comments

Comments
 (0)