Class: Strata::CLI::Utils::DeploymentMonitor

Inherits:
Object
  • Object
show all
Includes:
Terminal
Defined in:
lib/strata/cli/utils/deployment_monitor.rb

Overview

Monitors deployment progress by polling the API and displaying stage updates with animated spinners that transition to checkmarks as stages complete.

Constant Summary collapse

TERMINAL_STATUSES =

Terminal statuses that indicate deployment is complete

%w[succeeded failed].freeze
NOT_STARTED_STAGE =
"not_started"
DEFAULT_POLL_INTERVAL =

seconds

0.5
DEFAULT_TIMEOUT =

seconds (10 minutes)

600
TEST_WAIT_TIMEOUT =

seconds

5
STAGES =
%i[
  not_started
  preparing
  pre_migrations
  project_configuration
  processing_datasources
  processing_models
  removing_deleted_models
  processing_relationships
  forming_universes
  creating_blend_paths
  validating_references
  post_migrations
  cleaning_up
  finished
].freeze
TESTING_STAGE =
:running_tests

Instance Method Summary collapse

Methods included from Terminal

#create_spinner, #print_table, #with_spinner

Constructor Details

#initialize(client, project_id, branch_id, deployment_id, has_tests: nil) ⇒ DeploymentMonitor

Initialize a new DeploymentMonitor

Parameters:

  • client (API::Client)

    The API client for fetching deployment status

  • project_id (String)

    The project identifier

  • branch_id (String)

    The branch identifier

  • deployment_id (String, Integer)

    The deployment identifier

  • has_tests (Boolean, nil) (defaults to: nil)

    Whether tests exist for this deployment

Raises:

  • (ArgumentError)

    if required parameters are nil or empty



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/strata/cli/utils/deployment_monitor.rb', line 49

def initialize(client, project_id, branch_id, deployment_id, has_tests: nil)
  validate_initialization_params(client, project_id, branch_id, deployment_id)

  @client = client
  @project_id = project_id
  @branch_id = branch_id
  @deployment_id = deployment_id
  @has_tests = has_tests
  @spinners = {}
  @completed_stages = Set.new
  @seen_stages = []
  @last_stage = nil
  @tests_running = false
  @deployment_completed = false
end

Instance Method Details

#display_statusObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/strata/cli/utils/deployment_monitor.rb', line 139

def display_status
  deployment = fetch_deployment_status
  return nil if deployment.nil?

  display_initial_status
  display_exit_instruction

  process_deployment_state(deployment)

  display_final_status(deployment) if terminal_status?(deployment_value(deployment, "status"))

  deployment
rescue Interrupt
  stop_all_spinners
  say "\n\n  Exiting status view.\n", ColorHelper.info
  nil
rescue => e
  stop_all_spinners
  say "\n Error fetching deployment status: #{e.message}", ColorHelper.error
  nil
end

#process_deployment_state(deployment) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/strata/cli/utils/deployment_monitor.rb', line 161

def process_deployment_state(deployment)
  status = deployment_value(deployment, "status")
  stage = deployment_value(deployment, "stage")

  return unless stage && stage != NOT_STARTED_STAGE

  process_stage(stage, status, track_seen: false)
end

#start(poll_interval: DEFAULT_POLL_INTERVAL, timeout: DEFAULT_TIMEOUT, skip_initial_display: false) ⇒ Hash?

Start monitoring deployment progress by polling the API

Parameters:

  • poll_interval (Integer) (defaults to: DEFAULT_POLL_INTERVAL)

    Seconds between API polls (default: 2)

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    Maximum seconds to monitor before timing out (default: 600)

  • skip_initial_display (Boolean) (defaults to: false)

    Skip initial status messages (used when called after display_status)

Returns:

  • (Hash, nil)

    The final deployment hash, or nil if interrupted/errored



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/strata/cli/utils/deployment_monitor.rb', line 71

def start(poll_interval: DEFAULT_POLL_INTERVAL, timeout: DEFAULT_TIMEOUT, skip_initial_display: false)
  unless skip_initial_display
    display_initial_status
    display_exit_instruction
  end

  start_time = Time.now

  loop do
    deployment = fetch_deployment_status

    break if deployment.nil?

    status = deployment_value(deployment, "status")
    stage = deployment_value(deployment, "stage")

    unless terminal_status?(status)
      if @last_stage.nil? && stage
        process_deployment_state(deployment)
      elsif stage && stage != @last_stage
        handle_stage_change(stage, status)
      end
    end

    if terminal_status?(status) && !@deployment_completed
      result = handle_deployment_completion(deployment, status, stage)
      @deployment_completed = true
      return result if result
    end

    if @deployment_completed && status == "succeeded" && stage == "finished"
      latest_test_run = deployment_value(deployment, "latest_test_run")

      if @has_tests == true && latest_test_run.nil?
        start_stage_spinner(TESTING_STAGE) unless @tests_running
        @tests_running = true
      elsif latest_test_run
        complete_stage(TESTING_STAGE) if @tests_running
        @tests_running = false
        test_results = deployment_value(latest_test_run, "test_results")
        display_test_results(test_results) if test_results
        return deployment
      else
        return deployment
      end
    end

    if Time.now - start_time > timeout
      complete_stage(TESTING_STAGE) if @tests_running
      say "\n  Monitoring timeout reached (#{timeout}s)", ColorHelper.warning
      say "   Deployment may still be in progress. Check server for status.\n", ColorHelper.info
      return deployment
    end

    sleep(poll_interval)
  end
rescue Interrupt
  stop_all_spinners
  say "\n\n  Monitoring interrupted. Deployment continues in background.", ColorHelper.warning
  say "  Check server for deployment status with command `strata deploy status`.\n", ColorHelper.info
  nil
rescue => e
  stop_all_spinners
  say "\n Error monitoring deployment: #{e.message}", ColorHelper.error
  say "   Check server for deployment status.\n", ColorHelper.info
  nil
end