class Artifactory::Cleaner::DiscoveryWorker
Helper class representing Threads spawned to discover Artifacts
Public Class Methods
# 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
Is the Thread for this worker alive?
# File lib/artifactory/cleaner/discovery_worker.rb, line 33 def alive? @thread ? @thread.alive? : false end
Is this DiscoveryWorker
running, listening to the queue and processing requests
# File lib/artifactory/cleaner/discovery_worker.rb, line 16 def running? @running end
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 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
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
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
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
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
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
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