[Backgroundrb-devel] Backgroundrb assistance

Diogo Silva diogo.silva at w20.pt
Tue Jan 20 15:46:59 EST 2009


Hi,
Don't know if it's the right address but I'm seeking help with my workers.

I have two problems related to workers and backgroundrb:

1) I have a worker that uses soap/wsdlDriver and it's set to autoload and
starts called by controller. This only makes a simple SOAP communication. It
works most of the times. But sometimes worker dies and the reason is related
with some timeout related with httpclient gem. When it dies it's a pain to
make it work again.
Down it's the code of my controller:

 def index
    prepare_job
    @id = "prepare"
    render 'admin/importar/worker'
  end

  def import
    execute_job
    @id = "execute"
    render 'admin/importar/worker'
  end

  def prepare_job
    start_worker("PREPARE_IMPORT")
  end

  def execute_job
    start_worker("EXECUTE_IMPORT")
  end

  def start_worker(action)
    worker = MiddleMan.ask_status(:worker => :iif_worker)

    if worker != nil && worker[:progress] < 4
      flash[:notice] = "Já existe um processo de importação a decorrer!"
    else
      MiddleMan.ask_work(:worker => :iif_worker, :worker_method => :do_work,
:data => action)
      session[:job_key] = :import
    end
  end

  def get_progress
    if request.xhr?
      worker = MiddleMan.ask_status(:worker => :iif_worker)
      return if worker == nil

      progress_percent = worker[:progress]
      render :update do |page|
        page.call('progressPercent', 'progressbar', progress_percent)
        if progress_percent >= 100
          @info = worker[:info]
          @total = worker[:total]
          if params[:id] == "prepare"
            page.replace_html 'import_content', :partial =>
'admin/importar/todo'
          elsif params[:id] == "execute"
            flash[:notice] = "#{@total} marcas importas com sucesso!"
            page.replace_html 'import_content', :partial =>
'admin/importar/done'
          end
        end
      end
    else
     redirect_to :action => 'index'
    end
   end

And below some code of worker:

require 'rexml/document'
require 'soap/wsdlDriver'
require 'xsd/mapping'

class IifWorker < BackgrounDRb::MetaWorker
  set_worker_name :iif_worker
  #set_no_auto_load(true)
  #attr_reader :progress, :info, :total

  #def create(args = nil)
  #  register_status('Dass')
  #  Thread.abort_on_exception = true
  #end

  def do_work(action)
    @progress = 0
    update_status
    if action == "PREPARE_IMPORT"
      prepare_import
    elsif action == "EXECUTE_IMPORT"
      execute_import
    end
    @progress = 100
    update_status
  end

  def prepare_import
    stuff, chave = auth_and_query
    results = stuff.goldMineData.record

    @total = results.size
      @progress = 15
      @increment = ("%4.2f" % ((100.0 - @progress) / results.size)).to_f
    @info = findRecordsToUpdate(results)

    update_status

    @soap.reset_stream
  end

  def execute_import
    stuff, chave = auth_and_query
    results = stuff.goldMineData.record

    @total = results.size
      @progress = 15
      @increment = ("%4.2f" % ((100.0 - @progress) / results.size)).to_f
    @info = updateRecords(stuff.goldMineData.record)
    #@soap.UpdateGoldmine(:Key=>chave)

    update_status

    @soap.reset_stream
  end

  def auth_and_query
    factory = SOAP::WSDLDriverFactory.new("
http://infofranchisingwsp.ife.pt:8081/infofranchisingUpdateService.asmx?WSDL
")
    @soap = factory.create_rpc_driver
    soapResponse = @soap.GetToken(nil)
    token = soapResponse.getTokenResult

    chave = authenticate(token)
      @progress = 5
      update_status
    soapResponseXML = @soap.GetXML(:Key=>chave)
      @progress = 10
      update_status

    return soapResponseXML.getXMLResult, chave
  end

  def update_status
    register_status({:progress => @progress, :info => @info, :total =>
@total})
  end

  def increment_progress
    @progress += @increment
    @progress = ("%3.2f" % @progress).to_f
    @progress = 100 if @progress > 100
  end

  def findRecordsToUpdate(results)
(...)

Can you help me solve this problem?

Thanks in advance,
Diogo Silva
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/backgroundrb-devel/attachments/20090120/e1923403/attachment.html>


More information about the Backgroundrb-devel mailing list