Class: ParallelServer::Prefork

Inherits:
Object
  • Object
show all
Defined in:
lib/parallel_server/prefork.rb

Defined Under Namespace

Classes: Child, Conversation

Constant Summary collapse

DEFAULT_MIN_PROCESSES =
5
DEFAULT_MAX_PROCESSES =
20
DEFAULT_MAX_THREADS =
1
DEFAULT_STANDBY_THREADS =
5
DEFAULT_MAX_IDLE =
10
DEFAULT_MAX_USE =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = nil, port, opts = {}) ⇒ Prefork #initialize(socket, opts = {}) ⇒ Prefork #initialize(sockets, opts = {}) ⇒ Prefork

Returns a new instance of Prefork.

Overloads:

  • #initialize(host = nil, port, opts = {}) ⇒ Prefork

    Parameters:

    • host (String) (defaults to: nil)

      hostname or IP address

    • port (Integer / String)

      port number / service name

    • opts (Hash) (defaults to: {})

      options

    Options Hash (opts):

    • :min_processes (Integer) — default: 5

      minimum processes

    • :max_processes (Integer) — default: 20

      maximum processes

    • :max_idle (Integer) — default: 10

      cihld process exits if max_idle seconds is expired

    • :max_use (Integer) — default: 1000

      child process exits if it is connected max_use times.

    • :max_threads (Integer) — default: 1

      maximum threads per process

    • :standby_threads (Integer) — default: 5

      keep free processes or threads

    • :listen_backlog (Integer) — default: nil

      listen backlog

    • :on_start (#call) — default: nil

      object#call() is invoked when child process start. This is called in child process.

    • :on_reload (#call) — default: nil

      object#call(hash) is invoked when reload. This is called in child process.

    • :on_child_start (#call) — default: nil

      object#call(pid) is invoked when child process exit. This is call in parent process.

    • :on_child_exit (#call) — default: nil

      object#call(pid, status) is invoked when child process exit. This is call in parent process.

  • #initialize(socket, opts = {}) ⇒ Prefork

    Parameters:

    • socket (Socket)

      listening socket

    • opts (Hash) (defaults to: {})

      options

    Options Hash (opts):

    • :min_processes (Integer) — default: 5

      minimum processes

    • :max_processes (Integer) — default: 20

      maximum processes

    • :max_idle (Integer) — default: 10

      cihld process exits if max_idle seconds is expired

    • :max_use (Integer) — default: 1000

      child process exits if it is connected max_use times.

    • :max_threads (Integer) — default: 1

      maximum threads per process

    • :standby_threads (Integer) — default: 5

      keep free processes or threads

    • :listen_backlog (Integer) — default: nil

      listen backlog

    • :on_start (#call) — default: nil

      object#call() is invoked when child process start. This is called in child process.

    • :on_reload (#call) — default: nil

      object#call(hash) is invoked when reload. This is called in child process.

    • :on_child_start (#call) — default: nil

      object#call(pid) is invoked when child process exit. This is call in parent process.

    • :on_child_exit (#call) — default: nil

      object#call(pid, status) is invoked when child process exit. This is call in parent process.

  • #initialize(sockets, opts = {}) ⇒ Prefork

    Parameters:

    • sockets (Array<Socket>)

      listening sockets

    • opts (Hash) (defaults to: {})

      options

    Options Hash (opts):

    • :min_processes (Integer) — default: 5

      minimum processes

    • :max_processes (Integer) — default: 20

      maximum processes

    • :max_idle (Integer) — default: 10

      cihld process exits if max_idle seconds is expired

    • :max_use (Integer) — default: 1000

      child process exits if it is connected max_use times.

    • :max_threads (Integer) — default: 1

      maximum threads per process

    • :standby_threads (Integer) — default: 5

      keep free processes or threads

    • :listen_backlog (Integer) — default: nil

      listen backlog

    • :on_start (#call) — default: nil

      object#call() is invoked when child process start. This is called in child process.

    • :on_reload (#call) — default: nil

      object#call(hash) is invoked when reload. This is called in child process.

    • :on_child_start (#call) — default: nil

      object#call(pid) is invoked when child process exit. This is call in parent process.

    • :on_child_exit (#call) — default: nil

      object#call(pid, status) is invoked when child process exit. This is call in parent process.



39
40
41
42
43
44
45
46
47
48
# File 'lib/parallel_server/prefork.rb', line 39

def initialize(*args)
  @sockets, @host, @port, @opts = parse_args(*args)
  set_variables_from_opts
  @from_child = {}             # IO(r) => pid
  @to_child = {}               # IO(r) => IO(w)
  @child_status = {}           # IO(r) => Hash
  @children = []               # pid
  @loop = true
  @sockets_created = false
end

Instance Attribute Details

#child_statusObject (readonly)

Returns the value of attribute child_status.



13
14
15
# File 'lib/parallel_server/prefork.rb', line 13

def child_status
  @child_status
end

Instance Method Details

#reload(host = nil, port, opts = {}) ⇒ void #reload(socket, opts = {}) ⇒ void #reload(sockets, opts = {}) ⇒ void

This method returns an undefined value.

Overloads:

  • #reload(host = nil, port, opts = {}) ⇒ void

    Parameters:

    • host (String) (defaults to: nil)

      hostname or IP address

    • port (Integer / String)

      port number / service name

    • opts (Hash) (defaults to: {})

      options

    Options Hash (opts):

    • :min_processes (Integer) — default: 5

      minimum processes

    • :max_processes (Integer) — default: 20

      maximum processes

    • :max_idle (Integer) — default: 10

      cihld process exits if max_idle seconds is expired

    • :max_use (Integer) — default: 1000

      child process exits if it is connected max_use times.

    • :max_threads (Integer) — default: 1

      maximum threads per process

    • :standby_threads (Integer) — default: 5

      keep free processes or threads

    • :listen_backlog (Integer) — default: nil

      listen backlog

    • :on_start (#call) — default: nil

      object#call() is invoked when child process start. This is called in child process.

    • :on_reload (#call) — default: nil

      object#call(hash) is invoked when reload. This is called in child process.

    • :on_child_start (#call) — default: nil

      object#call(pid) is invoked when child process exit. This is call in parent process.

    • :on_child_exit (#call) — default: nil

      object#call(pid, status) is invoked when child process exit. This is call in parent process.

  • #reload(socket, opts = {}) ⇒ void

    Parameters:

    • socket (Socket)

      listening socket

    • opts (Hash) (defaults to: {})

      options

    Options Hash (opts):

    • :min_processes (Integer) — default: 5

      minimum processes

    • :max_processes (Integer) — default: 20

      maximum processes

    • :max_idle (Integer) — default: 10

      cihld process exits if max_idle seconds is expired

    • :max_use (Integer) — default: 1000

      child process exits if it is connected max_use times.

    • :max_threads (Integer) — default: 1

      maximum threads per process

    • :standby_threads (Integer) — default: 5

      keep free processes or threads

    • :listen_backlog (Integer) — default: nil

      listen backlog

    • :on_start (#call) — default: nil

      object#call() is invoked when child process start. This is called in child process.

    • :on_reload (#call) — default: nil

      object#call(hash) is invoked when reload. This is called in child process.

    • :on_child_start (#call) — default: nil

      object#call(pid) is invoked when child process exit. This is call in parent process.

    • :on_child_exit (#call) — default: nil

      object#call(pid, status) is invoked when child process exit. This is call in parent process.

  • #reload(sockets, opts = {}) ⇒ void

    Parameters:

    • sockets (Array<Socket>)

      listening sockets

    • opts (Hash) (defaults to: {})

      options

    Options Hash (opts):

    • :min_processes (Integer) — default: 5

      minimum processes

    • :max_processes (Integer) — default: 20

      maximum processes

    • :max_idle (Integer) — default: 10

      cihld process exits if max_idle seconds is expired

    • :max_use (Integer) — default: 1000

      child process exits if it is connected max_use times.

    • :max_threads (Integer) — default: 1

      maximum threads per process

    • :standby_threads (Integer) — default: 5

      keep free processes or threads

    • :listen_backlog (Integer) — default: nil

      listen backlog

    • :on_start (#call) — default: nil

      object#call() is invoked when child process start. This is called in child process.

    • :on_reload (#call) — default: nil

      object#call(hash) is invoked when reload. This is called in child process.

    • :on_child_start (#call) — default: nil

      object#call(pid) is invoked when child process exit. This is call in parent process.

    • :on_child_exit (#call) — default: nil

      object#call(pid, status) is invoked when child process exit. This is call in parent process.



87
88
89
# File 'lib/parallel_server/prefork.rb', line 87

def reload(*args)
  @reload_args = parse_args(*args)
end

#start {|sock, addr, child| ... } ⇒ void

This method returns an undefined value.

Yields:

  • (sock, addr, child)

Yield Parameters:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/parallel_server/prefork.rb', line 55

def start(&block)
  raise 'block required' unless block
  @block = block
  unless @sockets
    @sockets = Socket.tcp_server_sockets(@host, @port)
    @sockets.each{|s| s.listen(@listen_backlog)} if @listen_backlog
    @sockets_created = true
  end
  @reload_args = nil
  while @loop
    do_reload if @reload_args
    watch_children
    adjust_children
  end
ensure
  @sockets.each{|s| s.close rescue nil} if @sockets && @sockets_created
  @to_child.values.each{|s| s.close rescue nil}
  @to_child.clear
  Timeout.timeout(1){wait_all_children} rescue Thread.new{wait_all_children}
end

#stopvoid

This method returns an undefined value.



92
93
94
# File 'lib/parallel_server/prefork.rb', line 92

def stop
  @loop = false
end

#stop!void

This method returns an undefined value.



97
98
99
100
# File 'lib/parallel_server/prefork.rb', line 97

def stop!
  Process.kill 'TERM', *@children rescue nil
  @loop = false
end