Class: ScriptCore::ServiceProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/script_core/service_process.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, spawner, instruction_quota, instruction_quota_start, memory_quota, environment_variables = {}) ⇒ ServiceProcess

Returns a new instance of ServiceProcess.



7
8
9
10
11
12
13
14
# File 'lib/script_core/service_process.rb', line 7

def initialize(path, spawner, instruction_quota, instruction_quota_start, memory_quota, environment_variables = {})
  @path = path
  @spawner = spawner
  @instruction_quota = instruction_quota
  @instruction_quota_start = instruction_quota_start
  @memory_quota = memory_quota
  @environment_variables = environment_variables
end

Instance Attribute Details

#environment_variablesObject (readonly)

Returns the value of attribute environment_variables.



5
6
7
# File 'lib/script_core/service_process.rb', line 5

def environment_variables
  @environment_variables
end

#instruction_quotaObject (readonly)

Returns the value of attribute instruction_quota.



5
6
7
# File 'lib/script_core/service_process.rb', line 5

def instruction_quota
  @instruction_quota
end

#instruction_quota_startObject (readonly)

Returns the value of attribute instruction_quota_start.



5
6
7
# File 'lib/script_core/service_process.rb', line 5

def instruction_quota_start
  @instruction_quota_start
end

#memory_quotaObject (readonly)

Returns the value of attribute memory_quota.



5
6
7
# File 'lib/script_core/service_process.rb', line 5

def memory_quota
  @memory_quota
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/script_core/service_process.rb', line 5

def path
  @path
end

#spawnerObject (readonly)

Returns the value of attribute spawner.



5
6
7
# File 'lib/script_core/service_process.rb', line 5

def spawner
  @spawner
end

Instance Method Details

#openObject



16
17
18
19
20
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
48
49
50
51
52
53
54
# File 'lib/script_core/service_process.rb', line 16

def open
  in_reader, in_writer = IO.pipe
  out_reader, out_writer = IO.pipe

  pid = spawner.spawn(
    environment_variables,
    path,
    "-i", instruction_quota.to_s,
    "-C", instruction_quota_start.to_s,
    "-m", memory_quota.to_s,
    in: in_reader,
    out: out_writer,
    unsetenv_others: true
  )

  in_reader.close
  out_writer.close

  in_writer.binmode
  out_reader.binmode

  begin
    yield ScriptCore::ServiceChannel.new(in_writer, out_reader)
  ensure
    code = spawner.wait(pid, Process::WNOHANG) || begin
      begin
        spawner.kill(9, pid)
        spawner.wait(pid)
      rescue Errno::ESRCH
        -1
      end
    end

    out_reader.close
    in_writer.close
  end

  code
end