Method: Bj::API#submit

Defined in:
lib/bj/api.rb

#submit(jobs, options = {}, &block) ⇒ Object

submit jobs for background processing. ‘jobs’ can be a string or array of strings. options are applied to each job in the ‘jobs’, and the list of submitted jobs is always returned. options (string or symbol) can be

:rails_env => production|development|key_in_database_yml 
              when given this keyword causes bj to submit jobs to the
              specified database.  default is RAILS_ENV.

:priority => any number, including negative ones.  default is zero.

:tag => a tag added to the job.  simply makes searching easier.

:env => a hash specifying any additional environment vars the background
        process should have.

:stdin => any stdin the background process should have.

eg:

jobs = Bj.submit 'echo foobar', :tag => 'simple job'

jobs = Bj.submit '/bin/cat', :stdin => 'in the hat', :priority => 42

jobs = Bj.submit './script/runner ./scripts/a.rb', :rails_env => 'production'

jobs = Bj.submit './script/runner /dev/stdin', :stdin => 'p RAILS_ENV', :tag => 'dynamic ruby code'

jobs = Bj.submit array_of_commands, :priority => 451

when jobs are run, they are run in RAILS_ROOT. various attributes are available only once the job has finished. you can check whether or not a job is finished by using the #finished method, which simple does a reload and checks to see if the exit_status is non-nil.

eg:

jobs = Bj.submit list_of_jobs, :tag => 'important'
...

jobs.each do |job|
  if job.finished?
    p job.exit_status
    p job.stdout
    p job.stderr
  end
end


60
61
62
63
64
65
66
67
# File 'lib/bj/api.rb', line 60

def submit jobs, options = {}, &block
  options.to_options!
  Bj.transaction(options) do
    table.job.submit jobs, options, &block
  end
ensure
  Bj.runner.tickle unless options[:no_tickle]
end