class SimpleRotate::ProcessSync

Public Class Methods

new(sr) click to toggle source
# File lib/simple_rotate/internal/process_sync.rb, line 18
def initialize(sr)
    @sr       = sr
    @enable   = sr.instance_variable_defined?(:@is_psync) ? sr.instance_variable_get(:@is_psync) : nil
    file_name = sr.instance_variable_defined?(:@file_name) ? sr.instance_variable_get(:@file_name) : nil

    # #init not called
    return self if file_name == nil

    @try_limit   = 3
    @@tempf_name = File.dirname(file_name) + File::SEPARATOR + ".simple_rotate_tempfile_#{File.basename($0)}"
    # replace whitespaces
    @@tempf_name.gsub!(/\s/, '_')

    create_tempfile if @enable && !@@scheduled_del_lockfile
end

Private Instance Methods

close_temp_file() click to toggle source
# File lib/simple_rotate/internal/process_sync.rb, line 103
def close_temp_file
    if @@tempf.is_a?(IO) && !@@tempf.closed?
        begin
            @@tempf.close
        rescue
            SimpleRotate::Error.warning("Couldn't close temp file => #{@@tempf_name}")
        end
    end
end
create_tempfile() click to toggle source

Create the temp file for locking

# File lib/simple_rotate/internal/process_sync.rb, line 36
def create_tempfile
    begin
        if tempf_exists?
            set_delete_tempfile
        else
            @@tempf = File.open(@@tempf_name, File::RDWR|File::CREAT|File::EXCL)
            set_delete_tempfile
        end

    rescue
        SimpleRotate::Error.warning("Failed to create temp file => #{@@tempf_name}")
    end
end
delete_at_end() click to toggle source
# File lib/simple_rotate/internal/process_sync.rb, line 75
def delete_at_end
    at_exit do
        begin
            File.delete(@@tempf_name)
        rescue
            SimpleRotate::Error.warning("Failed to delete temp file => #{@@tempf_name}")
        end
    end
end
open_temp_file() click to toggle source
# File lib/simple_rotate/internal/process_sync.rb, line 92
def open_temp_file
    if @@tempf.is_a?(IO) && @@tempf.closed? || !@@tempf.is_a?(IO)
        begin
            @@tempf = File.open(@@tempf_name, File::RDWR|File::CREAT|File::APPEND)
        rescue
            SimpleRotate::Error.warning("Failed to open temp file => #{@@tempf_name}")
        end
    end
end
reopen_temp_file() click to toggle source
# File lib/simple_rotate/internal/process_sync.rb, line 86
def reopen_temp_file
    close_temp_file
    open_temp_file
end
set_delete_tempfile() click to toggle source

Delete the lock file at the end of the script

# File lib/simple_rotate/internal/process_sync.rb, line 57
def set_delete_tempfile
    return true if @@scheduled_del_lockfile

    if tempf_exists?
        # is it empty?
        if File.size(@@tempf_name) == 0
            delete_at_end
        else
            # it is not empty
            msg  = "File is not empty => #{@@tempf_name}#{$-0}"
            msg += "Skip to delete temp file!"
            SimpleRotate::Error.warning(msg)
        end
    end
    @@scheduled_del_lockfile = true
end
tempf_exists?() click to toggle source
# File lib/simple_rotate/internal/process_sync.rb, line 51
def tempf_exists?
    return File.exist?(@@tempf_name)
end