[Backgroundrb-devel] BackgrounDRb not working properly with files ?

Tim Anglade tim.anglade at gmail.com
Wed Jul 19 09:49:46 EDT 2006

Hey there,

I'm kinda new to BackgrounDRb but I'm facing a big problem which I
can't seem to wrap my head around.

The short version of it is : code that works when tested inside RoR's
console doesn't work when executed by a BackgrounDRb worker.

So I've tried a lot of things, including checking that the
"load_rails" was true in the config, and I'm starting to wonder if
this is not some kind of problem between BackgrounDRb and files or
BackgrounDRb and file_column (which I'm using here).

Any idea or help is greatly appreciated.

My models are basically :
- Clip has_many records
- Record belongs_to Clip with :path as the file_column field.

What I'm trying to do here is import a batch of files from a directory
that's passed as the args to the worker and add them as a new records
to new clips.

Again, this code is working perfectly from inside the console, but not
when I activate this worker through the application (the worker does
start, show the progress bar and completes the action, the clips and
records are created but the files are not attached correctly to the

My hinch is that the worker cannot understand that :path is a
file_column and that the file object (fl) is not passed correctly, but
I could be completely off-target.

Here's my worker's do_work :

def do_work(args)
    # This method is called in it's own new thread when you
    # call new worker. args is set to :args

      #We define the variables used to monitor the progress
      #@progress is used by the controller for the progress bar,
      #file_number is used to calculate the progress
      @progress = 0
      file_number = 0

      #Checking if the arg is indeed a directory
      if File.directory?(args)

        path = args

        #we get number_of_files to calculate the progress
        number_of_files = Dir.entries(path).size

        #Let's get into that dir and look inside
        Dir.open(path) do |dir|
          dir.each do |entry|
            #We ignore ".", ".." (included in ruby dir listings)
            #we also ignore sub-directories and hidden files
            unless  ["..", "."].include?(entry) or
File.directory?(entry) or entry.match('^\.')
              puts "treating file : "+entry

              #First we create a new clip with the filename
              cl = Clip.new(:title => entry)

              #We get a File object out of the path
              fl = File.open(path+entry)

              #We create a new record. path is the file_column defined
in the model.
              rec = cl.records.new(:path => fl)

              #... and we save everything.
              file_number += 1
              @progress = file_number * 100 / number_of_files

        puts "Gimme a directory !"

Any help greatly apreciated.


More information about the Backgroundrb-devel mailing list