Class: Caddie::MThreadedUpdater

Inherits:
Object
  • Object
show all
Defined in:
app/models/caddie/m_threaded_updater.rb

Instance Method Summary collapse

Constructor Details

#initialize(max_threads, daily_operations_list) ⇒ MThreadedUpdater

 TODO : - split the work in database using thread_slice_id (set numbers : 1 .. MAX_THREADS) TODO : Then run N threads each threads get the records associated to it’s number



9
10
11
12
# File 'app/models/caddie/m_threaded_updater.rb', line 9

def initialize( max_threads, daily_operations_list )
  @max_threads = max_threads
  @daily_operations_list = daily_operations_list
end

Instance Method Details

#feed_price_histories_threadedObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/caddie/m_threaded_updater.rb', line 14

def feed_price_histories_threaded
  Thread::abort_on_exception = true
  threads = []
  0.upto( @max_threads-1 ).each do |thread_id|
    threads << Thread.new {
      puts Thread.inspect
      Thread.current[:timings] = Caddie::CrestPriceHistoryUpdate.feed_price_histories( thread_id )
    }
  end
  result = []
  ThreadsWait.all_waits( *threads ) do |t|
    thread_result = t[:timings]
    result << thread_result
  end
  tmp = result.map { |a| Vector[*a] }.inject(:+)
  tmp.to_a
end

#split_work_for_threadsObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/caddie/m_threaded_updater.rb', line 32

def split_work_for_threads
  puts 'Start splitting work for threads'
  ids = @daily_operations_list.pluck( :id )
  ActiveRecord::Base.transaction do
    slice_size = ids.count/@max_threads + 1
    ( 0 ... @max_threads ).each do |thread_id|
      ids_slice = ids.shift( slice_size )
      @daily_operations_list.where( id: ids_slice ).update_all( thread_slice_id: thread_id )
    end
  end
  puts 'Finished splitting work for threads'
end