Class: Thinner::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/thinner/client.rb

Overview

A Thinner::Client runs as a background process and purges a list of urls in batches.

Constant Summary

ERRORS =

The list of Errors we want to catch.

[Varnish::Error, Varnish::BrokenConnection, Varnish::CommandFailed, Timeout::Error, Errno::ECONNREFUSED]

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Client) initialize(urls)

Before purging, each Thinner::Client grabs various configuration settings and makes a copy of the passed in urls.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/thinner/client.rb', line 17

def initialize(urls)
  @batch        = Thinner.configuration.batch_length
  @timeout      = Thinner.configuration.sleep_time
  @varnish      = Varnish::Client.new Thinner.configuration.server
  @log_file     = Thinner.configuration.log_file
  @purged_urls  = []
  @urls         = Array.new urls
  @length       = @urls.length
  logger
  handle_errors
end

Instance Attribute Details

- (Object) purged_urls (readonly)

A list of successfully purged urls.



10
11
12
# File 'lib/thinner/client.rb', line 10

def purged_urls
  @purged_urls
end

Instance Method Details

- (Object) close_log

Log the purged urls and exit the process.



81
82
83
84
# File 'lib/thinner/client.rb', line 81

def close_log
  @logger.info "Purged #{@purged_urls.length} of #{@length} urls."
  @logger.info "Exiting..."
end

- (Object) handle_errors

Trap certain signals so the Client can report back the progress of the job and close the log.



66
67
68
69
70
# File 'lib/thinner/client.rb', line 66

def handle_errors
  trap('TERM') { close_log }
  trap('KILL') { close_log }
  trap('INT')  { close_log }
end

- (Object) logger

The logger redirects all STDOUT writes to a logger instance.



73
74
75
76
77
78
# File 'lib/thinner/client.rb', line 73

def logger
  if !@log_file.respond_to?(:write)
    STDOUT.reopen(File.open(@log_file, (File::WRONLY | File::APPEND | File::CREAT)))
  end
  @logger = Logger.new(STDOUT)
end

- (Object) run!

Kickstart the purging process and loop through the array until there aren’t any urls left to purge. Each time the loop runs it will update the process label with the first url in the list.



32
33
34
35
36
37
38
39
40
# File 'lib/thinner/client.rb', line 32

def run!
  while @urls.length > 0
    @current_job = @urls.slice! 0, @batch
    $0 = "#{PROCESS_IDENTIFIER}: purging #{@current_job.first}"
    purge_urls
    sleep @timeout
  end
  close_log
end