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.



41
42
43
44
45
# File 'lib/webpage-archivist/fetcher/fetcher.rb', line 41

def initialize
  @waiting_requests = 0
  @status = :starting
  @requests = []
end

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests.



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

def requests
  @requests
end

Instance Method Details

#add_request(request) ⇒ Object

Add a request to wait for



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

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



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

def end_request request, ok
  if ok && request.instance
    operation = proc { ::WebpageArchivist::Snapshoter.snapshot_instance(request.instance) }
    callback = proc { end_request_inner }
    EM.defer(operation, callback)
  else
    end_request_inner
  end
end

#end_request_innerObject



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

def end_request_inner
  @waiting_requests -= 1
  if (@status == :waiting) && (@waiting_requests <= 0)
    end_watcher
  end
end

#end_watcherObject

End the watch



82
83
84
# File 'lib/webpage-archivist/fetcher/fetcher.rb', line 82

def end_watcher
  EM.stop
end

#waitObject

Start to wait



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

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