Class: Ace::Support::Models::Organisms::SyncOrchestrator
- Inherits:
-
Object
- Object
- Ace::Support::Models::Organisms::SyncOrchestrator
- Defined in:
- lib/ace/support/models/organisms/sync_orchestrator.rb
Overview
Orchestrates the sync workflow
Constant Summary collapse
- DEFAULT_CACHE_MAX_AGE =
24 hours
86_400
Instance Method Summary collapse
-
#initialize(cache_manager: nil) ⇒ SyncOrchestrator
constructor
Initialize orchestrator.
-
#status ⇒ Hash
Check sync status.
-
#sync(force: false, max_age: DEFAULT_CACHE_MAX_AGE) ⇒ Hash
Sync models from API.
Constructor Details
#initialize(cache_manager: nil) ⇒ SyncOrchestrator
Initialize orchestrator
13 14 15 |
# File 'lib/ace/support/models/organisms/sync_orchestrator.rb', line 13 def initialize(cache_manager: nil) @cache_manager = cache_manager || Molecules::CacheManager.new end |
Instance Method Details
#status ⇒ Hash
Check sync status
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ace/support/models/organisms/sync_orchestrator.rb', line 64 def status if @cache_manager.exists? data = @cache_manager.read stats = calculate_stats(data) if data { cached: true, fresh: @cache_manager.fresh?, last_sync_at: @cache_manager.last_sync_at, stats: stats } else { cached: false, fresh: false, last_sync_at: nil, stats: nil } end end |
#sync(force: false, max_age: DEFAULT_CACHE_MAX_AGE) ⇒ Hash
Sync models from API
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ace/support/models/organisms/sync_orchestrator.rb', line 21 def sync(force: false, max_age: DEFAULT_CACHE_MAX_AGE) # Check if we need to sync unless force if @cache_manager.fresh?(max_age: max_age) return { status: :skipped, message: "Cache is fresh (less than #{max_age / 3600}h old)", last_sync_at: @cache_manager.last_sync_at } end end # Fetch from API start_time = Time.now raw_json = Atoms::ApiFetcher.fetch # Parse JSON data = Atoms::JsonParser.parse(raw_json) # Write to cache @cache_manager.write(data) # Calculate stats stats = calculate_stats(data) duration = Time.now - start_time { status: :success, message: "Synced #{stats[:model_count]} models from #{stats[:provider_count]} providers", duration: duration.round(2), stats: stats, sync_at: Time.now } rescue NetworkError, ApiError => e { status: :error, message: e., error_class: e.class.name } end |