Skip to content

Instantly share code, notes, and snippets.

@flarik
Created July 25, 2016 13:22
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 flarik/9b0a8b323a74e07a0eb97bd0de0a7d1e to your computer and use it in GitHub Desktop.
Save flarik/9b0a8b323a74e07a0eb97bd0de0a7d1e to your computer and use it in GitHub Desktop.
Reopen closed file descriptors
# https://github.com/collectiveidea/delayed_job/blob/ce88693429188a63793b16daaab67056a4e4e0bf/lib/delayed/worker.rb#L77
def files_to_reopen
unless @files_to_reopen
@files_to_reopen = []
ObjectSpace.each_object(File) do |file|
@files_to_reopen << file unless file.closed?
end
end
@files_to_reopen
end
def reopen_files
@files_to_reopen.each do |file|
begin
puts "reopening: #{file}"
file.reopen file.path, 'a+'
file.sync = true
rescue ::Exception => e # rubocop:disable HandleExceptions, RescueException
puts "oepsie, raise...", e
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment