Class: Archaeo::DownloadState

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(output_dir) ⇒ DownloadState

Returns a new instance of DownloadState.



17
18
19
20
# File 'lib/archaeo/download_state.rb', line 17

def initialize(output_dir)
  @output_dir = output_dir
  @path = File.join(output_dir, STATE_FILE)
end

Instance Attribute Details

#output_dirObject (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

#clearObject



46
47
48
49
50
# File 'lib/archaeo/download_state.rb', line 46

def clear
  @entries = []
  @entries_key = nil
  FileUtils.rm_f(@path)
end

#completed?(timestamp) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/archaeo/download_state.rb', line 22

def completed?(timestamp)
  entries_key.include?(timestamp.to_s)
end

#entry_for(timestamp) ⇒ Object



38
39
40
# File 'lib/archaeo/download_state.rb', line 38

def entry_for(timestamp)
  entries.find { |e| e["ts"] == timestamp.to_s }
end

#mark_completed(timestamp, url: nil, bytes: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/archaeo/download_state.rb', line 26

def mark_completed(timestamp, url: nil, bytes: nil)
  ts = timestamp.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

#total_bytesObject



42
43
44
# File 'lib/archaeo/download_state.rb', line 42

def total_bytes
  entries.sum { |e| e["bytes"].to_i }
end