Skip to content

Commit

Permalink
Fixes #30870 - Option to delete paused task. (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
patilsuraj767 committed Jan 8, 2021
1 parent 2a12bca commit 616c23c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 28 deletions.
23 changes: 10 additions & 13 deletions definitions/checks/foreman_tasks/not_paused.rb
Expand Up @@ -9,23 +9,20 @@ class NotPaused < ForemanMaintain::Check
end

def run
paused_tasks_count = feature(:foreman_tasks).paused_tasks_count(ignored_tasks)
paused_tasks_count = feature(:foreman_tasks).paused_tasks_count()
assert(paused_tasks_count == 0,
"There are currently #{paused_tasks_count} paused tasks in the system",
:next_steps =>
[Procedures::ForemanTasks::Resume.new,
Procedures::ForemanTasks::UiInvestigate.new('search_query' => scoped_search_query)])
:next_steps => next_procedures)
end

# Note: this is for UI link generation only: we are not using scoped search for querying
# the tasks itself as we use direct SQL instead
def scoped_search_query
"state = paused AND label !^(#{ignored_tasks.join(' ')})"
end

def ignored_tasks
%w[Actions::Candlepin::ListenOnCandlepinEvents
Actions::Katello::EventQueue::Monitor]
def next_procedures
if assumeyes?
return [Procedures::ForemanTasks::Resume.new,
Procedures::ForemanTasks::Delete.new(:state => :paused)]
end
[Procedures::ForemanTasks::Resume.new,
Procedures::ForemanTasks::Delete.new(:state => :paused),
Procedures::ForemanTasks::UiInvestigate.new('search_query' => 'state = paused')]
end
end
end
8 changes: 7 additions & 1 deletion definitions/features/foreman_tasks.rb
Expand Up @@ -89,6 +89,8 @@ def condition(state)

if state == :old
old_tasks_condition
elsif state == :paused
paused_tasks_condition
else
tasks_condition(state)
end
Expand Down Expand Up @@ -180,6 +182,10 @@ def old_tasks_condition(state = "'stopped', 'paused'")
"foreman_tasks_tasks.started_at < CURRENT_DATE - INTERVAL '#{MIN_AGE} days'"
end

def paused_tasks_condition(state = "'paused'")
"foreman_tasks_tasks.state IN (#{state})"
end

def prepare_for_backup(state)
dir = backup_dir(state)
execute("mkdir -p #{dir}")
Expand All @@ -197,6 +203,6 @@ def tasks_condition(state)
end

def valid(state)
%w[old planning pending].include?(state.to_s)
%w[old planning pending paused].include?(state.to_s)
end
end
7 changes: 3 additions & 4 deletions definitions/procedures/foreman_tasks/delete.rb
@@ -1,6 +1,6 @@
module Procedures::ForemanTasks
class Delete < ForemanMaintain::Procedure
ALLOWED_STATES_VALUES = %w[old planning pending].freeze
ALLOWED_STATES_VALUES = %w[old planning pending paused].freeze

metadata do
param :state,
Expand All @@ -19,12 +19,11 @@ def run
feature(:foreman_tasks).backup_tasks(@state) do |backup_progress|
spinner.update backup_progress
end

spinner.update "Deleting #{@state} tasks [running]"
spinner.update "Deleting #{count_tasks_before} #{@state} tasks [running]"
count_tasks_later = feature(:foreman_tasks).delete(@state)
spinner.update "Deleting #{@state} tasks [DONE]"
spinner.update(
"Deleted #{@state} stopped and paused tasks: #{count_tasks_before - count_tasks_later}"
"Deleted #{@state} tasks: #{count_tasks_before - count_tasks_later}"
)
end
end
Expand Down
5 changes: 5 additions & 0 deletions definitions/procedures/foreman_tasks/resume.rb
Expand Up @@ -6,8 +6,13 @@ class Resume < ForemanMaintain::Procedure
description 'Resume paused tasks'
end

WAIT_TIME = 30

def run
output << feature(:foreman_tasks).resume_task_using_hammer
with_spinner('Waiting 30 seconds for resumed tasks to start.') do
sleep WAIT_TIME
end
end
end
end
8 changes: 6 additions & 2 deletions lib/foreman_maintain/reporter/cli_reporter.rb
Expand Up @@ -67,6 +67,7 @@ def print_current_line
end
end

attr_accessor :select_option_counter
attr_reader :last_line, :max_length

def initialize(stdout = STDOUT, stdin = STDIN, options = {})
Expand All @@ -81,6 +82,7 @@ def initialize(stdout = STDOUT, stdin = STDIN, options = {})
@spinner = Spinner.new(self)
@spinner.start_spinner if @stdout.tty?
@last_line = ''
@select_option_counter = 0
end

def before_scenario_starts(scenario)
Expand Down Expand Up @@ -212,8 +214,10 @@ def filter_decision(answer)
# rubocop:disable Metrics/MethodLength
def ask_to_select(message, steps, run_strategy)
if assumeyes?
puts('(assuming first option)')
return steps.first
step = steps[@select_option_counter]
@select_option_counter += 1
puts("(assuming option #{@select_option_counter})")
return step
end

until_valid_decision do
Expand Down
13 changes: 8 additions & 5 deletions lib/foreman_maintain/runner.rb
Expand Up @@ -18,6 +18,7 @@ def initialize(reporter, scenarios, options = {})
@last_scenario = nil
@last_scenario_continuation_confirmed = false
@exit_code = 0
@procedure_step_counter = 0
end

def quit?
Expand Down Expand Up @@ -143,14 +144,16 @@ def post_step_decisions(scenario, execution)

# rubocop:disable Metrics/MethodLength
def ask_about_offered_steps(step, scenario)
if assumeyes? && rerun_check?(step)
@reporter.puts 'Check still failing after attempt to fix. Skipping'
return :no
end
if step.next_steps && !step.next_steps.empty?
@last_decision_step = step
@procedure_step_counter += 1
steps = step.next_steps.map(&:ensure_instance)

if assumeyes? && @procedure_step_counter > steps.length
@procedure_step_counter = 0
@reporter.select_option_counter = 0
@reporter.puts 'Check still failing after attempt to fix. Skipping'
return :no
end
decision = @reporter.on_next_steps(steps, scenario.run_strategy)
case decision
when :quit
Expand Down
3 changes: 2 additions & 1 deletion test/definitions/checks/foreman_tasks/not_paused_test.rb
Expand Up @@ -18,7 +18,8 @@
result = run_check(subject)
assert result.fail?, 'Check expected to fail'
assert_match 'There are currently 5 paused tasks in the system', result.output
assert_equal [Procedures::ForemanTasks::Resume, Procedures::ForemanTasks::UiInvestigate],
assert_equal [Procedures::ForemanTasks::Resume, Procedures::ForemanTasks::Delete,
Procedures::ForemanTasks::UiInvestigate],
subject.next_steps.map(&:class)
end
end
2 changes: 1 addition & 1 deletion test/lib/reporter_test.rb
Expand Up @@ -184,7 +184,7 @@ def decision_question(description)
There are multiple steps to proceed:
1) Start the present service
2) Restart present service
(assuming first option)
(assuming option 1)
STR
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/lib/support/log_reporter.rb
@@ -1,7 +1,7 @@
class Support
class LogReporter < ForemanMaintain::Reporter
attr_reader :log, :output, :input, :executions
attr_accessor :planned_next_steps_answers
attr_accessor :planned_next_steps_answers, :select_option_counter

def initialize(options = {})
options.validate_options!(:assumeyes)
Expand Down

0 comments on commit 616c23c

Please sign in to comment.