diff --git a/app/models/infra_conversion_job.rb b/app/models/infra_conversion_job.rb index 85eb0cbda37..8b80e1b256e 100644 --- a/app/models/infra_conversion_job.rb +++ b/app/models/infra_conversion_job.rb @@ -77,17 +77,17 @@ def state_settings @state_settings ||= { :waiting_for_ip_address => { :description => 'Waiting for VM IP address', - :weight => 1, + :weight => 2, :max_retries => 1.hour / state_retry_interval }, :running_migration_playbook => { :description => "Running #{migration_phase}-migration playbook", - :weight => 10, + :weight => 15, :max_retries => 6.hours / state_retry_interval }, :shutting_down_vm => { :description => "Shutting down virtual machine", - :weight => 1, + :weight => 2, :max_retries => 15.minutes / state_retry_interval }, :transforming_vm => { @@ -97,7 +97,7 @@ def state_settings }, :waiting_for_inventory_refresh => { :description => "Identify destination VM", - :weight => 4, + :weight => 1, :max_retries => 1.hour / state_retry_interval }, :applying_right_sizing => { @@ -110,7 +110,7 @@ def state_settings }, :powering_on_vm => { :description => "Power on virtual machine", - :weight => 1, + :weight => 2, :max_retries => 15.minutes / state_retry_interval }, :marking_vm_migrated => { @@ -210,8 +210,11 @@ def update_migration_task_progress(state_phase, state_progress = nil) progress = migration_task.options[:progress] || { :current_state => state, :percent => 0.0, :states => {} } state_hash = send(state_phase, progress[:states][state.to_sym], state_progress) progress[:states][state.to_sym] = state_hash - progress[:current_description] = state_settings[state.to_sym][:description] if state_phase == :on_entry && state_settings[state.to_sym][:description].present? - progress[:percent] += state_hash[:percent] * state_settings[state.to_sym][:weight] / 100.0 if state_settings[state.to_sym][:weight].present? + if state_phase == :on_entry + progress[:current_state] = state + progress[:current_description] = state_settings[state.to_sym][:description] if state_settings[state.to_sym][:description].present? + end + progress[:percent] = progress[:states].map { |k, v| v[:percent] * (state_settings[k.to_sym][:weight] || 0) / 100.0 }.inject(0) { |sum, x| sum + x } migration_task.update_transformation_progress(progress) abort_conversion('Migration cancelation requested', 'ok') if migration_task.cancel_requested? end diff --git a/spec/models/infra_conversion_job_spec.rb b/spec/models/infra_conversion_job_spec.rb index 25b2c4d1876..28fbc1510b0 100644 --- a/spec/models/infra_conversion_job_spec.rb +++ b/spec/models/infra_conversion_job_spec.rb @@ -105,18 +105,20 @@ context 'state hash methods' do before do - job.state = 'running_in_automate' - job.context[:retries_running_in_automate] = 48 + job.state = 'running_migration_playbook' + job.context[:retries_running_migration_playbook] = 288 + task.update_options(:migration_phase => 'pre') end context '.on_entry' do it 'initializes the state hash if it did not exist' do Timecop.freeze(2019, 2, 6) do expect(job.on_entry(nil, nil)).to eq( - :state => 'active', - :status => 'Ok', - :started_on => Time.now.utc, - :percent => 0.0 + :description => 'Running pre-migration playbook', + :state => 'active', + :status => 'Ok', + :started_on => Time.now.utc, + :percent => 0.0 ) end end @@ -195,153 +197,265 @@ end context '.update_migration_task_progress' do - it 'initializes the progress hash on entry if it does not exist' do - Timecop.freeze(2019, 2, 6) do - job.update_migration_task_progress(:on_entry) - expect(task.reload.options[:progress]).to eq( - :current_state => 'running_in_automate', - :percent => 0.0, - :states => { - :running_in_automate => { - :state => 'active', - :status => 'Ok', - :started_on => Time.now.utc, - :percent => 0.0 + context 'on_entry' do + it 'initializes the progress hash on entry if it does not exist' do + Timecop.freeze(2019, 2, 6) do + progress = { + :current_state => 'waiting_for_ip_address', + :current_description => 'Waiting for VM IP address', + :percent => 2.0, + :states => { + :waiting_for_ip_address => { + :description => 'Waiting for VM IP address', + :state => 'finished', + :status => 'Ok', + :started_on => Time.now.utc - 10.minutes, + :updated_on => Time.now.utc - 5.minutes, + :percent => 100.0 + } } } - ) + task.update_options(:progress => progress) + job.update_migration_task_progress(:on_entry) + expect(task.reload.options[:progress]).to eq( + :current_state => 'running_migration_playbook', + :current_description => 'Running pre-migration playbook', + :percent => 2.0, + :states => { + :waiting_for_ip_address => { + :description => 'Waiting for VM IP address', + :state => 'finished', + :status => 'Ok', + :started_on => Time.now.utc - 10.minutes, + :updated_on => Time.now.utc - 5.minutes, + :percent => 100.0 + }, + :running_migration_playbook => { + :description => 'Running pre-migration playbook', + :state => 'active', + :status => 'Ok', + :started_on => Time.now.utc, + :percent => 0.0 + } + } + ) + end end end - it 'updates the task progress hash on retry without a state progress hash' do - Timecop.freeze(2019, 2, 6) do - progress = { - :current_state => 'running_in_automate', - :percent => 10.0, - :states => { - :running_in_automate => { - :state => 'active', - :status => 'Ok', - :started_on => Time.now.utc - 1.minute, - :percent => 10.0, - :updated_on => Time.now.utc - 30.seconds + context 'on_retry' do + it 'updates the task progress hash on retry without a state progress hash' do + Timecop.freeze(2019, 2, 6) do + progress = { + :current_state => 'running_migration_playbook', + :current_description => 'Running pre-migration playbook', + :percent => 3.5, + :states => { + :waiting_for_ip_address => { + :description => 'Waiting for VM IP address', + :state => 'finished', + :status => 'Ok', + :started_on => Time.now.utc - 10.minutes, + :updated_on => Time.now.utc - 5.minutes, + :percent => 100.0 + }, + :running_migration_playbook => { + :description => 'Running pre-migration playbook', + :state => 'active', + :status => 'Ok', + :started_on => Time.now.utc - 1.minute, + :percent => 10.0, + :updated_on => Time.now.utc - 30.seconds + } } } - } - task.update_options(:progress => progress) - job.update_migration_task_progress(:on_retry) - expect(task.reload.options[:progress]).to eq( - :current_state => 'running_in_automate', - :percent => 10.0, - :states => { - :running_in_automate => { - :state => 'active', - :status => 'Ok', - :started_on => Time.now.utc - 1.minute, - :percent => 20.0, - :updated_on => Time.now.utc + task.update_options(:progress => progress) + job.update_migration_task_progress(:on_retry) + expect(task.reload.options[:progress]).to eq( + :current_state => 'running_migration_playbook', + :current_description => 'Running pre-migration playbook', + :percent => 5.0, + :states => { + :waiting_for_ip_address => { + :description => 'Waiting for VM IP address', + :state => 'finished', + :status => 'Ok', + :started_on => Time.now.utc - 10.minutes, + :updated_on => Time.now.utc - 5.minutes, + :percent => 100.0 + }, + :running_migration_playbook => { + :description => 'Running pre-migration playbook', + :state => 'active', + :status => 'Ok', + :started_on => Time.now.utc - 1.minute, + :percent => 20.0, + :updated_on => Time.now.utc + } } - } - ) + ) + end end - end - it 'updates the task progress hash on retry with a state progress hash' do - Timecop.freeze(2019, 2, 6) do - progress = { - :current_state => 'running_in_automate', - :percent => 10.0, - :states => { - :running_in_automate => { - :state => 'active', - :status => 'Ok', - :started_on => Time.now.utc - 1.minute, - :percent => 10.0, - :updated_on => Time.now.utc - 30.seconds + it 'updates the task progress hash on retry with a state progress hash' do + Timecop.freeze(2019, 2, 6) do + progress = { + :current_state => 'running_migration_playbook', + :current_description => 'Running pre-migration playbook', + :percent => 3.5, + :states => { + :waiting_for_ip_address => { + :description => 'Waiting for VM IP address', + :state => 'finished', + :status => 'Ok', + :started_on => Time.now.utc - 10.minutes, + :updated_on => Time.now.utc - 5.minutes, + :percent => 100.0 + }, + :running_migration_playbook => { + :description => 'Running pre-migration playbook', + :state => 'active', + :status => 'Ok', + :started_on => Time.now.utc - 1.minute, + :percent => 10.0, + :updated_on => Time.now.utc - 30.seconds + } } } - } - task.update_options(:progress => progress) - job.update_migration_task_progress(:on_retry, :percent => 30) - expect(task.reload.options[:progress]).to eq( - :current_state => 'running_in_automate', - :percent => 10.0, - :states => { - :running_in_automate => { - :state => 'active', - :status => 'Ok', - :started_on => Time.now.utc - 1.minute, - :percent => 30.0, - :updated_on => Time.now.utc + task.update_options(:progress => progress) + job.update_migration_task_progress(:on_retry, :percent => 30) + expect(task.reload.options[:progress]).to eq( + :current_state => 'running_migration_playbook', + :current_description => 'Running pre-migration playbook', + :percent => 6.5, + :states => { + :waiting_for_ip_address => { + :description => 'Waiting for VM IP address', + :state => 'finished', + :status => 'Ok', + :started_on => Time.now.utc - 10.minutes, + :updated_on => Time.now.utc - 5.minutes, + :percent => 100.0 + }, + :running_migration_playbook => { + :description => 'Running pre-migration playbook', + :state => 'active', + :status => 'Ok', + :started_on => Time.now.utc - 1.minute, + :percent => 30.0, + :updated_on => Time.now.utc + } } - } - ) + ) + end end end - it 'updates the task progress hash on exit' do - Timecop.freeze(2019, 2, 6) do - progress = { - :current_state => 'running_in_automate', - :percent => 10.0, - :states => { - :running_in_automate => { - :state => 'active', - :status => 'Ok', - :started_on => Time.now.utc - 1.minute, - :percent => 10.0, - :updated_on => Time.now.utc - 30.seconds + context 'on_exit and on_error' do + it 'updates the task progress hash on exit' do + Timecop.freeze(2019, 2, 6) do + progress = { + :current_state => 'running_migration_playbook', + :current_description => 'Running pre-migration playbook', + :percent => 6.5, + :states => { + :waiting_for_ip_address => { + :description => 'Waiting for VM IP address', + :state => 'finished', + :status => 'Ok', + :started_on => Time.now.utc - 10.minutes, + :updated_on => Time.now.utc - 5.minutes, + :percent => 100.0 + }, + :running_migration_playbook => { + :description => 'Running pre-migration playbook', + :state => 'active', + :status => 'Ok', + :started_on => Time.now.utc - 1.minute, + :percent => 30.0, + :updated_on => Time.now.utc + } } } - } - task.update_options(:progress => progress) - job.update_migration_task_progress(:on_exit) - expect(task.reload.options[:progress]).to eq( - :current_state => 'running_in_automate', - :percent => 10.0, - :states => { - :running_in_automate => { - :state => 'finished', - :status => 'Ok', - :started_on => Time.now.utc - 1.minute, - :percent => 100.0, - :updated_on => Time.now.utc + task.update_options(:progress => progress) + job.update_migration_task_progress(:on_exit) + expect(task.reload.options[:progress]).to eq( + :current_state => 'running_migration_playbook', + :current_description => 'Running pre-migration playbook', + :percent => 17.0, + :states => { + :waiting_for_ip_address => { + :description => 'Waiting for VM IP address', + :state => 'finished', + :status => 'Ok', + :started_on => Time.now.utc - 10.minutes, + :updated_on => Time.now.utc - 5.minutes, + :percent => 100.0 + }, + :running_migration_playbook => { + :description => 'Running pre-migration playbook', + :state => 'finished', + :status => 'Ok', + :started_on => Time.now.utc - 1.minute, + :percent => 100.0, + :updated_on => Time.now.utc + } } - } - ) + ) + end end - end - it 'updates the task progress hash on error' do - Timecop.freeze(2019, 2, 6) do - progress = { - :current_state => 'running_in_automate', - :percent => 10.0, - :states => { - :running_in_automate => { - :state => 'active', - :status => 'Ok', - :started_on => Time.now.utc - 1.minute, - :percent => 10.0, - :updated_on => Time.now.utc - 30.seconds + it 'updates the task progress hash on error' do + Timecop.freeze(2019, 2, 6) do + progress = { + :current_state => 'running_migration_playbook', + :current_description => 'Running pre-migration playbook', + :percent => 6.5, + :states => { + :waiting_for_ip_address => { + :description => 'Waiting for VM IP address', + :state => 'finished', + :status => 'Ok', + :started_on => Time.now.utc - 10.minutes, + :updated_on => Time.now.utc - 5.minutes, + :percent => 100.0 + }, + :running_migration_playbook => { + :description => 'Running pre-migration playbook', + :state => 'active', + :status => 'Ok', + :started_on => Time.now.utc - 1.minute, + :percent => 30.0, + :updated_on => Time.now.utc + } } } - } - task.update_options(:progress => progress) - job.update_migration_task_progress(:on_error) - expect(task.reload.options[:progress]).to eq( - :current_state => 'running_in_automate', - :percent => 10.0, - :states => { - :running_in_automate => { - :state => 'finished', - :status => 'Error', - :started_on => Time.now.utc - 1.minute, - :percent => 10.0, - :updated_on => Time.now.utc + task.update_options(:progress => progress) + job.update_migration_task_progress(:on_error) + expect(task.reload.options[:progress]).to eq( + :current_state => 'running_migration_playbook', + :current_description => 'Running pre-migration playbook', + :percent => 6.5, + :states => { + :waiting_for_ip_address => { + :description => 'Waiting for VM IP address', + :state => 'finished', + :status => 'Ok', + :started_on => Time.now.utc - 10.minutes, + :updated_on => Time.now.utc - 5.minutes, + :percent => 100.0 + }, + :running_migration_playbook => { + :description => 'Running pre-migration playbook', + :state => 'finished', + :status => 'Error', + :started_on => Time.now.utc - 1.minute, + :percent => 30.0, + :updated_on => Time.now.utc + } } - } - ) + ) + end end end