Class: Grably::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/grably/server.rb

Overview

Entry point to submodule task management

Constant Summary collapse

EXPORT_FILENAME =
'grably.export'.freeze

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



8
9
10
# File 'lib/grably/server.rb', line 8

def initialize
  @child_processes = {}
end

Instance Method Details

#execute(call, out_file, prefix: ' ') ⇒ Object

rubocop:disable all



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/grably/server.rb', line 22

def execute(call, out_file, prefix: ' ')
  rakefile = call.path
  
  profile = *call.profile.map(&:to_s)

  if Grably.export_path
    data = [ rakefile, out_file, { 'target' => call.task, 'profile' => profile, 'params' => params } ]
    puts "remote_mbuild_request:#{JSON.generate(data)}"
    $stdout.flush
    $stdin.each do |l|
      raise "error in remote (slave) grably" unless l.strip == 'remote_grab_finished'
      break
    end
  else
    key = { rakefile: rakefile, profile: profile }
    
    process = @child_processes[key]
    
    unless process
      env = { 'config' => nil }
      cmd = [
        RbConfig.ruby,
        File.expand_path(File.join(__dir__, 'runner.rb')),
        '-f',
        File.basename(rakefile),
        "mp=#{profile.join(',')}"
      ]
      cmd << "--trace" if Rake.application.options.trace
      
      stdin, stdout, thread = Open3.popen2(*[env, cmd, { :err => [ :child, :out], :chdir => File.dirname(rakefile) }].flatten)
      stdout.sync = true
      stdin.sync = true
      
      process = { stdin: stdin, stdout: stdout, thread: thread }
      @child_processes[key] = process
    end
    
    process[:stdin].puts("build|#{out_file}|#{[call.task].flatten.join('|')}")
    
    ok = false
    process[:stdout].each do |l|
      ls = l.strip
      if ls == 'remote_grab_finished'
        ok = true
        break
      end
      
      if ls.start_with?('remote_grab_request:')
        data = JSON.parse(ls['remote_grab_request:'.size..-1])
        execute(data[0], data[1], "#{prefix}  ", { target: data[2]['target'], profile: data[2]['profile'], params: data[2]['params']})
        process[:stdin].puts('remote_grab_finished')
      else
        log "#{prefix}#{l}"
      end
    end
    
    raise 'error in remote grab' unless ok
  end
end

#remote_result(call, out_file) ⇒ Object



16
17
18
19
# File 'lib/grably/server.rb', line 16

def remote_result(call, out_file)
  execute(call, out_file)
  load_obj(out_file)
end

#schedule(call) ⇒ Object



12
13
14
# File 'lib/grably/server.rb', line 12

def schedule(call)
  ->(dir) { remote_result(call, File.expand_path(File.join(dir, EXPORT_FILENAME))) }
end