Skip to content

Sync Orchestrator #29

@dmitrytrager

Description

@dmitrytrager

Coordinate full sync flow with transaction safety

  class SyncOrchestrator
    def perform
      report_status(:syncing)

      manifest = fetch_manifest

      ActiveRecord::Base.transaction do
        sync_tags(manifest["tags"])
        sync_providers_with_content(manifest["providers"])
        cleanup_orphans(manifest)
        update_device_config(manifest)
      end

      report_status(:synced)
    rescue ManifestChangedError    <----- this may be redundant
      # Manifest changed during sync, restart
      retry
    rescue => e
      report_status(:error, error_message: e.message)
      raise
    end

    private

    def sync_providers_with_content(providers)
      providers.each_with_index do |provider_data, idx|
        provider = provider_sync.sync(provider_data)

        provider_data["topics"].each do |topic_data|
          # Check manifest hasn't changed every 5 files
          check_manifest_unchanged! if should_check_manifest?(idx)

          topic = topic_sync.sync(topic_data, provider)

          topic_data["files"].each do |file_data|
            topic_file_sync.sync(file_data, topic)
          end
        end
      end
    end
  end

Run sync process automatically

  class ContentSyncJob < ApplicationJob
    queue_as :default

    def perform
      return unless network_available?
      return if sync_in_progress?

      SyncOrchestrator.new.perform
    end
  end
  • Schedule periodic checks (e.g., hourly via cron or whenever gem)
  • Detect network connectivity before attempting
  • Prevent concurrent sync attempts with advisory lock

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions