Skip to content

Commit

Permalink
Fix job exit logic (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzirtusss authored May 17, 2024
1 parent fe6493f commit 5b190f8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Changes since the last non-beta release.

_Please add entries here for your pull requests that have not yet been released._

- Fixed issue with improper handling of job statuses. Fixed issue with interactive magic string showing and exit code. [PR 177](https://github.com/shakacode/control-plane-flow/pull/177) by [Sergey Tarasov](https://github.com/dzirtusss).

## [2.0.1] - 2024-05-15

### Fixed
Expand Down
26 changes: 16 additions & 10 deletions lib/command/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def runner_script # rubocop:disable Metrics/MethodLength
script += interactive_runner_script if interactive

script +=
if @log_method == 1
if @log_method == 1 || @interactive
args_join(config.args)
else
<<~SCRIPT
Expand Down Expand Up @@ -442,16 +442,22 @@ def print_detached_commands
)
end

def resolve_job_status
result = cp.fetch_cron_workload(runner_workload, location: location)
job_details = result&.dig("items")&.find { |item| item["id"] == job }
status = job_details&.dig("status")
def resolve_job_status # rubocop:disable Metrics/MethodLength
loop do
result = cp.fetch_cron_workload(runner_workload, location: location)
job_details = result&.dig("items")&.find { |item| item["id"] == job }
status = job_details&.dig("status")

Shell.debug("JOB STATUS", status)

case status
when "failed"
ExitCode::ERROR_DEFAULT
when "successful"
ExitCode::SUCCESS
case status
when "active"
sleep 1
when "successful"
break ExitCode::SUCCESS
else
break ExitCode::ERROR_DEFAULT
end
end
end

Expand Down
18 changes: 16 additions & 2 deletions spec/command/run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,35 @@
it "clones workload and runs provided command with success", :slow do
result = nil

spawn_cpl_command("run", "-a", app, "--entrypoint", "none", "--", "ls") do |it|
spawn_cpl_command("run", "-a", app, "--entrypoint", "none", "--verbose", "--", "ls") do |it|
result = it.read_full_output
end

expect(result).to include("Gemfile")
expect(result).to include("[#{Shell.color('JOB STATUS', :red)}] successful")
end

it "clones workload and runs provided command with failure", :slow do
result = nil

spawn_cpl_command("run", "-a", app, "--entrypoint", "none", "--", "nonexistent") do |it|
spawn_cpl_command("run", "-a", app, "--entrypoint", "none", "--verbose", "--", "nonexistent") do |it|
result = it.read_full_output
end

expect(result).not_to include("Gemfile")
expect(result).to include("[#{Shell.color('JOB STATUS', :red)}] failed")
end

it "waits for job to finish", :slow do
result = nil

spawn_cpl_command("run", "-a", app, "--entrypoint", "none", "--verbose", "--", "sleep 10; ls") do |it|
result = it.read_full_output
end

expect(result).to include("Gemfile")
expect(result).to include("[#{Shell.color('JOB STATUS', :red)}] active")
expect(result).to include("[#{Shell.color('JOB STATUS', :red)}] successful")
end
end

Expand Down

0 comments on commit 5b190f8

Please sign in to comment.