Class: OpalWebpackLoader::CompileWorker

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

Constant Summary collapse

SIGNALS =
%w[QUIT]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(master_pid, socket, tempfile, number, compiler_options, other_options) ⇒ CompileWorker



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/opal-webpack-loader/compile_worker.rb', line 9

def initialize(master_pid, socket, tempfile, number, compiler_options, other_options)
  @master_pid = master_pid
  @socket     = socket
  @tempfile   = tempfile
  @number     = number
  @compiler_options = compiler_options.merge(es6_modules: true)
  if other_options[:memcached]
    @cache = Dalli::Client.new(other_options[:memchached])
  elsif other_options[:redis]
    @cache = Redis.new(url: other_options[:redis])
  else
    @cache = false
  end
  @compiler_options_digest = Digest::SHA1.hexdigest(Oj.dump(@compiler_options, mode: :strict)) if cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



7
8
9
# File 'lib/opal-webpack-loader/compile_worker.rb', line 7

def cache
  @cache
end

#compiler_options_digestObject (readonly)

Returns the value of attribute compiler_options_digest.



7
8
9
# File 'lib/opal-webpack-loader/compile_worker.rb', line 7

def compiler_options_digest
  @compiler_options_digest
end

#numberObject (readonly)

Returns the value of attribute number.



7
8
9
# File 'lib/opal-webpack-loader/compile_worker.rb', line 7

def number
  @number
end

#tempfileObject (readonly)

Returns the value of attribute tempfile.



7
8
9
# File 'lib/opal-webpack-loader/compile_worker.rb', line 7

def tempfile
  @tempfile
end

Instance Method Details

#==(other_number) ⇒ Object



25
26
27
# File 'lib/opal-webpack-loader/compile_worker.rb', line 25

def ==(other_number)
  number == other_number
end

#startObject



29
30
31
32
33
34
35
36
37
38
39
40
41
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
72
# File 'lib/opal-webpack-loader/compile_worker.rb', line 29

def start
  $PROGRAM_NAME = "owl compile worker #{number}"
  SIGNALS.each { |sig| trap(sig, 'IGNORE') }
  trap('CHLD', 'DEFAULT')
  alive = true
  %w[TERM INT].each { |sig| trap(sig) { exit(0) } }
  trap('QUIT') do
    alive = false
    begin
      @socket.close
    rescue
      nil
    end
  end
  ret = nil
  i = 0
  while alive && @master_pid == Process.ppid
    tempfile.chmod(i += 1)

    if ret
      begin
        client = @socket.accept_nonblock
        request = client.gets("\x04")

        if request.start_with?('command:stop')
          OpalWebpackLoader::CompileServer.unlink_socket_on_exit
          Process.kill('TERM', @master_pid)
          exit(0)
        else
          result = compile(request)
          client.write result
          client.flush
          client.close
        end
      rescue Errno::EAGAIN, Errno::EWOULDBLOCK
      end
    end
    tempfile.chmod(i += 1)
    ret = begin
            IO.select([@socket], nil, nil, OpalWebpackLoader::CompileServer::TIMEOUT / 2) || next
          rescue Errno::EBADF
          end
  end
end