Module: IronWorkerNG::Code::Runtime::Ruby

Instance Method Summary collapse

Methods included from Feature::Ruby::MergeGemfile::InstanceMethods

#merge_gemfile

Methods included from Feature::Ruby::MergeGemDependency::InstanceMethods

#merge_gem, #merge_gem_dependency_fixate

Methods included from Feature::Common::MergeExec::InstanceMethods

#merge_exec

Instance Method Details

#install(standalone = false) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/iron_worker_ng/code/runtime/ruby.rb', line 140

def install(standalone = false)
  gemfile_dir = nil
  gemfile = nil

  if standalone
    gemfile = File.open('Gemfile', 'w')
  else
    gemfile_dir = ::Dir.tmpdir + '/' + ::Dir::Tmpname.make_tmpname('iron-worker-ng-', 'gemfile')

    FileUtils.mkdir(gemfile_dir)

    gemfile = File.open(gemfile_dir + '/Gemfile', 'w')
  end

  gemfile.puts('source \'http://rubygems.org\'')

  deps = @features.reject { |f| f.class != IronWorkerNG::Feature::Ruby::MergeGemDependency::Feature }

  deps.each do |dep|
    gemfile.puts("gem '#{dep.name}', '#{dep.version}'")
  end

  gemfile.close

  if standalone
    puts `bundle install --standalone`
  else
    puts `cd #{gemfile_dir} && bundle install`

    FileUtils.rm_r(gemfile_dir)
  end
end

#runtime_bundle(container, local = false) ⇒ Object



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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/iron_worker_ng/code/runtime/ruby.rb', line 16

def runtime_bundle(container, local = false)
  container.get_output_stream(@dest_dir + '__runner__.rb') do |runner|
    runner.write <<RUBY_RUNNER
# #{IronWorkerNG.full_version}

module IronWorkerNG
#{File.read(File.dirname(__FILE__) + '/../../../3rdparty/hashie/merge_initializer.rb')}
#{File.read(File.dirname(__FILE__) + '/../../../3rdparty/hashie/indifferent_access.rb')}
end

class IronWorkerNGHash < Hash
  include IronWorkerNG::Hashie::Extensions::MergeInitializer
  include IronWorkerNG::Hashie::Extensions::IndifferentAccess
end

root = nil
payload_file = nil
config_file = nil
task_id = nil
schedule_id = nil

0.upto($*.length - 2) do |i|
  root = $*[i + 1] if $*[i] == '-d'
  payload_file = $*[i + 1] if $*[i] == '-payload'
  config_file = $*[i + 1] if $*[i] == '-config'
  task_id = $*[i + 1] if $*[i] == '-id'
  schedule_id = $*[i + 1] if $*[i] == '-schedule_id'
end

ENV['GEM_PATH'] = ([root + '__gems__'] + (ENV['GEM_PATH'] || '').split(':')).join(':')
ENV['GEM_HOME'] = root + '__gems__'

$:.unshift("\#{root}")

require 'json'
require 'yaml'

@iron_task_id = task_id
@iron_schedule_id = schedule_id

@payload = File.read(payload_file)

params = {}
begin
  params = JSON.parse(@payload)
rescue
end

@config = nil
if config_file
  @config = File.read(config_file)
  begin
    @config = JSON.parse(@config)
    @config = IronWorkerNGHash.new(@config) if @config.is_a?(::Hash)
  rescue
    # try yaml
    begin
      @config = YAML.load(@config)
      @config = IronWorkerNGHash.new(@config) if @config.is_a?(::Hash)
    rescue
    end
  end
end

if params.is_a?(::Hash)
  @params = IronWorkerNGHash.new(params)
else
  @params = params
end

def payload
  @payload
end

def config
  @config
end

def params
  @params
end

def iron_task_id
  @iron_task_id
end

def iron_schedule_id
  @iron_schedule_id
end

require '#{File.basename(@exec.path)}'

unless #{@exec.arg(:class, 0) == nil}
  exec_class = Kernel.const_get('#{@exec.arg(:class, 0)}')
  exec_inst = exec_class.new

  params.keys.each do |param|
    if param.class == String
      if exec_inst.respond_to?(param + '=')
exec_inst.send(param + '=', params[param])
      end
    end
  end

  if exec_inst.respond_to?(:iron_task_id=)
    exec_inst.send(:iron_task_id=, iron_task_id)
  end

  if exec_inst.respond_to?(:iron_schedule_id=)
    exec_inst.send(:iron_schedule_id=, iron_schedule_id)
  end

  exec_inst.run
end
RUBY_RUNNER
  end
end

#runtime_run_code(local, params) ⇒ Object



134
135
136
137
138
# File 'lib/iron_worker_ng/code/runtime/ruby.rb', line 134

def runtime_run_code(local, params)
  <<RUN_CODE
ruby __runner__.rb #{params}
RUN_CODE
end