Class: UrlTracker::Server
- Inherits:
-
Object
- Object
- UrlTracker::Server
- Includes:
- SocketCommunication
- Defined in:
- lib/url_tracker/server.rb
Overview
Class that waits for messages on a given socket and responds back to clients. It can track URLs, list currently tracked URLs and stop tracking. The interval between consecutive checks for change in the pages is customizable.
Constant Summary
Constants included from SocketCommunication
UrlTracker::SocketCommunication::InvalidSocketError, UrlTracker::SocketCommunication::MAX_CONN_QUEUE, UrlTracker::SocketCommunication::MAX_MESSAGE_LENGTH
Instance Attribute Summary
Attributes included from SocketCommunication
Instance Method Summary collapse
-
#initialize(logger = Logger.new(STDERR)) ⇒ Server
constructor
Initializes a new server which logs its activities using the passed logger object.
-
#list ⇒ Object
List tracked URLs.
-
#loop_forever ⇒ Object
Main server method.
-
#release(uri) ⇒ Object
Release an URL.
-
#run(params) ⇒ Object
Runs the server, according to the argv options passed.
-
#stop ⇒ Object
Stops the current server.
-
#track(uri) ⇒ Object
Track an URL.
Methods included from SocketCommunication
#bind, #close_connection, #connect, #next_message, #wait_for_connection, #write
Constructor Details
#initialize(logger = Logger.new(STDERR)) ⇒ Server
Initializes a new server which logs its activities using the passed logger object. Defaults to Ruby’s Logger class. Log level defaults to Logger::INFO
and you can override it by setting the URL_TRACKER_DEBUG environment variable
17 18 19 20 21 |
# File 'lib/url_tracker/server.rb', line 17 def initialize(logger=Logger.new(STDERR)) setup_signals @logger = logger @logger.level = level_from_env || Logger::INFO end |
Instance Method Details
#list ⇒ Object
List tracked URLs
52 53 54 |
# File 'lib/url_tracker/server.rb', line 52 def list UrlTracker.list_all end |
#loop_forever ⇒ Object
Main server method. Loops forever, waiting for new connections, and checking new messages. Takes the appropriate action according to what is received, such as a new URL to track.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/url_tracker/server.rb', line 26 def loop_forever running = true while wait_for_connection @logger.info("New client connected") command, *arguments = .split @logger.debug "#{command} received" response = case command when /^track$/i then track(arguments.first) when /^list$/i then list when /^release$/i then release(arguments.first) end write(response) unless response.nil? end rescue => e @logger.error("An error occurred when waiting for new connections!\n\t#{e.inspect}\n\t#{e.backtrace.join("\n\t")}") end |
#release(uri) ⇒ Object
Release an URL
57 58 59 60 |
# File 'lib/url_tracker/server.rb', line 57 def release(uri) @logger.info("Releasing URL #{uri}") UrlTracker.release_uri(uri) end |
#run(params) ⇒ Object
Runs the server, according to the argv options passed. Possible options:
-s, --socket [FILE] #=> Uses FILE as socket file for communication
-f, --fork #=> Forks and works as a daemon
params
can also be a hash, containing the parsed information to the server.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/url_tracker/server.rb', line 69 def run(params) @logger.info "UrlTracker #{UrlTracker::VERSION} starting. Log level is #{@logger.level.inspect}." = parse(params) @socket_file = .socket_file @pid = nil bind(@socket_file) @logger.info "Server starting at socket #{Pathname.new(@socket_file).realpath.to_s}" if .fork @pid = fork { loop_forever } @logger.info "Forking to background. Child pid #{@pid}" else loop_forever end end |
#stop ⇒ Object
Stops the current server
89 90 91 |
# File 'lib/url_tracker/server.rb', line 89 def stop @pid ? Process.kill('TERM', @pid) : close_connection end |
#track(uri) ⇒ Object
Track an URL
46 47 48 49 |
# File 'lib/url_tracker/server.rb', line 46 def track(uri) @logger.info("Tracking URL #{uri}") UrlTracker.track_uri(uri) end |