Class: Gel::Pinboard
- Inherits:
-
Object
- Object
- Gel::Pinboard
- Defined in:
- lib/gel/pinboard.rb
Overview
For each URI, this stores:
* the current etag
* an external freshness token
* a stale flag
Constant Summary collapse
- UPDATE_CONCURRENCY =
8
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #add(uri, token: nil) ⇒ Object
- #async_file(uri, token: nil, tail: true, only_updated: false, error: nil) ⇒ Object
- #etag(uri) ⇒ Object
- #file(uri, token: nil, tail: true) ⇒ Object
- #filename(uri) ⇒ Object
-
#initialize(root, httpool: Gel::Httpool.new, work_pool: nil) ⇒ Pinboard
constructor
A new instance of Pinboard.
- #read(uri) ⇒ Object
- #stale(uri, token) ⇒ Object
- #updated(uri, etag, changed = true) ⇒ Object
Constructor Details
#initialize(root, httpool: Gel::Httpool.new, work_pool: nil) ⇒ Pinboard
Returns a new instance of Pinboard.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gel/pinboard.rb', line 16 def initialize(root, httpool: Gel::Httpool.new, work_pool: nil) @root = root @httpool = httpool @db = Gel::DB.new(root, "pins") @files = {} @waiting = Hash.new { |h, k| h[k] = [] } @work_pool = work_pool || Gel::WorkPool.new(UPDATE_CONCURRENCY, name: "gel-pinboard") @monitor = Monitor.new end |
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
15 16 17 |
# File 'lib/gel/pinboard.rb', line 15 def root @root end |
Instance Method Details
#add(uri, token: nil) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/gel/pinboard.rb', line 85 def add(uri, token: nil) @db[uri.to_s] ||= { etag: nil, token: token, stale: true, } end |
#async_file(uri, token: nil, tail: true, only_updated: false, error: nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gel/pinboard.rb', line 43 def async_file(uri, token: nil, tail: true, only_updated: false, error: nil) file_to_yield = nil @monitor.synchronize do already_done = @files.key?(uri) && @files[uri].done? if !already_done && stale(uri, token) add uri, token: token already_queued = @files.key?(uri) tail_file = @files[uri] ||= Gel::TailFile.new(uri, self, httpool: @httpool) unless already_queued @work_pool.queue(uri.path) do begin tail_file.update(force_reset: !tail) rescue Exception => ex if error error.call(ex) else raise end end end @work_pool.start end @waiting[uri] << lambda do |f, changed| yield f if changed || !only_updated end elsif !only_updated file_to_yield = filename(uri) end end if file_to_yield File.open(file_to_yield, "r") do |f| yield f end end end |
#etag(uri) ⇒ Object
97 98 99 |
# File 'lib/gel/pinboard.rb', line 97 def etag(uri) read(uri)[:etag] end |
#file(uri, token: nil, tail: true) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/gel/pinboard.rb', line 28 def file(uri, token: nil, tail: true) @monitor.synchronize do add uri, token: token tail_file = Gel::TailFile.new(uri, self, httpool: @httpool) tail_file.update(force_reset: !tail) if stale(uri, token) end if block_given? File.open(filename(uri), "r") do |f| yield f end end end |
#filename(uri) ⇒ Object
93 94 95 |
# File 'lib/gel/pinboard.rb', line 93 def filename(uri) File.(mangle_uri(uri), @root) end |
#read(uri) ⇒ Object
113 114 115 |
# File 'lib/gel/pinboard.rb', line 113 def read(uri) @db[uri.to_s] end |
#stale(uri, token) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/gel/pinboard.rb', line 101 def stale(uri, token) if h = read(uri) if token && h[:token] == token || token == false return h[:stale] end @db[uri.to_s] = h.merge(token: token, stale: true) end true end |
#updated(uri, etag, changed = true) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/gel/pinboard.rb', line 117 def updated(uri, etag, changed = true) blocks = nil @monitor.synchronize do @db[uri.to_s] = read(uri).merge(etag: etag, stale: false) blocks = @waiting.delete(uri) end return unless blocks && blocks.any? File.open(filename(uri), "r") do |f| blocks.each do |block| f.rewind block.call(f, changed) end end end |