Class: FC::CopyRule

Inherits:
DbBase show all
Defined in:
lib/fc/copy_rule.rb

Class Attribute Summary collapse

Attributes inherited from DbBase

#additional_fields, #database_fields, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DbBase

apply!, create_from_fiels, #delete, #dump, find, #initialize, #load, #reload, #save, set_table, where

Constructor Details

This class inherits a constructor from FC::DbBase

Class Attribute Details

.copy_storages_cache_timeObject

Returns the value of attribute copy_storages_cache_time.



8
9
10
# File 'lib/fc/copy_rule.rb', line 8

def copy_storages_cache_time
  @copy_storages_cache_time
end

.get_copy_storages_mutexObject

Returns the value of attribute get_copy_storages_mutex.



8
9
10
# File 'lib/fc/copy_rule.rb', line 8

def get_copy_storages_mutex
  @get_copy_storages_mutex
end

.get_rules_mutexObject

Returns the value of attribute get_rules_mutex.



8
9
10
# File 'lib/fc/copy_rule.rb', line 8

def get_rules_mutex
  @get_rules_mutex
end

.rules_cache_timeObject

Returns the value of attribute rules_cache_time.



8
9
10
# File 'lib/fc/copy_rule.rb', line 8

def rules_cache_time
  @rules_cache_time
end

Class Method Details

.allObject



15
16
17
18
19
20
21
22
23
# File 'lib/fc/copy_rule.rb', line 15

def self.all
  get_rules_mutex.synchronize do
    unless @all_rules_cache && Time.new.to_i - @get_all_rules_time.to_i < rules_cache_time
      @get_all_rules_time = Time.new.to_i
      @all_rules_cache = where("1")
    end
  end
  @all_rules_cache
end

.check_all(item_id, size, item_copies, name, tag, dir, src_storage) ⇒ Object



25
26
27
28
29
# File 'lib/fc/copy_rule.rb', line 25

def self.check_all(item_id, size, item_copies, name, tag, dir, src_storage)
  all.select do |r|
    r.check(item_id, size, item_copies, name, tag, dir, src_storage)
  end
end

.get_proper_storage_for_copy(options) ⇒ Object

get available storage for copy



32
33
34
35
36
37
38
39
# File 'lib/fc/copy_rule.rb', line 32

def self.get_proper_storage_for_copy(options)
  rules = check_all(options[:item_id].to_i, options[:size].to_i, options[:item_copies].to_i, options[:name].to_s, options[:tag].to_s, options[:dir] ? true : false, options[:src_storage])
  result = nil
  rules.detect do |rule|
    result = FC::Storage.select_proper_storage_for_create(rule.get_copy_storages, options[:size].to_i, options[:exclude] || [])
  end
  result
end

Instance Method Details

#check(item_id, size, item_copies, name, tag, dir, src_storage) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/fc/copy_rule.rb', line 52

def check(item_id, size, item_copies, name, tag, dir, src_storage)
  return false unless rule
  $SELF = 4
  r = eval(rule)
  $SELF = 0
  r ? true : false
end

#get_copy_storagesObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/fc/copy_rule.rb', line 41

def get_copy_storages
  self.class.get_copy_storages_mutex.synchronize do
    unless @copy_storages_cache && Time.new.to_i - @get_copy_storages_time.to_i < self.class.copy_storages_cache_time
      @get_copy_storages_time = Time.new.to_i
      names = copy_storages.to_s.split(',').map{|s| "'#{s}'"}.join(',')
      @copy_storages_cache = names.empty? ? [] : FC::Storage.where("name IN (#{names}) ORDER BY FIELD(name, #{names})")
    end
  end
  @copy_storages_cache
end

#testObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fc/copy_rule.rb', line 60

def test
  storage = FC::Storage.new(
    :id => 1,
    :name => 'test_storage',
    :host => 'test_host',
    :path => '/bla/bla',
    :url  => 'http://bla',
    :size => 1000,
    :size_limit => 9999,
    :check_time => Time.new.to_i, 
    :copy_storages => 'a,b,c'
  )
  check(3, 1, 1, 'test/item', 'tag', false, storage)
end