Module: Bj::API
- Defined in:
- lib/bj/api.rb
Overview
Bj.table.job.find :all
Instance Method Summary collapse
-
#generate_migration(options = {}) ⇒ Object
generate_migration, suprisingly, generates the single migration needed for bj.
-
#in(rails_env = Bj.rails_env, &block) ⇒ Object
this method changes the context under which bj is operating.
-
#list(options = {}, &block) ⇒ Object
list simply returns a list of all jobs in the job table.
-
#migrate(options = {}) ⇒ Object
migrate a database (production|development|etc).
-
#plugin(options = {}) ⇒ Object
install plugin into this rails app.
- #run(options = {}) ⇒ Object
-
#setup(options = {}) ⇒ Object
generate a migration and migrate a database (production/development/etc).
-
#submit(jobs, options = {}, &block) ⇒ Object
submit jobs for background processing.
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.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/bj/api.rb', line 119 def generate_migration = {} . [:verbose] = true chroot do before = Dir.glob "./db/migrate/*" n = Dir.glob("./db/migrate/*_bj_*").size classname = "BjMigration#{ n }" util.spawn "#{ Bj.ruby } ./script/generate migration #{ classname }", 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(classname)} 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 95 96 |
# File 'lib/bj/api.rb', line 90 def list = {}, &block . Bj.transaction() do .delete :rails_env table.job.find(:all, ) end end |
#migrate(options = {}) ⇒ Object
migrate a database (production|development|etc)
145 146 147 148 149 150 151 |
# File 'lib/bj/api.rb', line 145 def migrate = {} . [:verbose] = true chroot do util.spawn "#{ Bj.rake } RAILS_ENV=#{ Bj.rails_env } db:migrate", end end |
#plugin(options = {}) ⇒ Object
install plugin into this rails app
155 156 157 158 159 160 161 |
# File 'lib/bj/api.rb', line 155 def plugin = {} . [:verbose] = true chroot do util.spawn "#{ Bj.ruby } ./script/plugin install http://codeforpeople.rubyforge.org/svn/rails/plugins/bj --force", end end |
#run(options = {}) ⇒ Object
100 101 102 |
# File 'lib/bj/api.rb', line 100 def run = {} runner.run end |
#setup(options = {}) ⇒ Object
generate a migration and migrate a database (production/development/etc)
106 107 108 109 110 111 112 |
# File 'lib/bj/api.rb', line 106 def setup = {} . chroot do generate_migration migrate 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, = {}, &block . Bj.transaction() do table.job.submit jobs, , &block end ensure Bj.runner.tickle unless [:no_tickle] end |