Class: Steep::Server::WorkerProcess
- Defined in:
- lib/steep/server/worker_process.rb
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#io_socket ⇒ Object
readonly
Returns the value of attribute io_socket.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#wait_thread ⇒ Object
readonly
Returns the value of attribute wait_thread.
-
#writer ⇒ Object
readonly
Returns the value of attribute writer.
Class Method Summary collapse
- .fork_worker(type, name:, steepfile:, index:, delay_shutdown:, patterns:, is_primary:) ⇒ Object
- .spawn_worker(type, name:, steepfile:, steep_command:, index:, delay_shutdown:, patterns:) ⇒ Object
- .start_typecheck_workers(steepfile:, args:, steep_command:, count: [Etc.nprocessors - 1, 1].max || raise, delay_shutdown: false) ⇒ Object
- .start_worker(type, name:, steepfile:, steep_command:, index: nil, delay_shutdown: false, patterns: []) ⇒ Object
Instance Method Summary collapse
- #<<(message) ⇒ Object
-
#initialize(reader:, writer:, io_socket: nil, stderr:, wait_thread:, name:, index: nil) ⇒ WorkerProcess
constructor
A new instance of WorkerProcess.
- #kill(force: false) ⇒ Object
- #pid ⇒ Object
- #read(&block) ⇒ Object
- #redirect_to(worker) ⇒ Object
Constructor Details
#initialize(reader:, writer:, io_socket: nil, stderr:, wait_thread:, name:, index: nil) ⇒ WorkerProcess
Returns a new instance of WorkerProcess.
13 14 15 16 17 18 19 20 21 |
# File 'lib/steep/server/worker_process.rb', line 13 def initialize(reader:, writer:, io_socket: nil, stderr:, wait_thread:, name:, index: nil) @reader = reader @writer = writer @stderr = stderr @io_socket = io_socket @wait_thread = wait_thread @name = name @index = index end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
10 11 12 |
# File 'lib/steep/server/worker_process.rb', line 10 def index @index end |
#io_socket ⇒ Object (readonly)
Returns the value of attribute io_socket.
11 12 13 |
# File 'lib/steep/server/worker_process.rb', line 11 def io_socket @io_socket end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/steep/server/worker_process.rb', line 8 def name @name end |
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
4 5 6 |
# File 'lib/steep/server/worker_process.rb', line 4 def reader @reader end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
6 7 8 |
# File 'lib/steep/server/worker_process.rb', line 6 def stderr @stderr end |
#wait_thread ⇒ Object (readonly)
Returns the value of attribute wait_thread.
9 10 11 |
# File 'lib/steep/server/worker_process.rb', line 9 def wait_thread @wait_thread end |
#writer ⇒ Object (readonly)
Returns the value of attribute writer.
5 6 7 |
# File 'lib/steep/server/worker_process.rb', line 5 def writer @writer end |
Class Method Details
.fork_worker(type, name:, steepfile:, index:, delay_shutdown:, patterns:, is_primary:) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/steep/server/worker_process.rb', line 47 def self.fork_worker(type, name:, steepfile:, index:, delay_shutdown:, patterns:, is_primary:) stdin_in, stdin_out = IO.pipe stdout_in, stdout_out = IO.pipe sock_master, sock_worker = UNIXSocket.socketpair if is_primary worker = Drivers::Worker.new(stdout: stdout_out, stdin: stdin_in, stderr: STDERR) worker.steepfile = steepfile worker.worker_type = type worker.worker_name = name worker.delay_shutdown = delay_shutdown if (max, this = index) worker.max_index = max worker.index = this end worker.commandline_args = patterns worker.io_socket = sock_worker pid = fork do Process.setpgid(0, 0) Steep.ui_logger.level = :fatal stdin_out.close stdout_in.close sock_master&.close worker.run() end pid or raise writer = LanguageServer::Protocol::Transport::Io::Writer.new(stdin_out) reader = LanguageServer::Protocol::Transport::Io::Reader.new(stdout_in) # @type var wait_thread: Thread & _ProcessWaitThread wait_thread = _ = Thread.new { Process.waitpid(pid) } wait_thread.define_singleton_method(:pid) { pid } stdin_in.close stdout_out.close sock_worker&.close new( reader: reader, writer: writer, stderr: STDERR, wait_thread: wait_thread, name: name, index: index&.[](1), io_socket: sock_master, ) end |
.spawn_worker(type, name:, steepfile:, steep_command:, index:, delay_shutdown:, patterns:) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/steep/server/worker_process.rb', line 98 def self.spawn_worker(type, name:, steepfile:, steep_command:, index:, delay_shutdown:, patterns:) args = ["--name=#{name}"] args << "--steepfile=#{steepfile}" if steepfile args << (%w(debug info warn error fatal unknown)[Steep.logger.level].yield_self {|log_level| "--log-level=#{log_level}" }) if Steep.log_output.is_a?(String) args << "--log-output=#{Steep.log_output}" end if (max, this = index) args << "--max-index=#{max}" args << "--index=#{this}" end if delay_shutdown args << "--delay-shutdown" end command = case type when :interaction [steep_command, "worker", "--interaction", *args, *patterns] when :typecheck [steep_command, "worker", "--typecheck", *args, *patterns] else raise "Unknown type: #{type}" end stdin, stdout, thread = if Gem.win_platform? __skip__ = Open3.popen2(*command, new_pgroup: true) else __skip__ = Open3.popen2(*command, pgroup: true) end stderr = nil writer = LanguageServer::Protocol::Transport::Io::Writer.new(stdin) reader = LanguageServer::Protocol::Transport::Io::Reader.new(stdout) new(reader: reader, writer: writer, stderr: stderr, wait_thread: thread, name: name, index: index&.[](1)) end |
.start_typecheck_workers(steepfile:, args:, steep_command:, count: [Etc.nprocessors - 1, 1].max || raise, delay_shutdown: false) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/steep/server/worker_process.rb', line 138 def self.start_typecheck_workers(steepfile:, args:, steep_command:, count: [Etc.nprocessors - 1, 1].max || raise, delay_shutdown: false) count.times.map do |i| start_worker( :typecheck, name: "typecheck@#{i}", steepfile: steepfile, steep_command: steep_command, index: [count, i], patterns: args, delay_shutdown: delay_shutdown, ) end end |
.start_worker(type, name:, steepfile:, steep_command:, index: nil, delay_shutdown: false, patterns: []) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/steep/server/worker_process.rb', line 23 def self.start_worker(type, name:, steepfile:, steep_command:, index: nil, delay_shutdown: false, patterns: []) if Steep.can_fork? && !steep_command fork_worker( type, name: name, steepfile: steepfile, index: index, is_primary: index && (index[1] == 0 && index[0] >= 2), delay_shutdown: delay_shutdown, patterns: patterns ) else spawn_worker( type, name: name, steepfile: steepfile, steep_command: steep_command || "steep", index: index, delay_shutdown: delay_shutdown, patterns: patterns ) end end |
Instance Method Details
#<<(message) ⇒ Object
156 157 158 |
# File 'lib/steep/server/worker_process.rb', line 156 def <<() writer.write() end |
#kill(force: false) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/steep/server/worker_process.rb', line 164 def kill(force: false) Steep.logger.tagged("WorkerProcess#kill@#{name}(#{pid})") do begin signal = force ? :KILL : :TERM Steep.logger.debug("Sending signal SIG#{signal}...") Process.kill(signal, pid) Steep.logger.debug("Successfully sent the signal.") rescue Errno::ESRCH => error Steep.logger.debug("Failed #{error.inspect}") end unless force Steep.logger.debug("Waiting for process exit...") wait_thread.join() Steep.logger.debug("Confirmed process exit.") end end end |
#pid ⇒ Object
182 183 184 |
# File 'lib/steep/server/worker_process.rb', line 182 def pid wait_thread.pid end |
#read(&block) ⇒ Object
160 161 162 |
# File 'lib/steep/server/worker_process.rb', line 160 def read(&block) reader.read(&block) end |
#redirect_to(worker) ⇒ Object
152 153 154 |
# File 'lib/steep/server/worker_process.rb', line 152 def redirect_to(worker) @writer = worker.writer end |