Class: MadCat::LitterBox
- Inherits:
-
Object
- Object
- MadCat::LitterBox
- Defined in:
- lib/madcat.rb
Defined Under Namespace
Classes: ErrorFromJSON, MalformedInput, MalformedOutput, NonZeroExit, PipelineStop
Instance Method Summary collapse
-
#initialize(logger) ⇒ LitterBox
constructor
A new instance of LitterBox.
- #l ⇒ Object
- #run(blob, id = :unknown, trace = false, depth = 0, meta_in = nil, meta_key = nil) ⇒ Object
Constructor Details
#initialize(logger) ⇒ LitterBox
Returns a new instance of LitterBox.
209 210 211 |
# File 'lib/madcat.rb', line 209 def initialize(logger) @logger = logger end |
Instance Method Details
#l ⇒ Object
341 342 343 |
# File 'lib/madcat.rb', line 341 def l @logger end |
#run(blob, id = :unknown, trace = false, depth = 0, meta_in = nil, meta_key = nil) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/madcat.rb', line 213 def run(blob, id=:unknown, trace=false, depth=0, =nil, =nil) begin # scaffold the results-object as early as possible # because this will also be returned and augmented # when an exception occurs. results = [{'id' => id, :exit => -1, :stdin => blob, 'duration' => -1, :stdout => '', :stderr => ''}] blob = JSON.parse(blob) = Marshal.load(Marshal.dump()) # deep_copy todo = ['todo'] raise RuntimeError, 'Pipeline has no jobs defined?!' if todo.length == 0 pipeline_id = ['id'] job = todo.shift cmd = job.split(' ') job = cmd[0] # find our executable bin_path = ['path'] bin = nil bin_path.each do |e| bin = File.join(e, cmd[0]) break if File.executable? bin bin = nil end if bin.nil? raise RuntimeError, "Executable #{job} not found in #{bin_path.join(':')}" end # update meta ['current'] = job # add read-only metadata if blob[] = end # run our executable blob['*'] = cmd[0] stdin = blob.to_json results[0][:stdin] = stdin # we modified it above start_time = Time.now p = MadCat::Helpers::exec!([bin] + cmd[1..-1], stdin) duration = (Time.now - start_time).to_f results[0].merge!({:bin => bin, :exit => p.status, 'duration' => duration, :stdout => p.stdout, :stderr => p.stderr, :todo => todo, :depth => depth }) if trace d=0 ['done'].each do |node| l.debug "## #{' ' * d}#{node.keys[0]}" d+=1 end l.debug "## #{' ' * d}#{id} <--" l.debug "#" * 70 # zomg HACK: append a newline to stdin/stdout/stderr to convince # the yaml serializer to print them as block literals. # a bit evil, but it makes the trace-output much more readable... [:stdin, :stdout, :stderr].each do |key| if 0 < results[0][key].length && results[0][key][-1] != "\n" results[0][key] += "\n" end end results[0].to_yaml.split("\n").each do |line| next if line[0..2] == '---' l.debug line end l.debug "#" * 70 end # alert on exit code != 0 raise NonZeroExit, "#{p.status}" if p.status != 0 # no output? game over. raise PipelineStop if p.stdout == '' # parse output and wrap JSON errors begin blobs = JSON.parse(p.stdout) rescue => e raise MalformedOutput, "Could not parse JSON: #{e.}" if 0 < todo.length end blobs = [blobs] unless blobs.is_a? Array # raise on json-key 'err' blobs.each do |blob| if blob.include? 'err' raise ErrorFromJSON, blob['err'] end end # oob mode ['done'] << { results[-1]['id'] => results[-1].select{|k,v| ['duration', :exit].include? k} } # feed blob to next stage of the pipeline i=0 blobs.each do |new_inner_blob| break if 0 == todo.length # store output blob under jobid blob_ = Marshal.load(Marshal.dump(blob)) # deep_copy blob_[job] ||= {} blob_[job].deep_merge! new_inner_blob got = run(blob_.to_json, "#{todo[0].split(' ')[0]}:#{i}", trace, depth+1, , ) results += got i+=1 end results rescue => e results[-1][:exception_type] = e.class.name.split('::')[-1] results[-1][:exception_message] = e. results[-1][:exception_backtrace] = e.backtrace # bubble up results end end |