1

我正在尝试REXML::element使用 sidekiq 向工作人员发送邮件。但是,当我这样做时,我得到一个错误:ActiveSupport::JSON::Encoding::CircularReferenceError

在这种情况下,我将获取一个在线存储的 XML 文档并运行一个将其划分为多个部分的工作人员,然后创建子工作人员来处理每个部分。

require "rexml/document"
require 'open-uri'
class GroupUploadWorker
  include Sidekiq::Worker
  include REXML

  def perform(user_id, ip_address)
    url = URI.parse('https://s3-us-west-2.amazonaws.com/nick-grn-dev/alldata.xml')
    open(url) do |http|
      @response = http.read
    end
    @doc = Document.new(@response.force_encoding("ISO-8859-1").encode("UTF-8"))
    @doc.root.elements[1].elements.each do |group|
      SingelGroupUploadWorker.perform_async(user_id, ip_address, group)
    end
  end
end

我发现的问题是 group 变量的传递。如果我传递一个字符串但组变量是REXML::Element. 恐怕我对系统的理解不够好,无法理解错误。我在网上做了一些研究,但我不认为我在问正确的问题。非常感激任何的帮助。

有人建议我应该为此添加一个堆栈跟踪。我得到的错误是在 sidekiq 中,所以我不知道如何得到,直到我在控制台中重现了错误。这就是控制台中发生的事情。

1.9.3p286 :011 > SingelGroupUploadWorker.perform_async(1, '127.0.0.1', @doc.root.elements[1].elements[1])
2013-09-26T01:49:32Z 37015 TID-oxoz25ihc INFO: Sidekiq client using redis://localhost:6379/0 with options {:size=>1}
ActiveSupport::JSON::Encoding::CircularReferenceError: object references itself
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-    3.2.11/lib/active_support/json/encoding.rb:75:in `check_for_circular_references'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/json/encoding.rb:46:in `encode'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/json/encoding.rb:252:in `block in encode_json'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/json/encoding.rb:252:in `each'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/json/encoding.rb:252:in `map'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/json/encoding.rb:252:in `encode_json'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/json/encoding.rb:220:in `block in encode_json'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/json/encoding.rb:220:in `map'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/json/encoding.rb:220:in `encode_json'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/json/encoding.rb:48:in `block in encode'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/json/encoding.rb:77:in `check_for_circular_references'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/json/encoding.rb:46:in `encode'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/json/encoding.rb:31:in `encode'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.11/lib/active_support/core_ext/object/to_json.rb:16:in `to_json'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/json-1.8.0/lib/json/common.rb:223:in `generate'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/json-1.8.0/lib/json/common.rb:223:in `generate'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/sidekiq-2.13.1/lib/sidekiq.rb:97:in `dump_json'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/sidekiq-2.13.1/lib/sidekiq/client.rb:103:in `block (2 levels) in raw_push'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/sidekiq-2.13.1/lib/sidekiq/client.rb:103:in `map'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/sidekiq-2.13.1/lib/sidekiq/client.rb:103:in `block in raw_push'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/connection_pool-1.1.0/lib/connection_pool.rb:49:in `with'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/sidekiq-2.13.1/lib/sidekiq.rb:67:in `redis'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/sidekiq-2.13.1/lib/sidekiq/client.rb:95:in `raw_push'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/sidekiq-2.13.1/lib/sidekiq/client.rb:44:in `push'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/sidekiq-2.13.1/lib/sidekiq/worker.rb:85:in `client_push'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/sidekiq-2.13.1/lib/sidekiq/worker.rb:40:in `perform_async'
from (irb):11
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /Users/suddenlyaware/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
4

1 回答 1

0

我发现将任何类型的复杂对象传递给 perform 方法都会导致类似的问题。我所做的是尝试将参数简化为简单的字符串或数字。

于 2013-10-19T18:56:07.953 回答