Class: CopyTaskThread

Inherits:
BaseThread show all
Defined in:
lib/daemon/copy_task_thread.rb

Instance Method Summary collapse

Methods inherited from BaseThread

#initialize

Constructor Details

This class inherits a constructor from BaseThread

Instance Method Details

#calc_avg_countObject



63
64
65
# File 'lib/daemon/copy_task_thread.rb', line 63

def calc_avg_count
  $copy_cont_avg = ($copy_count + $copy_cont_avg) / 2.0
end

#go(storage_name) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/daemon/copy_task_thread.rb', line 2

def go(storage_name)
  return unless $tasks_copy[storage_name]
  Thread.current[:tasks_processed] = 0 unless Thread.current[:tasks_processed]
  while task = $tasks_copy[storage_name].shift do
    $curr_tasks << task
    $log.debug("CopyTaskThread(#{storage_name}): run task for item_storage ##{task.id}, copy_count=#{$copy_count}/#{$copy_cont_avg}, copy_speed=#{$copy_speed}")
    make_copy(task)
    $curr_tasks.delete(task)
    $log.debug("CopyTaskThread(#{storage_name}): finish task for item_storage ##{task.id}")
    Thread.current[:tasks_processed] += 1
    exit if $exit_signal
  end
end

#make_copy(task) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/daemon/copy_task_thread.rb', line 16

def make_copy(task)
  sleep 0.1 while $copy_count > FC::Var.get('daemon_copy_tasks_per_host_limit', 10).to_i
  limit = FC::Var.get_current_speed_limit
  speed_limit = nil
  if limit
    if $copy_count == 0
      $copy_speed = 0
      speed_limit = limit * 0.75
    elsif $copy_count <= $copy_cont_avg && (speed_limit = limit / $copy_cont_avg) < limit * 1.1 - $copy_speed
    else
      while (speed_limit = (limit - $copy_speed) * 0.75) < limit*0.1 do
        sleep 0.1
      end
    end
  end
  $copy_count += 1
  $copy_speed += speed_limit if speed_limit
  calc_avg_count()
  
  storage = $storages.detect{|s| s.name == task.storage_name}
  begin
    item = FC::Item.find(task.item_id)
  rescue Exception => e
    if e.message.match('Record not found')
      $log.warn("Item ##{task.item_id} not found before copy")
      return nil
    else 
      raise e
    end
  end
  return nil unless item && item.status == 'ready'
  src_item_storage = FC::ItemStorage.where("item_id = ? AND status = 'ready'", item.id).sample
  unless src_item_storage
    $log.warn("Item ##{item.id} #{item.name} has no ready item_storage")
    return nil 
  end
  src_storage = $all_storages.detect{|s| s.name == src_item_storage.storage_name}
  $log.debug("Copy from #{src_storage.name} to #{storage.name} #{storage.path}#{item.name}")
  item.copy_item_storage(src_storage, storage, task, false, speed_limit)
rescue Exception => e
  error "Copy item_storage error: #{e.message}; #{e.backtrace.join(', ')}", :item_id => task.item_id, :item_storage_id => task.id
  $curr_tasks.delete(task)
ensure 
  $copy_count -= 1 if $copy_count > 0
  $copy_speed -= speed_limit if speed_limit
end