Class: Tor::Instance

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Strategy::Restart
Defined in:
lib/rest_tor/instance.rb

Constant Summary collapse

ATTRS =
%i(ip port using counter)

Constants included from Strategy::Restart

Strategy::Restart::DEFAULT_EXCEPTION_COUNT, Strategy::Restart::EXCEPTIONS

Instance Method Summary collapse

Methods included from Strategy::Restart

#died?

Constructor Details

#initialize(port, ip: nil, using: nil, counter: nil) ⇒ Instance

Returns a new instance of Instance.



17
18
19
20
21
22
# File 'lib/rest_tor/instance.rb', line 17

def initialize(port, ip: nil, using: nil, counter: nil)
  @port   = port
  @ip     = ip
  @using  = using
  @counter= Counter.new(self, counter || {})
end

Instance Method Details

#apply(&block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rest_tor/instance.rb', line 79

def apply(&block)
  Tor.lock("tor:#{port}:update", expires: 1.minutes) do
    if not Tor.store.has_key?(port)
      logger.info "Has been destroyed"
      return
    end
    if block_given?
      yield.tap do
        Tor.store[port] = attributes
      end
    else
      Tor.store[port] = attributes
    end
  end
end

#attributesObject



31
32
33
# File 'lib/rest_tor/instance.rb', line 31

def attributes
  { ip: @ip, port: @port, using: @using, counter: @counter.to_h }
end

#avaliable?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
# File 'lib/rest_tor/instance.rb', line 95

def avaliable?
  begin
    pid && Process.getpgid( pid ) && true
  rescue Errno::ESRCH
    false
  end
end

#last?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rest_tor/instance.rb', line 43

def last?
  Tor.store.max_by {|(k, v)| k}.try(:first).to_s == port.to_s
end

#pidObject



24
25
26
27
28
29
# File 'lib/rest_tor/instance.rb', line 24

def pid
  path = Tor::TOR_DIR.join("#{port}/tor.pid")
  if File.exists?(path)
    File.read(path).chomp.to_i
  end
end

#release!Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rest_tor/instance.rb', line 47

def release!
  self.using = nil

  if Tor.locked?("#{port}:restart")
    logger.info "The tor(#{port}) already processing!"
  else
    if error=died?
      restart!("Died(#{error})")
    end
  end
  true
end

#restart!(message = "") ⇒ Object



60
61
62
63
# File 'lib/rest_tor/instance.rb', line 60

def restart!(message="")
  logger.info "#{message} Restart => #{ip}:#{port}"
  Tor.restart(port)
end

#stopObject Also known as: destroy



73
74
75
# File 'lib/rest_tor/instance.rb', line 73

def stop
  Tor.stop(port)
end

#use!Object



65
66
67
# File 'lib/rest_tor/instance.rb', line 65

def use!
  self.using = Time.now
end

#using?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/rest_tor/instance.rb', line 69

def using?
  @using.present?
end