# \[ANN\] bj-0.0.2

**URL:** <https://www.ruby-forum.com/t/ann-bj-0-0-2/122966>\
**Category:** Rails\
**Created:** [December 13, 2007, 2:17am UTC](https://www.ruby-forum.com/t/ann-bj-0-0-2/122966 "2007-12-13T02:17:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 13, 2007, 2:17am UTC](https://www.ruby-forum.com/t/ann-bj-0-0-2/122966/1 "2007-12-13T02:17:55Z")

</div>

NAME  
bj

SYNOPSIS  
bj (migration\_code|generate\_migration|migrate|setup|run|submit|list|  
set|config|pid) [options]+

DESCRIPTION

* * *

## Overview

```
Backgroundjob (Bj) is a simple to use background priority queue

```

for rails.  
Although not yet tested on windows, the design of bj is such that  
operation  
should be possible on any operating system, including M$.

```
Jobs can be submitted to the queue directly using the api or from

```

the  
commandline using the ‘bj’ script. For example

```
code:
    Bj.submit 'cat /etc/password'

  cli:
    bj submit cat /etc/password

When used from inside a rails application bj arranges that another

```

process  
will always be running in the background to process the jobs that  
you submit.  
By using a separate process to run jobs bj does not impact the  
resource  
utilization of your rails application at all and enables several  
very cool  
features:

```
  1) Bj allows you to sumbit jobs to any of your configured

```

databases and,  
in each case, spawns a separate background process to run jobs  
from that  
queue

```
    Bj.in :production do
      Bj.submit 'production_job.exe'
    end

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

  2) Although bj ensures that a process is always running to

```

process  
your jobs, you can start a proces manually. This means that any  
machine  
capable of seeing your RAILS\_ROOT can run jobs for your  
application, allowing  
one to setup a cluster of machines doing the work of a single  
front end rails  
applicaiton.

* * *

## Install

```
Bj can be installed two ways: as a gem or as a plugin.

  gem:
    1) $sudo gem install bj
    2) add "require 'bj'" to config/environment.rb
    3) bj setup

  plugin:
    1) ./script/plugin install 

```

[http://codeforpeople.rubyforge.org/svn/rails/plugins/bj](http://codeforpeople.rubyforge.org/svn/rails/plugins/bj)  
2) ./script/bj setup

* * *

## Api

```
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

```

See lib/bj/api.rb for more details.

* * *

## Sponsors

```
http://www.engineyard.com/
http://quintess.com/
http://eparklabs.com/

```

PARAMETERS  
–rails\_root=rails\_root, -R (0 ~\> rails\_root=/Users/ahoward/  
rails\_root)  
the rails\_root will be guessed unless you set this  
–rails\_env=rails\_env, -E (0 ~\> rails\_env=development)  
set the rails\_env  
–log=log, -l (0 ~\> log=STDERR)  
set the logfile  
–help, -h

AUTHOR  
removed\_email\_address@domain.invalid

URIS  
[http://codeforpeople.com/lib/ruby/](http://codeforpeople.com/lib/ruby/)  
[http://rubyforge.org/projects/codeforpeople/](http://rubyforge.org/projects/codeforpeople/)  
[http://codeforpeople.rubyforge.org/svn/rails/plugins/](http://codeforpeople.rubyforge.org/svn/rails/plugins/)

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 13, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/ann-bj-0-0-2/122966/2 "2007-12-13T16:12:00Z")

</div>

On Wed, 12 Dec 2007, ara.t.howard wrote:

> NAME  
> bj

Not exactly the best name… 🙂

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 13, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/ann-bj-0-0-2/122966/3 "2007-12-13T20:36:45Z")

</div>

ara.t.howard,

I need to spawn uploading very large file uploads for a Rails app and  
this looks like it would work for my purposes.

Could you confirm that? Thank you.

Cody S.

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 13, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/ann-bj-0-0-2/122966/4 "2007-12-13T21:20:44Z")

</div>

Eno wrote:

> On Wed, 12 Dec 2007, ara.t.howard wrote:
> 
> > NAME  
> > bj
> 
> Not exactly the best name… 🙂

Hey, if it works though you feel good, relaxed, you lean back in our  
chair happy, with a stupid smile on your face just like. . .I think it’s  
a great name

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 13, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/ann-bj-0-0-2/122966/5 "2007-12-13T21:26:38Z")

</div>

On Dec 13, 12:36 pm, Cody S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ara.t.howard,
> 
> I need to spawn uploading very large file uploads for a Rails app and  
> this looks like it would work for my purposes.
> 
> Could you confirm that? Thank you.
> 
> ## Cody S.
> 
> Posted viahttp://www.ruby-forum.com/.  
> .  
> not really. spawn, backgrounddrb, and bj all serve very different  
> purposes. for better or worse the rail conversation seems to be  
> happening over here

[http://groups.google.com/group/ruby-talk-google/browse\_frm/thread/144e9b3644ffe88e#38b99ec30098b956](http://groups.google.com/group/ruby-talk-google/browse_frm/thread/144e9b3644ffe88e#38b99ec30098b956)

join in! 😉

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 20, 2007, 12:45am UTC](https://www.ruby-forum.com/t/ann-bj-0-0-2/122966/6 "2007-12-20T00:45:26Z")

</div>

On 12/12/07, ara.t.howard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> NAME  
> bj

Hey Ara, sorry to post a ticket like this… rubyforge has been down  
for me all day (local internet issues, no clue…) I had a few issues  
using the plugin I wanted to mention before I forgot.

One is that freezing in the gems doesn’t really work because they try  
to load each other. The ‘main’ gem has tries to load the ‘attributes’  
gem, even if its frozen inside the bj plugin. Not a big deal since I  
can just load the gems…

The more important issue is that your definition of #instance\_methods  
differs from the ruby core api (which includes an extra argument).  
This breaks gems like aws-s3. Here’s a patch  
([Parked at Loopia](http://pastie.caboo.se/130739)), but I’m not sure if it’ll break for  
users on older versions of ruby. At any rate, just thought I’d let  
you know.

Thanks for the library though. It seems like it’ll work a lot better  
than my own job queue service…

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [December 21, 2007, 11:41pm UTC](https://www.ruby-forum.com/t/ann-bj-0-0-2/122966/7 "2007-12-21T23:41:05Z")

</div>

On Dec 19, 4:44 pm, “Rick O.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 12/12/07, ara.t.howard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > NAME  
> > bj
> 
> Hey Ara, sorry to post a ticket like this… rubyforge has been down  
> for me all day (local internet issues, no clue…) I had a few issues  
> using the plugin I wanted to mention before I forgot.

thanks for tip rick!

> One is that freezing in the gems doesn’t really work because they try  
> to load each other. The ‘main’ gem has tries to load the ‘attributes’  
> gem, even if its frozen inside the bj plugin. Not a big deal since I  
> can just load the gems…

yeah this bums me out and i haven’t found a good work around. i’m  
playing with setting GEM\_PATH now, but even that doesn’t really seem  
to work ;-(

> The more important issue is that your definition of #instance\_methods  
> differs from the ruby core api (which includes an extra argument).  
> This breaks gems like aws-s3. Here’s a patch  
> ([Parked at Loopia](http://pastie.caboo.se/130739)), but I’m not sure if it’ll break for  
> users on older versions of ruby. At any rate, just thought I’d let  
> you know.

thanks - that, and bunch of other stuff, is fixed in the new version -  
due out today or tomorrow.

> Thanks for the library though. It seems like it’ll work a lot better  
> than my own job queue service…

glad to hear you are liking it!

-a
