Class: Soba::Services::StatusManager
- Inherits:
-
Object
- Object
- Soba::Services::StatusManager
- Defined in:
- lib/soba/services/status_manager.rb
Instance Attribute Summary collapse
-
#status_file ⇒ Object
readonly
Returns the value of attribute status_file.
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize(status_file) ⇒ StatusManager
constructor
A new instance of StatusManager.
- #read ⇒ Object
- #update_current_issue(issue_number, phase) ⇒ Object
- #update_last_processed ⇒ Object
- #update_memory(memory_mb) ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(status_file) ⇒ StatusManager
Returns a new instance of StatusManager.
12 13 14 |
# File 'lib/soba/services/status_manager.rb', line 12 def initialize(status_file) @status_file = status_file end |
Instance Attribute Details
#status_file ⇒ Object (readonly)
Returns the value of attribute status_file.
10 11 12 |
# File 'lib/soba/services/status_manager.rb', line 10 def status_file @status_file end |
Instance Method Details
#clear ⇒ Object
69 70 71 |
# File 'lib/soba/services/status_manager.rb', line 69 def clear FileUtils.rm_f(status_file) end |
#read ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/soba/services/status_manager.rb', line 30 def read return nil unless File.exist?(status_file) content = File.read(status_file) return nil if content.empty? JSON.parse(content, symbolize_names: true) rescue JSON::ParserError, StandardError nil end |
#update_current_issue(issue_number, phase) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/soba/services/status_manager.rb', line 41 def update_current_issue(issue_number, phase) data = read || {} data[:current_issue] = { number: issue_number, phase: phase, started_at: Time.now.iso8601, } write(data) end |
#update_last_processed ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/soba/services/status_manager.rb', line 51 def update_last_processed data = read || {} if data[:current_issue] data[:last_processed] = { number: data[:current_issue][:number], completed_at: Time.now.iso8601, } data.delete(:current_issue) end write(data) end |
#update_memory(memory_mb) ⇒ Object
63 64 65 66 67 |
# File 'lib/soba/services/status_manager.rb', line 63 def update_memory(memory_mb) data = read || {} data[:memory_mb] = memory_mb write(data) end |
#write(data) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/soba/services/status_manager.rb', line 16 def write(data) dir = File.dirname(status_file) FileUtils.mkdir_p(dir) unless File.directory?(dir) # Atomic write using temp file temp_file = "#{status_file}.tmp" File.write(temp_file, JSON.pretty_generate(data)) File.rename(temp_file, status_file) rescue StandardError => e # Clean up temp file if something goes wrong FileUtils.rm_f(temp_file) if defined?(temp_file) raise e end |