Class: OpalWebpackLoader::CompileServer

Inherits:
Object
  • Object
show all
Defined in:
lib/opal-webpack-loader/compile_server.rb

Constant Summary collapse

SIGNALS =
%w[QUIT INT TERM]
TIMEOUT =
15

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompileServer

Returns a new instance of CompileServer.



36
37
38
39
40
# File 'lib/opal-webpack-loader/compile_server.rb', line 36

def initialize
  @read_pipe, @write_pipe = IO.pipe
  @workers = {}
  @signal_queue = []
end

Class Method Details



17
18
19
# File 'lib/opal-webpack-loader/compile_server.rb', line 17

def self.dont_unlink_socket_on_exit
  @unlink = false
end

.stop(socket_path, do_exit = true) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/opal-webpack-loader/compile_server.rb', line 21

def self.stop(socket_path, do_exit = true)
  if File.exist?(socket_path)
    dont_unlink_socket_on_exit
    begin
      s = UNIXSocket.new(socket_path)
      s.send("command:stop\x04", 0)
      s.close
    rescue
      # socket cant be reached so owlcs is already dead, delete socket
      unlink_socket_on_exit
    end
    exit(0) if do_exit
  end
end

Returns:

  • (Boolean)


9
10
11
# File 'lib/opal-webpack-loader/compile_server.rb', line 9

def self.unlink_socket?
  @unlink
end


13
14
15
# File 'lib/opal-webpack-loader/compile_server.rb', line 13

def self.unlink_socket_on_exit
  @unlink = true
end

Instance Method Details

#start(number_of_workers = 4, compiler_options, socket_path) ⇒ Object



42
43
44
45
46
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
# File 'lib/opal-webpack-loader/compile_server.rb', line 42

def start(number_of_workers = 4, compiler_options, socket_path)
  $PROGRAM_NAME = 'owl compile server'
  @number_of_workers = number_of_workers
  @server_pid = Process.pid
  $stderr.sync = $stdout.sync = true
  @socket = UNIXServer.new(socket_path)
  spawn_workers(compiler_options)
  SIGNALS.each { |sig| trap_deferred(sig) }
  trap('CHLD') { @write_pipe.write_nonblock('.') }

  loop do
    reap_workers
    mode = @signal_queue.shift
    case mode
    when nil
      kill_runaway_workers
      spawn_workers(compiler_options)
    when 'QUIT', 'TERM', 'INT'
      @workers.each_pair do |pid, _worker|
        Process.kill('TERM', pid)
      end
      break
    end
    reap_workers
    ready = IO.select([@read_pipe], nil, nil, 1) || next
    ready.first && ready.first.first || next
    @read_pipe.read_nonblock(1)
    OpalWebpackLoader::CompileServer.unlink_socket_on_exit
  end
end