Class: WebpageArchivist::Fetcher::FetcherWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/webpage-archivist/fetcher/fetcher.rb

Overview

Wait for callbacks for webpages’ requests, stop event machine when all requests are over and manage the snapshot generation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFetcherWatcher

Returns a new instance of FetcherWatcher.



38
39
40
41
42
43
# File 'lib/webpage-archivist/fetcher/fetcher.rb', line 38

def initialize
  @waiting_requests = 0
  @status = :starting
  @requests = []
  @thread_pool = Pool.new 1
end

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests.



36
37
38
# File 'lib/webpage-archivist/fetcher/fetcher.rb', line 36

def requests
  @requests
end

Instance Method Details

#add_request(request) ⇒ Object

Add a request to wait for



54
55
56
57
# File 'lib/webpage-archivist/fetcher/fetcher.rb', line 54

def add_request request
  @waiting_requests += 1
  @requests << request
end

#end_request(request, ok) ⇒ Object

A request is over

request

the request

ok

indicates if the request went ok, in this case ask for a snapshot



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/webpage-archivist/fetcher/fetcher.rb', line 62

def end_request request, ok
  @waiting_requests -= 1
  if ok && request.instance
    @thread_pool.schedule do
      ::WebpageArchivist::Snapshoter.snapshot_instance request.instance
    end
  end
  if (@status == :waiting) && (@waiting_requests <= 0)
    end_watcher
  end
end

#end_watcherObject

End the watch



75
76
77
78
# File 'lib/webpage-archivist/fetcher/fetcher.rb', line 75

def end_watcher
  EM.stop
  @thread_pool.shutdown
end

#waitObject

Start to wait



46
47
48
49
50
51
# File 'lib/webpage-archivist/fetcher/fetcher.rb', line 46

def wait
  @status = :waiting
  if @waiting_requests == 0
    end_watcher
  end
end