Class: FC::Var

Inherits:
Object
  • Object
show all
Defined in:
lib/fc/var.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cache_timeObject

Returns the value of attribute cache_time.



6
7
8
# File 'lib/fc/var.rb', line 6

def cache_time
  @cache_time
end

Class Method Details

.get(name, default_value = nil) ⇒ Object



21
22
23
# File 'lib/fc/var.rb', line 21

def self.get(name, default_value = nil)
  get_all[name] || default_value
end

.get_allObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fc/var.rb', line 25

def self.get_all
  @mutex.synchronize do
    if !@get_all_read_time || Time.new.to_i - @get_all_read_time > cache_time
      @all_vars = {}
      FC::DB.query("SELECT * FROM #{FC::DB.prefix}vars").each do |row|
        @all_vars[row['name']] = row['val']
        @all_vars[row['name'].to_sym] = row['val']
      end
      @get_all_read_time = Time.new.to_i
    end
  end
  @all_vars 
end

.get_autosyncObject



39
40
41
42
43
44
45
# File 'lib/fc/var.rb', line 39

def self.get_autosync
  sync_interval = {
    'all' => 604_800 # default 7 days in seconds
  }
  sync_interval.merge! Hash[get('autosync_intervals').to_s.split(';;').map { |v| v.split('::') }]
  sync_interval.each { |host, val| sync_interval[host] = val.to_i > 0 ? val.to_i : 0 }
end

.get_current_speed_limitObject



74
75
76
77
78
79
# File 'lib/fc/var.rb', line 74

def self.get_current_speed_limit
  limits = self.get_speed_limits
  limit = limits[FC::Storage.curr_host]
  limit = limits['all'] unless limit
  limit
end

.get_speed_limitsObject



58
59
60
61
62
63
64
65
# File 'lib/fc/var.rb', line 58

def self.get_speed_limits
  limits = {
    'all' => nil
  }
  list = self.get('daemon_copy_speed_per_host_limit', '').to_s
  limits.merge! Hash[list.split(';;').map{|v| v.split('::')}]
  limits.each{|host, val| limits[host] = val.to_f > 0 ? val.to_f : nil }
end

.set(name, val) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/fc/var.rb', line 12

def self.set(name, val)
  @mutex.synchronize do
    FC::DB.query("UPDATE #{FC::DB.prefix}vars SET val='#{Mysql2::Client.escape(val.to_s)}' WHERE name='#{Mysql2::Client.escape(name.to_s)}'")
    FC::DB.query("INSERT IGNORE INTO #{FC::DB.prefix}vars SET val='#{Mysql2::Client.escape(val.to_s)}', name='#{Mysql2::Client.escape(name.to_s)}'")
    @all_vars[name.to_s] = val.to_s
    @all_vars[name.to_sym] = val.to_s
  end
end

.set_autosync(host, val) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/fc/var.rb', line 47

def self.set_autosync(host, val)
  current = get_autosync
  if val.nil? || val == ''
    current.delete(host)
  else
    current[host] = val
  end
  list = current.map { |h, v| "#{h}::#{v}" }.join(';;')
  set('autosync_intervals', list)
end

.set_speed_limit(host, val) ⇒ Object



67
68
69
70
71
72
# File 'lib/fc/var.rb', line 67

def self.set_speed_limit(host, val)
  limits = self.get_speed_limits
  limits[host.to_s] = val.to_f
  list = limits.map{|h, v| "#{h}::#{v}"}.join(';;')
  self.set('daemon_copy_speed_per_host_limit', list)
end