Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add log dir lock and unlock #317

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lib/roby/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,8 @@ def log_setup(mod_path, level, file = nil)
"engine" => {}
}.freeze

LOCK_FILE_EXT = ".lock"

# @!method public_rest_interface?
# @!method public_rest_interface=(flag)
#
Expand Down Expand Up @@ -955,6 +957,8 @@ def initialize(plan: ExecutablePlan.new)
@robots = App::RobotNames.new

@base_setup_done = false

@lock_file = nil
end

# Loads the base configuration
Expand Down Expand Up @@ -1031,7 +1035,9 @@ def base_setup

load_base_config
unless @log_dir
unlock_log_dir
find_and_create_log_dir
lock_log_dir
end
setup_loggers(redirections: true)

Expand All @@ -1041,6 +1047,44 @@ def base_setup
@base_setup_done = true
end

def lock_log_dir
return if log_dir_locked?

return unless @lock_file

@lock_file ||= File.open(
File.join(@log_dir, LOCK_FILE_EXT),
File::RDWR | File::CREAT, 0o644
)
@lock_file.flock(File::LOCK_EX | File::LOCK_NB)
rescue Errno::ENOENT, Errno::EACCES, RuntimeError => e
warn "Failed to lock directory #{@log_dir}: #{e.message}"
end

def unlock_log_dir
return unless log_dir_locked?

return unless @lock_file

@lock_file.flock(File::LOCK_UN)
@lock_file.close
@lock_file = nil
rescue Errno::ENOENT, Errno::EACCES, RuntimeError => e
warn "Failed to unlock directory #{@log_dir}: " \
"#{e.message}"
end

def log_dir_locked?
return unless @lock_file

File.open(@lock_file) do |file|
return !file.flock(File::LOCK_EX | File::LOCK_NB)
end
rescue Errno::ENOENT, Errno::EACCES => e
warn "Failed to check lock status for log directory " \
"#{@log_dir}: #{e.message}"
end

# The inverse of #base_setup
def base_cleanup
unless base_setup_done?
Expand Down