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.



15
16
17
18
19
20
# File 'lib/rest_tor/instance.rb', line 15

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rest_tor/instance.rb', line 70

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



22
23
24
# File 'lib/rest_tor/instance.rb', line 22

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

#avaliable?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/rest_tor/instance.rb', line 86

def avaliable?
  `ps aux | grep tor | grep RunAsDaemon | grep -v "ps aux" | grep #{port} | awk '{print $2}'`.to_s.strip.to_i > 0
end

#last?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rest_tor/instance.rb', line 34

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

#release!Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rest_tor/instance.rb', line 38

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



51
52
53
54
# File 'lib/rest_tor/instance.rb', line 51

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

#stopObject Also known as: destroy



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

def stop
  Tor.stop(port)
end

#use!Object



56
57
58
# File 'lib/rest_tor/instance.rb', line 56

def use!
  self.using = Time.now
end

#using?Boolean

Returns:

  • (Boolean)


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

def using?
  @using.present?
end