Skip to content

Instantly share code, notes, and snippets.

@ahsandar
Created June 5, 2020 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahsandar/51a2666954b314cc3194f9e9cbe20515 to your computer and use it in GitHub Desktop.
Save ahsandar/51a2666954b314cc3194f9e9cbe20515 to your computer and use it in GitHub Desktop.
backgrond job processing suing ruby process library uses ahsandar/fury to run shell commands
module ForkDetach
def chk_download_complete(job_list)
loop do
process_output = []
job_list.each do | job|
process_output << format_process_output(Fury.run_now("ps ho pid,state -p #{job}"))
end
break if wait_process_completion(process_output)
end
true
end
def wait_process_completion(process_output)
sleep_time = 30
puts "#{Time.now}: going to sleep for #{sleep_time}"
sleep sleep_time
puts "#{Time.now}: awake going to check again"
process_output.reject { |e| e.to_s.empty? }.empty?
end
def format_process_output(output)
return output if output.nil?
ps_output = output.gsub("PID STAT\n",'')
return (ps_output.nil? ? output.chop.strip : ps_output.chop.strip)
end
def fork_detach(folder, file)
job = fork do
Fury.run_now("s3cmd --config=<s3_config file> get --force <s3 bucket> <local dir>")
end
Process.detach(job)
job
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment