Skip to content

Callbacks and Events

mosop edited this page Jan 4, 2017 · 24 revisions

Note: This feature is not supported from 0.5.

You can set callbacks to command groups. A callback is a Proc object and called when the corresponding event is occurred.

class WhatsWrong < Run::CommandGroup
  on_group_abort do |cg, pg|
    puts "what's wrong?"
  end

  on_abort do |cg, p|
    puts "what's wrong, #{p.context.command}?"
  end
end

cg = WhatsWrong.new
cg.command "/bin/bash", ["-c", "exit 2"], abort_on_error: true
cg.command "sleep", ["100"]
cg.run.wait

This prints:

what's wrong?
what's wrong, sleep?

You must inherit Run::CommandGroup and use the inheriting class for setting callbacks because callbacks are set to the class variables.

Events

on_abort(&block : (Run::CommandGroup, Run::Process) -> _)

Occurs when a single process are killed or timed out on abort.

on_group_abort(&block : (Run::CommandGroup, Run::ProcessGroup) -> _)

Occurs when all the descendant processes in a process group are killed or timed out on abort.

on_success(&block : (Run::CommandGroup, Run::Process) -> _)

on_group_success(&block : (Run::CommandGroup, Run::ProcessGroup) -> _)

on_error(&block : (Run::CommandGroup, Run::Process) -> _)

on_group_error(&block : (Run::CommandGroup, Run::ProcessGroup) -> _)