Class: Archaeo::DownloadState
- Inherits:
-
Object
- Object
- Archaeo::DownloadState
- Defined in:
- lib/archaeo/download_state.rb
Overview
Tracks download progress for resume support.
Persists completed snapshot metadata to a JSONL state file within the output directory, allowing interrupted downloads to resume without re-fetching already downloaded snapshots.
Constant Summary collapse
- STATE_FILE =
".archaeo-state"
Instance Attribute Summary collapse
-
#output_dir ⇒ Object
readonly
Returns the value of attribute output_dir.
Instance Method Summary collapse
- #clear ⇒ Object
- #completed?(timestamp) ⇒ Boolean
- #entry_for(timestamp) ⇒ Object
-
#initialize(output_dir) ⇒ DownloadState
constructor
A new instance of DownloadState.
- #mark_completed(timestamp, url: nil, bytes: nil) ⇒ Object
- #size ⇒ Object
- #timestamps ⇒ Object
- #total_bytes ⇒ Object
Constructor Details
#initialize(output_dir) ⇒ DownloadState
Returns a new instance of DownloadState.
17 18 19 20 21 |
# File 'lib/archaeo/download_state.rb', line 17 def initialize(output_dir) @output_dir = output_dir @path = File.join(output_dir, STATE_FILE) @mutex = Mutex.new end |
Instance Attribute Details
#output_dir ⇒ Object (readonly)
Returns the value of attribute output_dir.
15 16 17 |
# File 'lib/archaeo/download_state.rb', line 15 def output_dir @output_dir end |
Instance Method Details
#clear ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/archaeo/download_state.rb', line 57 def clear @mutex.synchronize do @entries = [] @entries_key = nil FileUtils.rm_f(@path) end end |
#completed?(timestamp) ⇒ Boolean
23 24 25 |
# File 'lib/archaeo/download_state.rb', line 23 def completed?() @mutex.synchronize { entries_key.include?(.to_s) } end |
#entry_for(timestamp) ⇒ Object
41 42 43 |
# File 'lib/archaeo/download_state.rb', line 41 def entry_for() @mutex.synchronize { entries.find { |e| e["ts"] == .to_s } } end |
#mark_completed(timestamp, url: nil, bytes: nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/archaeo/download_state.rb', line 27 def mark_completed(, url: nil, bytes: nil) @mutex.synchronize do ts = .to_s return if entries_key.include?(ts) entry = { "ts" => ts, "at" => Time.now.utc.iso8601 } entry["url"] = url if url entry["bytes"] = bytes if bytes entries << entry @entries_key = nil save end end |
#size ⇒ Object
49 50 51 |
# File 'lib/archaeo/download_state.rb', line 49 def size @mutex.synchronize { entries.size } end |
#timestamps ⇒ Object
53 54 55 |
# File 'lib/archaeo/download_state.rb', line 53 def @mutex.synchronize { entries.map { |e| e["ts"] } } end |
#total_bytes ⇒ Object
45 46 47 |
# File 'lib/archaeo/download_state.rb', line 45 def total_bytes @mutex.synchronize { entries.sum { |e| e["bytes"].to_i } } end |