Class: Thinner::Purger

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

Overview

A Thinner::Purger dispatches a client and ensures only one instance of a Thinner::Client is running at a given time.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Purger) initialize(urls)

Each Purger accepts a list of urls to pass on to the client to purge.



8
9
10
# File 'lib/thinner/purger.rb', line 8

def initialize(urls)
  @urls = urls
end

Class Method Details

+ (Object) job_ids

A list of Thinner::Client process ids — adapted from resque.



26
27
28
29
30
# File 'lib/thinner/purger.rb', line 26

def self.job_ids
  lines = `ps -A -o pid,command | grep #{PROCESS_IDENTIFIER}`.split("\n").map do |line|
    line.split(' ')[0].to_i
  end
end

+ (Object) stop!

Before we spin up a new client each running process is killed by pid. Each killed process’s id is logged in the Thinner log file.



34
35
36
37
38
39
40
41
42
43
# File 'lib/thinner/purger.rb', line 34

def self.stop!
  my_id = $$
  job_ids.each do |pid|
    begin
      Process.kill("KILL", pid.to_i) unless pid.to_i == my_id
      puts "==== Killing process: #{pid}"
    rescue Errno::ESRCH
    end
  end
end

Instance Method Details

- (Object) purge!

After the configuration is in place and the Purger has a list of urls, it can fork a client process to run in the background. By default the Purger will kill any old Thinner::Client processes still running so as to not double up on purge requests.



16
17
18
19
20
21
22
23
# File 'lib/thinner/purger.rb', line 16

def purge!
  self.class.stop! unless Thinner.configuration.no_kill
  puts "==== Starting purge see: #{Thinner.configuration.log_file} for finished urls."
  client_id = fork {
    Client.new(@urls).run!
  }
  Process.detach(client_id)
end