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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CompileServer

Returns a new instance of CompileServer.



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

def initialize(options)
  @read_pipe, @write_pipe = IO.pipe
  @workers = {}
  @signal_queue = []
  @socket_path = options[:socket_path]
  @options = options
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



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

def cache
  @cache
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) ⇒ Object



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

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(@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