-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Enable combination daemon and no supervisor #2912
Merged
ganmacs
merged 3 commits into
fluent:master
from
ganmacs:enable-combination-daemon-and-no-supervisor
Mar 31, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# | ||
# Fluentd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'fluent/config/error' | ||
|
||
module Fluent | ||
class Daemonizer | ||
def self.daemonize(pid_path, args = [], &block) | ||
new.daemonize(pid_path, args, &block) | ||
end | ||
|
||
def daemonize(pid_path, args = []) | ||
pid_fullpath = File.absolute_path(pid_path) | ||
check_pidfile(pid_fullpath) | ||
|
||
begin | ||
Process.daemon(false, false) | ||
|
||
File.write(pid_fullpath, Process.pid.to_s) | ||
|
||
# install signal and set process name are performed by supervisor | ||
install_at_exit_handlers(pid_fullpath) | ||
|
||
yield | ||
rescue NotImplementedError | ||
daemonize_with_spawn(pid_fullpath, args) | ||
end | ||
end | ||
|
||
private | ||
|
||
def daemonize_with_spawn(pid_fullpath, args) | ||
pid = Process.spawn(*['fluentd'].concat(args)) | ||
|
||
File.write(pid_fullpath, pid.to_s) | ||
|
||
pid | ||
end | ||
|
||
def check_pidfile(pid_path) | ||
if File.exist?(pid_path) | ||
if !File.readable?(pid_path) || !File.writable?(pid_path) | ||
raise Fluent::ConfigError, "Cannot access pid file: #{pid_path}" | ||
end | ||
|
||
pid = | ||
begin | ||
Integer(File.read(pid_path), 10) | ||
rescue TypeError, ArgumentError | ||
return # ignore | ||
end | ||
|
||
begin | ||
Process.kill(0, pid) | ||
raise Fluent::ConfigError, "pid(#{pid}) is running" | ||
rescue Errno::EPERM | ||
raise Fluent::ConfigError, "pid(#{pid}) is running" | ||
rescue Errno::ESRCH | ||
end | ||
else | ||
unless File.writable?(File.dirname(pid_path)) | ||
raise Fluent::ConfigError, "Cannot access directory for pid file: #{File.dirname(pid_path)}" | ||
end | ||
end | ||
end | ||
|
||
def install_at_exit_handlers(pidfile) | ||
at_exit do | ||
if File.exist?(pidfile) | ||
File.delete(pidfile) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
require_relative 'helper' | ||
require 'fluent/daemonizer' | ||
|
||
class DaemonizerTest < ::Test::Unit::TestCase | ||
TMP_DIR = File.join(File.dirname(__FILE__), 'tmp', 'daemonizer') | ||
|
||
setup do | ||
FileUtils.mkdir_p(TMP_DIR) | ||
end | ||
|
||
teardown do | ||
FileUtils.rm_rf(TMP_DIR) rescue nil | ||
end | ||
|
||
test 'makes pid file' do | ||
pid_path = File.join(TMP_DIR, 'file.pid') | ||
|
||
mock(Process).daemon(anything, anything).once | ||
r = Fluent::Daemonizer.daemonize(pid_path) { 'ret' } | ||
assert_equal 'ret', r | ||
assert File.exist?(pid_path) | ||
assert Process.pid.to_s, File.read(pid_path).to_s | ||
end | ||
|
||
test 'in platforms which do not support fork' do | ||
pid_path = File.join(TMP_DIR, 'file.pid') | ||
|
||
mock(Process).daemon(anything, anything) { raise NotImplementedError } | ||
args = ['-c', 'test.conf'] | ||
mock(Process).spawn(anything, *args) { Process.pid } | ||
|
||
Fluent::Daemonizer.daemonize(pid_path, args) { 'ret' } | ||
assert File.exist?(pid_path) | ||
assert Process.pid.to_s, File.read(pid_path).to_s | ||
end | ||
|
||
sub_test_case 'when pid file already exists' do | ||
test 'raise an error when process is running' do | ||
omit 'chmod of file does not affetct root user' if Process.uid.zero? | ||
pid_path = File.join(TMP_DIR, 'file.pid') | ||
File.write(pid_path, '1') | ||
|
||
mock(Process).daemon(anything, anything).never | ||
mock(Process).kill(0, 1).once | ||
|
||
assert_raise(Fluent::ConfigError.new('pid(1) is running')) do | ||
Fluent::Daemonizer.daemonize(pid_path) { 'ret' } | ||
end | ||
end | ||
|
||
test 'raise an error when file is not redable' do | ||
omit 'chmod of file does not affetct root user' if Process.uid.zero? | ||
not_readable_path = File.join(TMP_DIR, 'not_readable.pid') | ||
|
||
File.write(not_readable_path, '1') | ||
FileUtils.chmod(0333, not_readable_path) | ||
|
||
mock(Process).daemon(anything, anything).never | ||
assert_raise(Fluent::ConfigError.new("Cannot access pid file: #{File.absolute_path(not_readable_path)}")) do | ||
Fluent::Daemonizer.daemonize(not_readable_path) { 'ret' } | ||
end | ||
end | ||
|
||
test 'raise an error when file is not writable' do | ||
omit 'chmod of file does not affetct root user' if Process.uid.zero? | ||
not_writable_path = File.join(TMP_DIR, 'not_writable.pid') | ||
|
||
File.write(not_writable_path, '1') | ||
FileUtils.chmod(0555, not_writable_path) | ||
|
||
mock(Process).daemon(anything, anything).never | ||
assert_raise(Fluent::ConfigError.new("Cannot access pid file: #{File.absolute_path(not_writable_path)}")) do | ||
Fluent::Daemonizer.daemonize(not_writable_path) { 'ret' } | ||
end | ||
end | ||
|
||
test 'raise an error when directory is not writable' do | ||
omit 'chmod of file does not affetct root user' if Process.uid.zero? | ||
not_writable_dir = File.join(TMP_DIR, 'not_writable') | ||
pid_path = File.join(not_writable_dir, 'file.pid') | ||
|
||
FileUtils.mkdir_p(not_writable_dir) | ||
FileUtils.chmod(0555, not_writable_dir) | ||
|
||
mock(Process).daemon(anything, anything).never | ||
assert_raise(Fluent::ConfigError.new("Cannot access directory for pid file: #{File.absolute_path(not_writable_dir)}")) do | ||
Fluent::Daemonizer.daemonize(pid_path) { 'ret' } | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this exception happens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's for the environments that don't support fork(2) such as Windows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use
Fluent.windows?
branch instead ofrescue NotImplementedError
guard?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to use rescue instead of Fluent.windows? because using spawn here is not for windows but for the platforms that don’t support fork(2) and we now support many platforms. So I think rescue is proper in this context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got it. Thanks!