Skip to content

Commit

Permalink
add "no running tasks" check for Pulpcore
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed Jun 17, 2024
1 parent f0e78f7 commit e90ef3a
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
32 changes: 32 additions & 0 deletions definitions/checks/pulpcore/no_running_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Checks::Pulpcore
class NoRunningTasks < ForemanMaintain::Check
metadata do
for_feature :pulpcore
description 'Check for running pulpcore tasks'
tags :pre_upgrade
param :wait_for_tasks,
'Wait for tasks to finish or fail directly',
:required => false
end

def run
tasks = feature(:pulpcore).running_tasks
assert(
tasks.empty?,
failure_message(tasks.length),
:next_steps => calculate_next_steps
)
end

private

def failure_message(task_count)
"There are #{task_count} active task(s) in the system." \
"\nPlease wait for these to complete."
end

def calculate_next_steps
@wait_for_tasks ? [Procedures::Pulpcore::WaitForTasks.new] : []
end
end
end
24 changes: 24 additions & 0 deletions definitions/features/pulpcore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,34 @@
class Features::Pulpcore < ForemanMaintain::Feature
include ForemanMaintain::Concerns::PulpCommon

TIMEOUT_FOR_TASKS_STATUS = 300
RETRY_INTERVAL_FOR_TASKS_STATE = 10

metadata do
label :pulpcore
end

def cli(args)
parse_json(execute("pulp --format json #{args}"))
end

def running_tasks
cli('task list --state-in running --state-in canceling')
end

def wait_for_tasks(spinner, timeout_for_tasks_status = TIMEOUT_FOR_TASKS_STATUS)
Timeout.timeout(timeout_for_tasks_status) do
while (task_count = running_tasks.length) != 0
puts "\nThere are #{task_count} tasks."
spinner.update "Waiting #{RETRY_INTERVAL_FOR_TASKS_STATE} seconds before retry."
sleep RETRY_INTERVAL_FOR_TASKS_STATE
end
end
rescue Timeout::Error => e
logger.error(e.message)
puts "\nTimeout: #{e.message}. Try again."
end

def services
redis_services = feature(:redis) ? feature(:redis).services : []

Expand Down
15 changes: 15 additions & 0 deletions definitions/procedures/pulpcore/wait_for_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Procedures::Pulpcore
class WaitForTasks < ForemanMaintain::Procedure
metadata do
for_feature :pulpcore
description 'Fetch tasks status and wait till they finish'
advanced_run false
end

def run
with_spinner("waiting for tasks to finish") do |spinner|
feature(:pulpcore).wait_for_tasks(spinner)
end
end
end
end
50 changes: 50 additions & 0 deletions test/definitions/checks/pulpcore/no_running_tasks_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'test_helper'

describe Checks::Pulpcore::NoRunningTasks do
include DefinitionsTestHelper

context 'with default params' do
subject do
Checks::Pulpcore::NoRunningTasks.new
end

it 'passes when not active tasks are present' do
assume_feature_present(:pulpcore, :running_tasks => [])
result = run_check(subject)
assert result.success?, 'Check expected to succeed'
end

it 'fails when running/paused tasks are present' do
assume_feature_present(:pulpcore, :running_tasks => ['a_task'])
result = run_check(subject)
assert result.fail?, 'Check expected to fail'
msg = 'There are 1 active task(s) in the system.'
msg += "\nPlease wait for these to complete."
assert_match msg, result.output
assert_empty subject.next_steps.map(&:class)
end
end

context 'with wait_for_tasks=>true' do
subject do
Checks::Pulpcore::NoRunningTasks.new(:wait_for_tasks => true)
end

it 'passes when not active tasks are present' do
assume_feature_present(:pulpcore, :running_tasks => [])
result = run_check(subject)
assert result.success?, 'Check expected to succeed'
end

it 'fails when running/paused tasks are present' do
assume_feature_present(:pulpcore, :running_tasks => ['a_task'])
result = run_check(subject)
assert result.fail?, 'Check expected to fail'
msg = 'There are 1 active task(s) in the system.'
msg += "\nPlease wait for these to complete."
assert_match msg, result.output
assert_equal [Procedures::Pulpcore::WaitForTasks],
subject.next_steps.map(&:class)
end
end
end

0 comments on commit e90ef3a

Please sign in to comment.