Skip to content

Commit

Permalink
Use INotify to wait for ansible-runner pid file
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Oct 7, 2020
1 parent 30a0e1a commit 60d2f2a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ gem "query_relation", "~>0.1.0", :require => false
gem "rails", "~>5.2.4", ">=5.2.4.4"
gem "rails-i18n", "~>5.x"
gem "rake", ">=12.3.3", :require => false
gem "rb-inotify", "~>0.10.0", :require => false
gem "rest-client", "~>2.1.0", :require => false
gem "ripper_ruby_parser", "~>1.5.1", :require => false
gem "ruby-progressbar", "~>1.7.0", :require => false
Expand Down
37 changes: 36 additions & 1 deletion lib/ansible/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ def run_via_cli(hosts, credentials, env_vars, extra_vars, tags: nil, ansible_run

begin
fetch_galaxy_roles(playbook_or_role_args)
result = AwesomeSpawn.run("ansible-runner", :env => env_vars_hash, :params => params)

result = wait_for(pid_file(base_dir)) do
AwesomeSpawn.run("ansible-runner", :env => env_vars_hash, :params => params)
end

res = response(base_dir, ansible_runner_method, result)
ensure
# Clean up the tmp dir for the sync method, for async we will clean it up after the job is finished and we've
Expand Down Expand Up @@ -354,6 +358,37 @@ def env_dir(base_dir)
FileUtils.mkdir_p(File.join(base_dir, "env")).first
end

def pid_file(base_dir)
File.join(base_dir, "pid")
end

def wait_for(path)
require "rb-inotify"
dir_name = File.dirname(path)
file_name = File.basename(path)

path_created = Concurrent::Event.new

notifier = INotify::Notifier.new
notifier.watch(dir_name, :moved_to, :create) do |event|
# Once the file we are looking for is created set the concurrent event
path_created.set if event.name == file_name
end

thread = Thread.new { notifier.run }

res = yield

# Wait for the target file to exist
path_created.wait

# Shutdown the inotify loop and join the thread
notifier.close
thread.join

res
end

PYTHON2_MODULE_PATHS = %w[
/var/lib/manageiq/venv/lib/python2.7/site-packages
].freeze
Expand Down

0 comments on commit 60d2f2a

Please sign in to comment.