class Tor::Instance

Constants

ATTRS

Public Class Methods

new(port, ip: nil, using: nil, counter: nil, created_at: Time.now) click to toggle source
# File lib/rest_tor/instance.rb, line 17
def initialize(port, ip: nil, using: nil, counter: nil, created_at: Time.now)
  @port       = port
  @ip         = ip
  @using      = using
  @counter    = Counter.new(self, counter || {})
  @created_at = created_at
end

Public Instance Methods

apply() { || ... } click to toggle source
# File lib/rest_tor/instance.rb, line 80
def apply(&block)
  Tor.lock("tor:#{port}:update", expires: 1.minutes) do
    if not Tor.store.has_key?(port)
      Tor.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
attributes() click to toggle source
# File lib/rest_tor/instance.rb, line 32
def attributes
  { ip: @ip, port: @port, using: @using, counter: @counter.to_h, created_at: @created_at }
end
destroy()
Alias for: stop
last?() click to toggle source
# File lib/rest_tor/instance.rb, line 44
def last?
  Tor.store.max_by {|(k, v)| k}.try(:first).to_s == port.to_s
end
pid() click to toggle source
# File lib/rest_tor/instance.rb, line 25
def pid
  path = Tor.config.dir.join("#{port}/tor.pid")
  if File.exists?(path)
    File.read(path).chomp.to_i
  end
end
release!() click to toggle source
# File lib/rest_tor/instance.rb, line 48
def release!
  self.using = nil

  if Tor.locked?("#{port}:restart")
    Tor.logger.info "The tor(#{port}) already processing!"
  else
    if error=died?
      restart!("Died(#{error})")
    end
  end
  true
end
restart!(message="") click to toggle source
# File lib/rest_tor/instance.rb, line 61
def restart!(message="")
  Tor.logger.info "#{message} Restart => #{ip}:#{port}"
  Tor.restart(port)
end
stop() click to toggle source
# File lib/rest_tor/instance.rb, line 74
def stop
  Tor.stop(port)
end
Also aliased as: destroy
use!() click to toggle source
# File lib/rest_tor/instance.rb, line 66
def use!
  self.using = Time.now
end
using?() click to toggle source
# File lib/rest_tor/instance.rb, line 70
def using?
  @using.present?
end