Module: Bj::API

Defined in:
lib/bj/api.rb

Overview

Bj.table.job.find :all

Instance Method Summary collapse

Instance Method Details

#generate_migration(options = {}) ⇒ Object

generate_migration, suprisingly, generates the single migration needed for bj. you’ll notice the the migration is very short as the migration classes themselves are inner classes of the respective bj table class. see lib/bj/table.rb for details.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bj/api.rb', line 117

def generate_migration options = {}
  options.to_options!
  chroot do
    before = Dir.glob "./db/migrate/*"
    util.spawn "./script/generate migration BjMigration", options rescue nil
    after = Dir.glob "./db/migrate/*"
    candidates = after - before
    case candidates.size
      when 0
        false
      when 1
        generated = candidates.first
        open(generated, "w"){|fd| fd.puts Bj.table.migration_code}
        Bj.logger.info{ "generated <#{ generated }>" }
        generated
      else
        raise "ambiguous migration <#{ candidates.inspect }>"
    end
  end
end

#in(rails_env = Bj.rails_env, &block) ⇒ Object

this method changes the context under which bj is operating. a context is a RAILS_ENV. the method accepts a block and it used to alter the behaviour of the bj lib on a global scale such that all operations, spawning of background runnner processes, etc, occur in that context.

eg:

Bj.in :production do
  Bj.submit './script/runner ./scripts/facebook_notification.rb'
end

Bj.in :development do
  Bj.submit 'does_this_eat_memory.exe'
end


84
85
86
# File 'lib/bj/api.rb', line 84

def in rails_env = Bj.rails_env, &block
  transaction(:rails_env => rails_env.to_s, &block)
end

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

list simply returns a list of all jobs in the job table



90
91
92
93
94
# File 'lib/bj/api.rb', line 90

def list options = {}, &block
  Bj.transaction(options) do
    table.job.find(:all, options)
  end
end

#migrate(options = {}) ⇒ Object

migrate a database (production|development|etc)



140
141
142
143
144
145
# File 'lib/bj/api.rb', line 140

def migrate options = {}
  options.to_options!
  chroot do
    util.spawn "rake RAILS_ENV=#{ Bj.rails_env } db:migrate", options
  end
end

#run(options = {}) ⇒ Object



98
99
100
# File 'lib/bj/api.rb', line 98

def run options = {}
  runner.run options
end

#setup(options = {}) ⇒ Object

generate a migration and migrate a database (production/development/etc)



104
105
106
107
108
109
110
# File 'lib/bj/api.rb', line 104

def setup options = {}
  options.to_options!
  chroot do
    generate_migration options
    migrate options
  end
end

#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