Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #30870 - Option to delete paused task. #407

Merged
merged 8 commits into from Jan 8, 2021
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()
patilsuraj767 marked this conversation as resolved.
Show resolved Hide resolved
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})"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you have taken state of tasks into consideration.
not_paused check calls feature(:foreman_tasks).paused_tasks_count(ignored_tasks) to find specific paused tasks.
I think, deleting only such paused tasks where result=error and not having label from ignored_tasks is expected here. Could you confirm this behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kgaikwad Yes it is expected that we should not delete ignored_tasks
These tasks are meant to be running always.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kgaikwad @patilsuraj767 Currently we delete tasks where result=error, what about the tasks where result is pending or delayed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jameerpathan111 There is another check in foreman-maintain. Check not_running task which is run after the check not_paused in upgrade workflow, it will take care of running/pending tasks.

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
patilsuraj767 marked this conversation as resolved.
Show resolved Hide resolved
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)
patilsuraj767 marked this conversation as resolved.
Show resolved Hide resolved
@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