class Sleepier::ChildSpec

Specifies the properties of the child process to be launched

Attributes

args[RW]
child_id[RW]
pid[RW]
restart[RW]
shutdown[RW]
shutdown_timeout[RW]
start_func[RW]
terminating[RW]

Public Class Methods

new(child_id, start_func, args, restart, shutdown, is_supervisor=false, shutdown_timeout=0) click to toggle source

Create a new `ChildSpec`

@param child_id Unique id associated with this specification. This can be used to terminate the process @param start_func The function run by the process @param args [Array] The list of arguments passed to the function. If there are none, pass it an empty `Array` @param restart [VALID_RESTART_OPTIONS] One of the `VALID_RESTART_OPTIONS` that determines how to handle restarts @param shutdown [VALID_SHUTDOWN_OPTIONS] One of the `VALID_SHUTDOWN_OPTIONS` that determines how to shutdown the process @param is_supervisor [true, false] Is the child a supervisor itself?! Potentially useful for trees of supervisors @param shutdown_timeout [int] If `shutdown` is `:timeout`, this is how long to wait before brutally killing the process

# File lib/sleepier.rb, line 66
def initialize(child_id, start_func, args, restart, shutdown, is_supervisor=false, shutdown_timeout=0)
    @child_id = child_id
    @start_func = start_func
    @args = args
    @restart = restart
    @shutdown = shutdown
    @is_supervisor = is_supervisor
    @shutdown_timeout = shutdown_timeout
    @pid = nil

    # Array of timestamps of restarts.  Used for checking restarts
    @failures = Array.new

    # Used for
    @terminating = false
end

Public Instance Methods

allows_restart?(status_code) click to toggle source
# File lib/sleepier.rb, line 106
def allows_restart?(status_code)
    case self.restart
    when :permanent
        true
    when :temporary
        false
    when :transient
        if status_code == 0
            false
        else
            true
        end
    end
end
child?() click to toggle source
# File lib/sleepier.rb, line 87
def child?
    !@is_supervisor
end
restarted() click to toggle source

Used to notify the child spec that it has been restarted, and when. This allows tracking of how many recent restarts the child spec has had.

# File lib/sleepier.rb, line 123
def restarted
    @failures << Time.now.to_i
end
restarts_within_window(max_restart_window) click to toggle source

Counts how many restarts have happened within the last `max_restart_window` seconds, and clears out old failures.

# File lib/sleepier.rb, line 133
def restarts_within_window(max_restart_window)
    failures_within_window = 0
    now = Time.now.to_i
    new_failures = Array.new

    @failures.each do |f|
        if now - f < max_restart_window
            failures_within_window += 1
            new_failures << f
        end
    end

    # Update the failures array to only include things currently within the window
    @failures = new_failures

    # Return the current number of failures within the window
    failures_within_window
end
should_restart?(status_code, max_restart_count, max_restart_window) click to toggle source

Called by the supervisor to check whether the process should be restarted. Checks whether the process has been restarted more than `max_restart_count` times in `max_restart_window` seconds, and whether the `shutdown` type even allows restarts

@return [true, false]

# File lib/sleepier.rb, line 96
def should_restart?(status_code, max_restart_count, max_restart_window)
    if self.too_many_restarts?(max_restart_count, max_restart_window)
        false
    elsif self.allows_restart?(status_code) && !@terminating
        true
    else
        false
    end
end
supervisor?() click to toggle source
# File lib/sleepier.rb, line 83
def supervisor?
    @is_supervisor
end
too_many_restarts?(max_restart_count, max_restart_window) click to toggle source
# File lib/sleepier.rb, line 127
def too_many_restarts?(max_restart_count, max_restart_window)
    max_restart_count <= self.restarts_within_window(max_restart_window)
end