[Backgroundrb-devel] MY worker won't stop working

Chris H chris at etechdata.com.au
Mon Jul 31 19:30:26 EDT 2006

> On Jul 30, 2006, at 5:11 PM, Chris H wrote:
>> Hi Ezra,
>> thanks for the reply.
>> There's a ruby process that appears in top when I fire off the do_work
>> method.
>> It uses around 30-50% cpu and disappears once all processing has  
>> completed.
>> When I try to stop processing using delete_worker I was expecting this
>> ruby process to stop,
>> but understand/expect that backgroundrb is still running.
>> I tried
>> MiddleMan[:process_watcher].terminate &&  MiddleMan.delete_worker
>> (:process_watcher)
>> however this seems to do less then MiddleMan.delete_worker
>> (:process_watcher)  on its own.
>> As Michael Siebert wrote, will I need to have do_work return when I  
>> want
>> my process to stop?
>> Thanks in advance,
>> Chris.
> Hey Chris-
>     By itself Backgroundrb won't spawn any extra processes except for  
> itself. Are you doing a system call or using `` to call an external  
> program? Or are you using fork at all? Maybe if you paste your worker  
> code and how you are calling it here I can help you track down the  
> issue.
> -Ezra

Hi Ezra,

Sorry I've been a bit misleading.  When viewing top I had the processes 
ordered by cpu % usage.  So when I ran my worker
it appeared at the top and then disappeared off the list once it was 
I'm not using a system call or any external program in my worker.  It's 
main function is to read data from a file into a database.

I've been successful in getting the worker to stop now by adding a 
stop_work method to my worker which sets @running to false in the 
process_cdr class and causes the code in do_work to return.
My woker method stop_work calls processs_cdr.stop_process:

 def stop_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
   if args
     @process_cdr = ProcessCdr.new(args)
     @process_cdr = ProcessCdr.new

When @running is set to false the loop breaks and process() returns.
This is the main code from the process_cdr class:

 def stop_process
   @running = false

 def process
   @running = true
   @time_start = Time.new.to_f
   @files_processed = 0
   phone_service_accounts = PhoneServiceAccount.find(:all)
   phone_service_accounts.each do |myPsa|       process_class_name = 
     @current_account = myPsa.name
     if @file.strip.length == 0
       @phone_service_cdr = PhoneServiceCdr.find(:all, :conditions => 
"processed = 'n'")
       @phone_service_cdr = PhoneServiceCdr.find(:all, :conditions => 
"processed = 'n' AND name='#{@file}'")
     @total_files = @phone_service_cdr.size.to_i         
@phone_service_cdr.each do |myPsc|             @current_file = myPsc.name
       @process_class = 
       finished_processing_cdr = false
       while(@running && !finished_processing_cdr)
         @process_class.process_records do |record|
           PhoneServiceRecord.new do |phone_service_record|
             phone_service_number = PhoneServiceNumber.find(:first, 
:conditions => "number = '#{record[:charged_party]}'")
             customer_id = 0
             customer_id = phone_service_number.customer_id if 
phone_service_number != nil
             phone_service_record.date_time = record[:date_time]
             phone_service_record.duration = record[:duration]
             phone_service_record.originating_number = 
             phone_service_record.terminating_number = 
             phone_service_record.charged_party = record[:charged_party]
             phone_service_record.currency = record[:currency]
             phone_service_record.wholesale_cost = record[:wholesale_cost]
             phone_service_record.customer_cost = record[:customer_cost]
             phone_service_record.distance = record[:distance]
             phone_service_record.is_local = record[:is_local]
             phone_service_record.type_of_call = record[:type_of_call]
             phone_service_record.rate_code = record[:rate_code]
             phone_service_record.batchname = record[:batchname]
             phone_service_record.customer_id = customer_id
             phone_service_record.phone_service_account_id = myPsa.id
         finished_processing_cdr = true
         @files_processed += 1
         myPsc.processed = 'y'
       return false if !@running
   @running = false   end

I'm not to sure if this is the correct way to go about stopping the 
worker.  How does this look to you?

Thanks in advance,

More information about the Backgroundrb-devel mailing list