Skip to content

Commit

Permalink
for Windows platform
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Jul 17, 2013
1 parent a4946e3 commit 81d6633
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 30 deletions.
7 changes: 6 additions & 1 deletion lib/fluent/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ def initialize(out=STDERR, level=LEVEL_TRACE)
@self_event = false
@tag = 'fluent'
@time_format = '%Y-%m-%d %H:%M:%S %z '
enable_color out.tty?
if $platformwin == false

This comment has been minimized.

Copy link
@repeatedly

repeatedly Jul 22, 2013

Member

hoge == false is redundant so unless $platform or changing order of if $platform is better.

enable_color out.tty?
else
enable_color false
end

# TODO: This variable name is unclear so we should change to better name.
@threads_exclude_events = []
end
Expand Down
58 changes: 53 additions & 5 deletions lib/fluent/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ def on_notify
while @rotate_queue.first.ready?
if io = @rotate_queue.first.io
stat = io.stat
if $platformwin == false

This comment has been minimized.

Copy link
@repeatedly

repeatedly Jul 22, 2013

Member

ditto

inode = stat.ino
else
inode = Win32File.getfileindex(io.path)
end
if inode == @pe.read_inode
# rotated file has the same inode number with the last file.
# assuming following situation:
Expand Down Expand Up @@ -188,7 +192,11 @@ def on_rotate(io)
# first time
stat = io.stat
fsize = stat.size
inode = stat.ino
if $platformwin == false
inode = stat.ino
else
inode = Win32File.getfileindex(io.path)
end

last_inode = @pe.read_inode
if inode == last_inode
Expand Down Expand Up @@ -301,10 +309,18 @@ def on_notify

begin
while true
if @buffer.empty?
@io.read_nonblock(2048, @buffer)
if $platformwin == false
if @buffer.empty?
@io.read_nonblock(2048, @buffer)
else
@buffer << @io.read_nonblock(2048, @iobuf)
end
else
@buffer << @io.read_nonblock(2048, @iobuf)
if @buffer.empty?
@io.readpartial(2048, @buffer)
else
@buffer << @io.readpartial(2048, @buffer)
end
end
while line = @buffer.slice!(/.*?\n/m)
lines << line
Expand Down Expand Up @@ -363,7 +379,11 @@ def on_notify
begin
io = File.open(@path)
stat = io.stat
inode = stat.ino
if $platformwin == false
inode = stat.ino
else
inode = Win32File.getfileindex(io.path)
end
fsize = stat.size
rescue Errno::ENOENT
# moved or deleted
Expand Down Expand Up @@ -492,4 +512,32 @@ def read_inode
end
end
end
#temprary code
require 'Win32API'
class Win32File
def initialize
super
end

def Win32File.getfileindex(path)
createfile = Win32API.new('kernel32', 'CreateFile', %w(p i i i i i i), 'i')

This comment has been minimized.

Copy link
@repeatedly

repeatedly Jul 22, 2013

Member

In ruby, create_file is popular thant createfile

closehandle = Win32API.new('kernel32', 'CloseHandle', 'i', 'v')
getFileInformation = Win32API.new('kernel32', 'GetFileInformationByHandle', %w(i p), 'i')

file_handle = createfile.call(path, 0, 0, 0, 3, 0x80, 0 )
if file_handle == -1
return 0
end

by_handle_file_information = '\0'*(4+8+8+8+4+4+4+4+4+4) #72bytes
ret = getFileInformation.call(file_handle, by_handle_file_information)
closehandle.call(file_handle)
if ret == 0
return 0
end

return by_handle_file_information.unpack("I11Q1")[11]
end
end

end
81 changes: 57 additions & 24 deletions lib/fluent/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
module Fluent


$usespawn = 0

This comment has been minimized.

Copy link
@repeatedly

repeatedly Jul 22, 2013

Member

should be moved to Supervisor's member variable and accessor or something.
We should not use global variable in Ruby.

class Supervisor
class LoggerInitializer
def initialize(path, level, chuser, chgroup)
Expand Down Expand Up @@ -69,7 +69,12 @@ def initialize(opt)
@inline_config = opt[:inline_config]
@suppress_interval = opt[:suppress_interval]
@dry_run = opt[:dry_run]
$usespawn = opt[:usespawn]

$platformwin = false

This comment has been minimized.

Copy link
@repeatedly

repeatedly Jul 22, 2013

Member

ditto

if RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|cygwin|bccwin/
$platformwin = true
end
@log = LoggerInitializer.new(@log_path, @log_level, @chuser, @chgroup)
@finished = false
@main_pid = nil
Expand Down Expand Up @@ -168,9 +173,25 @@ def supervise(&block)
start_time = Time.now

$log.info "starting fluentd-#{Fluent::VERSION}"
$log.info "is windows platform : #{$platformwin}"

if $platformwin == false
@main_pid = fork do
main_process(&block)
end
else
if $usespawn == 0 then

This comment has been minimized.

Copy link
@repeatedly

repeatedly Jul 22, 2013

Member

then is not needed

flunetd_spawn_cmd = "fluentd "
$fluentdargv.each{|a|
flunetd_spawn_cmd << (a + " ")
}
flunetd_spawn_cmd << "-u"
@main_pid = Process.spawn(flunetd_spawn_cmd)
else
@main_pid = Process.pid
main_process(&block)
end
end

if @daemonize && @wait_daemonize_pipe_w
STDIN.reopen("/dev/null")
Expand All @@ -180,7 +201,13 @@ def supervise(&block)
@wait_daemonize_pipe_w = nil
end

Process.waitpid(@main_pid)
if $platform == false
Process.waitpid(@main_pid)
else
if $usespawn == 0
Process.waitpid(@main_pid)
end
end
@main_pid = nil
ecode = $?.to_i

Expand Down Expand Up @@ -249,25 +276,28 @@ def install_supervisor_signal_handlers
end
end

trap :HUP do
$log.debug "fluentd supervisor process get SIGHUP"
$log.info "restarting"
if pid = @main_pid
Process.kill(:TERM, pid)
# don't resuce Erro::ESRSH here (invalid status)
if $platformwin == false
trap :HUP do
$log.debug "fluentd supervisor process get SIGHUP"
$log.info "restarting"
if pid = @main_pid
Process.kill(:TERM, pid)
# don't resuce Erro::ESRSH here (invalid status)
end
end
end

trap :USR1 do
$log.debug "fluentd supervisor process get SIGUSR1"
@log.reopen!
if pid = @main_pid
Process.kill(:USR1, pid)
# don't resuce Erro::ESRSH here (invalid status)
if $platformwin == false
trap :USR1 do
$log.debug "fluentd supervisor process get SIGUSR1"
@log.reopen!
if pid = @main_pid
Process.kill(:USR1, pid)
# don't resuce Erro::ESRSH here (invalid status)
end
end
end
end

def read_config
$log.info "reading config file", :path=>@config_path
@config_fname = File.basename(@config_path)
Expand Down Expand Up @@ -356,16 +386,19 @@ def install_main_process_signal_handlers
end
end

trap :HUP do
# TODO
$log.debug "fluentd main process get SIGHUP"
if $platformwin == false
trap :HUP do
# TODO
$log.debug "fluentd main process get SIGHUP"
end
end

trap :USR1 do
$log.debug "fluentd main process get SIGUSR1"
$log.info "force flushing buffered events"
@log.reopen!
Fluent::Engine.flush!
if $platformwin == false
trap :USR1 do
$log.debug "fluentd main process get SIGUSR1"
$log.info "force flushing buffered events"
@log.reopen!
Fluent::Engine.flush!
end
end
end

Expand Down

0 comments on commit 81d6633

Please sign in to comment.