Class: OpalWebpackLoader::CompileServer

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

Constant Summary collapse

OWL_CACHE_DIR =
File.join('.','.owl_cache/')
OWL_LP_CACHE =
File.join(OWL_CACHE_DIR, 'load_paths.json')
OWCS_SOCKET_PATH =
File.join(OWL_CACHE_DIR, 'owcs_socket')
SIGNALS =
%w[QUIT INT TERM]
TIMEOUT =
15

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompileServer

Returns a new instance of CompileServer.



39
40
41
42
43
# File 'lib/opal-webpack-loader/compile_server.rb', line 39

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

Class Method Details



20
21
22
# File 'lib/opal-webpack-loader/compile_server.rb', line 20

def self.dont_unlink_socket_on_exit
  @unlink = false
end

.stop(do_exit = true) ⇒ Object



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

def self.stop(do_exit = true)
  if File.exist?(OWCS_SOCKET_PATH)
    dont_unlink_socket_on_exit
    begin
      s = UNIXSocket.new(OWCS_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)


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

def self.unlink_socket?
  @unlink
end


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

def self.unlink_socket_on_exit
  @unlink = true
end

Instance Method Details

#start(number_of_workers = 4, compiler_options) ⇒ Object



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
72
73
74
# File 'lib/opal-webpack-loader/compile_server.rb', line 45

def start(number_of_workers = 4, compiler_options)
  $PROGRAM_NAME = 'owl compile server'
  @number_of_workers = number_of_workers
  @server_pid = Process.pid
  $stderr.sync = $stdout.sync = true
  @socket = UNIXServer.new(OWCS_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