Class: ActiveExchanger::Supervisor

Inherits:
Object
  • Object
show all
Defined in:
lib/active_exchanger/supervisor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode = :normal, &block) ⇒ Supervisor

Returns a new instance of Supervisor.


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/active_exchanger/supervisor.rb', line 5

def initialize(mode = :normal, &block)
  if block_given?
    raise 'Invalid arity must be 1..2' unless (1..2).cover?(block.arity)
    @block = block
  end
  @mode = mode
  @max = ENV['max'].to_i
  @count = nil
  @color = :green
  @cursor = 0
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color


3
4
5
# File 'lib/active_exchanger/supervisor.rb', line 3

def color
  @color
end

Instance Method Details

#check_point(new_cursor = nil) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_exchanger/supervisor.rb', line 23

def check_point(new_cursor = nil)
  raise 'You need to set count before calling check_point' unless @count
  if new_cursor
    @cursor = new_cursor
  else
    @cursor += 1
  end
  if @block
    value = (100.0 * (@cursor.to_f / @count.to_f)).to_i
    if value != @last_value
      if @block.arity == 1
        @block.call(value)
      elsif @block.arity == 2
        @block.call(value, @cursor)
      end
      @last_value = value
    end
  end
end

#count=(value) ⇒ Object


17
18
19
20
21
# File 'lib/active_exchanger/supervisor.rb', line 17

def count=(value)
  raise 'Need a positive value' unless value >= 0
  @count = value
  @count = @max if @max > 0 && @count > @max
end

#debug(msg) ⇒ Object


53
54
55
56
# File 'lib/active_exchanger/supervisor.rb', line 53

def debug(msg)
  print("\n" + 'DEBUG'.white + ': ' + msg) if verbose?
  Rails.logger.debug(msg)
end

#error(msg) ⇒ Object


68
69
70
71
# File 'lib/active_exchanger/supervisor.rb', line 68

def error(msg)
  print("\n" + 'ERROR'.red + ': ' + msg) if verbose?
  Rails.logger.error(msg)
end

#fatal(msg) ⇒ Object


73
74
75
76
# File 'lib/active_exchanger/supervisor.rb', line 73

def fatal(msg)
  print("\n" + 'FATAL'.red + ': ' + msg) if verbose?
  Rails.logger.fatal(msg)
end

#info(msg) ⇒ Object


58
59
60
61
# File 'lib/active_exchanger/supervisor.rb', line 58

def info(msg)
  print("\n" + msg.gsub(/^/, 'INFO'.green + ': ')) if verbose?
  Rails.logger.info(msg)
end

#reset!(value = nil, color = :green) ⇒ Object


43
44
45
46
47
# File 'lib/active_exchanger/supervisor.rb', line 43

def reset!(value = nil, color = :green)
  self.count = value if value
  @cursor = 0
  @color = color
end

#tmp_dir(*subdirs) ⇒ Object


78
79
80
81
82
83
# File 'lib/active_exchanger/supervisor.rb', line 78

def tmp_dir(*subdirs)
  number = (1000 * Time.zone.now.to_f).to_i.to_s(36) + Array.new(3) { rand(1_679_616) }.sum.to_s(36)
  dir = Rails.root.join('tmp', 'exchangers', number, *subdirs)
  FileUtils.mkdir_p(dir)
  dir
end

#verbose?Boolean

Returns:

  • (Boolean)

49
50
51
# File 'lib/active_exchanger/supervisor.rb', line 49

def verbose?
  ENV['VERBOSE'].to_i > 0
end

#warn(msg) ⇒ Object


63
64
65
66
# File 'lib/active_exchanger/supervisor.rb', line 63

def warn(msg)
  print("\n" + 'WARNING'.yellow + ': ' + msg) if verbose?
  Rails.logger.error(msg)
end