Class: AnalyticsOps::Applier

Inherits:
Object
  • Object
show all
Defined in:
lib/analytics_ops/applier.rb,
sig/analytics_ops.rbs

Overview

Executes only the operations contained in an approved, non-stale plan.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(admin:) ⇒ Applier

Returns a new instance of Applier.

Parameters:



13
14
15
# File 'lib/analytics_ops/applier.rb', line 13

def initialize(admin:)
  @admin = admin
end

Instance Method Details

#call(plan, confirm: false) ⇒ Result

Parameters:

  • plan (Plan)
  • confirm: (Boolean) (defaults to: false)

Returns:

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/analytics_ops/applier.rb', line 17

def call(plan, confirm: false)
  raise ConfirmationRequiredError, "Applying a plan requires explicit boolean confirmation" unless confirm == true
  raise InvalidPlanError, "plan must be an AnalyticsOps::Plan" unless plan.is_a?(Plan)

  current = @admin.snapshot(plan.property_id)
  unless current.fingerprint == plan.snapshot_fingerprint
    raise StalePlanError, "Remote state changed after this plan was generated; create a new plan"
  end

  applied = []
  plan.changes.each_with_index do |change, index|
    @admin.apply_change(change, property_id: plan.property_id)
    applied << change.to_h
  rescue StandardError => error
    result = Result.new(
      status: "partial",
      applied:,
      failed: failure(change, error),
      remaining: plan.changes.drop(index + 1).map(&:to_h)
    )
    raise PartialApplyError.new("Apply stopped after #{applied.length} successful changes", result:)
  end

  Result.new(status: "applied", applied:, failed: nil, remaining: [])
end