Class: Caddie::MThreadedUpdater
- Inherits:
-
Object
- Object
- Caddie::MThreadedUpdater
- Defined in:
- app/models/caddie/m_threaded_updater.rb
Instance Method Summary collapse
- #feed_price_histories_threaded ⇒ Object
-
#initialize(max_threads, daily_operations_list) ⇒ MThreadedUpdater
constructor
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.
- #split_work_for_threads ⇒ Object
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_threaded ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/models/caddie/m_threaded_updater.rb', line 14 def feed_price_histories_threaded split_work_for_threads Thread::abort_on_exception = true threads = [] 0.upto( @max_threads-1 ).each do |thread_id| threads << Thread.new { thread_log = File.open( "log/feed_price_histories_threaded_#{thread_id}.log", 'w') Thread.current[:timings] = Caddie::CrestPriceHistoryUpdate. feed_price_histories( updates_ids: @threads_split[ thread_id ], thread_log_file: thread_log ) } end result = [] ThreadsWait.all_waits( *threads ) do |t| thread_result = t[:timings] result << thread_result end results = result.map { |a| Vector[*a] }.inject(:+).to_a results[ 2 ] = result.map{ |e| e[2] }.max # For timings we are using the max, not the sum. results end |
#split_work_for_threads ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/models/caddie/m_threaded_updater.rb', line 35 def split_work_for_threads puts 'Start splitting work for threads' ids = @daily_operations_list.pluck( :id ) @threads_split = [] slice_size = ids.count/@max_threads + 1 ( 0 ... @max_threads ).each do |thread_id| @threads_split[ thread_id ] = ids.shift( slice_size ) end puts 'Finished splitting work for threads' end |