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.
-
#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:) ⇒ 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:, stderr:, wait_thread:, name:, index: nil) ⇒ WorkerProcess
constructor
A new instance of WorkerProcess.
- #kill(force: false) ⇒ Object
- #read(&block) ⇒ Object
Constructor Details
#initialize(reader:, writer:, stderr:, wait_thread:, name:, index: nil) ⇒ WorkerProcess
Returns a new instance of WorkerProcess.
12 13 14 15 16 17 18 19 |
# File 'lib/steep/server/worker_process.rb', line 12 def initialize(reader:, writer:, stderr:, wait_thread:, name:, index: nil) @reader = reader @writer = writer @stderr = stderr @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 |
#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:) ⇒ Object
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 |
# File 'lib/steep/server/worker_process.rb', line 49 def self.fork_worker(type, name:, steepfile:, index:, delay_shutdown:, patterns:) stdin_in, stdin_out = IO.pipe stdout_in, stdout_out = IO.pipe 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 pid = fork do Process.setpgid(0, 0) Steep.ui_logger.level = :fatal stdin_out.close stdout_in.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 new( reader: reader, writer: writer, stderr: STDERR, wait_thread: wait_thread, name: name, index: index&.[](1) ) end |
.spawn_worker(type, name:, steepfile:, steep_command:, index:, delay_shutdown:, patterns:) ⇒ Object
95 96 97 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 |
# File 'lib/steep/server/worker_process.rb', line 95 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
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/steep/server/worker_process.rb', line 135 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
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/steep/server/worker_process.rb', line 21 def self.start_worker(type, name:, steepfile:, steep_command:, index: nil, delay_shutdown: false, patterns: []) begin unless steep_command fork_worker( type, name: name, steepfile: steepfile, index: index, delay_shutdown: delay_shutdown, patterns: patterns ) else # Use `#spawn_worker` raise NotImplementedError end rescue NotImplementedError 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
149 150 151 |
# File 'lib/steep/server/worker_process.rb', line 149 def <<() writer.write() end |
#kill(force: false) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/steep/server/worker_process.rb', line 157 def kill(force: false) Steep.logger.tagged("WorkerProcess#kill@#{name}(#{wait_thread.pid})") do begin signal = force ? :KILL : :TERM Steep.logger.debug("Sending signal SIG#{signal}...") Process.kill(signal, wait_thread.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 |
#read(&block) ⇒ Object
153 154 155 |
# File 'lib/steep/server/worker_process.rb', line 153 def read(&block) reader.read(&block) end |