Class: Ace::Support::Models::Organisms::SyncOrchestrator

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

Constructor Details

#initialize(cache_manager: nil) ⇒ SyncOrchestrator

Initialize orchestrator

Parameters:



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

#statusHash

Check sync status

Returns:

  • (Hash)

    Status info



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

Parameters:

  • force (Boolean) (defaults to: false)

    Force sync even if cache is fresh

  • max_age (Integer) (defaults to: DEFAULT_CACHE_MAX_AGE)

    Max cache age in seconds

Returns:

  • (Hash)

    Sync result with stats



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.message,
    error_class: e.class.name
  }
end