Class: NewsFetcher::Commands::Update

Inherits:
NewsFetcher::Command show all
Defined in:
lib/newsfetcher/commands/update.rb

Instance Attribute Summary

Attributes inherited from NewsFetcher::Command

#dir, #log_level, #max_threads

Instance Method Summary collapse

Instance Method Details

#run(args) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/newsfetcher/commands/update.rb', line 7

def run(args)
  super
  subscriptions = @profile.find_subscriptions(ids: args).reject(&:disabled?)
  if @profile.config.max_threads == 0
    subscriptions.each(&:update)
  else
    run_threads(subscriptions, &:update)
  end
end

#run_threads(objects, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/newsfetcher/commands/update.rb', line 17

def run_threads(objects, &block)
  threads = []
  objects.each do |object|
    if threads.length >= @profile.config.max_threads
      $logger.debug { "Waiting for #{threads.length} threads to finish" }
      threads.map(&:join)
      threads = []
    end
    threads << Thread.new do
      $logger.debug { "Started thread for #{object.id}" }
      yield(object)
    end
  end
  unless threads.empty?
    $logger.debug { "Waiting for last #{threads.length} threads to finish" }
    threads.map(&:join)
  end
end