class Artifactory::Cleaner::DiscoveryWorker

Helper class representing Threads spawned to discover Artifacts

Public Class Methods

new(processing_queues, artifactory_client) click to toggle source
# File lib/artifactory/cleaner/discovery_worker.rb, line 6
def initialize(processing_queues, artifactory_client)
  @running = false
  @working = false
  @thread = nil
  @queues = processing_queues
  @artifactory_client = artifactory_client
end

Public Instance Methods

alive?() click to toggle source

Is the Thread for this worker alive?

# File lib/artifactory/cleaner/discovery_worker.rb, line 33
def alive?
  @thread ? @thread.alive? : false
end
kill()
Alias for: stop
running?() click to toggle source

Is this DiscoveryWorker running, listening to the queue and processing requests

# File lib/artifactory/cleaner/discovery_worker.rb, line 16
def running?
  @running
end
shutdown(timeout = 300) click to toggle source

Stop the Thread and re-join the parent

# File lib/artifactory/cleaner/discovery_worker.rb, line 51
def shutdown(timeout = 300)
  @running = false
  @thread.join(timeout) if @thread and @thread.alive?
end
start() click to toggle source

Start the DiscoveryWorker and begin processing from the queue

# File lib/artifactory/cleaner/discovery_worker.rb, line 39
def start
  @running = true
  @thread = Thread.new do
    while running?
      process @queues.incoming.pop(false)
    end
  end
  self
end
stop() click to toggle source

Forcibly kill the Thread and destroy it

# File lib/artifactory/cleaner/discovery_worker.rb, line 58
def stop
  @running = false
  @thread.kill if @thread and @thread.alive?
  @thread = nil
end
Also aliased as: kill
to_s() click to toggle source

String representation of this DiscoveryWorker and it's status

# File lib/artifactory/cleaner/discovery_worker.rb, line 67
def to_s
  "#<#{self.class}:#{self.object_id}; #{running? ? 'running' : 'not running'}, #{working? ? 'working' : 'idle'}, #{alive? ? 'alive' : 'dead'}>"
end
working?() click to toggle source

Is this DiscoveryWorker currently processing a request?

when running? is true and working? is not, then the worker is idle, blocked, waiting for an action.

when working? is true, there will be at least one more result pushed to the outgoing queue when the current request finishes

# File lib/artifactory/cleaner/discovery_worker.rb, line 27
def working?
  @working
end

Private Instance Methods

discover_artifact_from(artifact_info, retries = 10) click to toggle source

main method, given artifact info, generate a Artifactory::Cleaner::DiscoveredArtifact

this method makes HTTP calls to the Artifactory API

# File lib/artifactory/cleaner/discovery_worker.rb, line 98
def discover_artifact_from(artifact_info, retries = 10)
  artifact = nil
  while retries > 0
    begin
      #STDERR.puts "[DEBUG] thread discover_artifact_from #{artifact_info["uri"]} start"
      retries -= 1
      artifact = Artifactory::Cleaner::DiscoveredArtifact.from_url(artifact_info["uri"], client: @artifactory_client)
      artifact.last_downloaded = Time.parse(artifact_info["lastDownloaded"]) unless artifact_info["lastDownloaded"].to_s.empty?
      #STDERR.puts "[DEBUG] thread discover_artifact_from #{artifact_info["uri"]} end"
      return artifact
    rescue Net::OpenTimeout, Artifactory::Error::ConnectionError => err
      artifact = err
      if retries
        log "[WARN] Connection Failure attempting to reach Artifactory API: #{err}; Retrying in 10 seconds"
        sleep 10
      end
    rescue Artifactory::Error::HTTPError => err
      artifact = err
      if err.code == 404
        log "[WARN] HTTP 404 Not Found fetching: #{artifact_info["uri"]}"
        return nil
      else
        retries = min(retries, 1)
        log "[ERROR] HTTP Error while fetching an artifact from a usage search: #{err}; #{retries ? 'Will' : 'Will not'} retry"
      end
    end
  end
  artifact
end
log(msg) click to toggle source

debug / verbose logging TODO: add a verbose conditional, maybe a different log destination

# File lib/artifactory/cleaner/discovery_worker.rb, line 76
def log(msg)
  STDERR.puts msg
end
process(payload) click to toggle source

pop from the incoming queue, process, push result to outgoing

# File lib/artifactory/cleaner/discovery_worker.rb, line 82
def process(payload)
  @working = true
  begin
    @queues.outgoing.push(discover_artifact_from(payload))
  rescue => ex
    STDERR.puts "[Error] Exception in thread: #{ex.full_message}"
    @queues.outgoing.push(ex)
  ensure
    @working = false
  end
end