大型导轨后台进程无法完成的问题

问题描述 投票:0回答:1

我有一个rails后台进程(使用Sidekiq和Redis)解析XML文件,然后对其进行修改。

后台进程按预期工作,但仍处于处理状态,并且在XML非常大时无法完成。我对此的两个假设是:

  1. 我的后台进程是将XML中的大量文本存储到数组old_textsnew_texts中,这会导致问题
  2. 我的后台流程正在超时

我的开发机器(&staging)上都会出现问题。

我不知道如何调试这个问题。我不认为发布我的代码会有所帮助,但我会这样做,以防你需要知道我在做什么:

old_texts, new_texts = [], []

xml_no_includs = ["pctHeight","pctWidth","posOffset","delText","delInstrText","instrText"]    

search = '//w:document//w:body//w:p'

ancestors_excluds = ['//mc:Fallback', '//w:tbl', '//wps:txbx', '//v:textbox']

old_texts, new_texts = get_texts(old_texts, new_texts, XML, xml_no_includs, ancestors_excluds, search)

search_param = '//w:document//w:body//w:p'

ancestors_excluds = ['//mc:Fallback', '//w:tbl', '//wps:txbx', '//v:textbox']

replace_texts(old_texts, new_texts, XML, search_param, ancestors_excluds)

-

def replace_texts(old_texts, new_texts, XML, search_param, ancestors_excluds)
  text_params = './/text()[not(ancestor::wp14:pctHeight or ancestor::wp14:pctWidth or ancestor::wp:posOffset or ancestor::w:instrText or ancestor::w:delText or ancestor::w:delInstrText)]'
  inc = 0

  old_texts.each_with_index do |old_text, index|
    accum_string = ''
    double_break = false
    XML.search(search_param).drop(inc).each do |line|
      inc += 1
      temp = true
        line.search(text_params).each do |p|
          temp2 = true
          ancestors_excluds.each do |param|
            temp2 = false if p.ancestors(param).present? 
          end
          if temp2 == true
            if accum_string.blank? && !p.content.blank?
              accum_string += p.content
              p.content = new_texts[index]
            else
              accum_string += p.content unless accum_string.blank?
              p.content = ''
            end
            if accum_string.strip == old_text.strip            
              double_break = true
              break
            end
          end
        end    
      break if double_break == true
    end
  end
end

-

def get_texts(old_texts, new_texts, XML, xml_no_includs, ancestors_excluds, search_param)
  XML.xpath(search_param).each do |p|
    text = ''
    temp = true
    p.search('text()').each do |p2|
      temp2 = true
      temp2 = false if xml_no_includs.include?(p2.parent.name)
      ancestors_excluds.each do |param|
        temp2 = false if p2.ancestors(param).present? 
      end
      text += p2.text if temp2 == true
    end
    unless text.blank?
      old_texts.append(text) 
      new_texts.append(text.gsub(/(.)./, '\1*') )
    end
  end
  old_texts.reject!(&:blank?)
  new_texts.reject!(&:blank?)

  return old_texts, new_texts
end
ruby-on-rails xml redis background-process sidekiq
1个回答
0
投票

我最终重构了我的代码来管理最初将被推入数组的每个元素。没有加快速度,但至少后台任务在几个小时后完成。

© www.soinside.com 2019 - 2024. All rights reserved.