Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with character encoding in Resque
    primarykey
    data
    text
    <p>I have a series of JSON messages that I need to process and I've run into some trouble with character encodings. I retrieve this message from a RabbitMQ queue like so:</p> <pre><code>@data = [] @queue.pop do |metadata, payload| parsed_message = JSON.parse(payload) @data &lt;&lt; parsed_message end </code></pre> <p>I then process them using Resque and enqueue them like so:</p> <pre><code>Resque.enqueue(Worker, @data) </code></pre> <p>When I run the worker it throws this error:</p> <pre><code>** [16:06:13 2012-09-14] 5375: Error reserving job: #&lt;Encoding::InvalidByteSequenceError: "\xC3" on US-ASCII&gt; ** [16:06:13 2012-09-14] 5375: /usr/local/lib/ruby/1.9.1/json/common.rb:148:in `encode' /usr/local/lib/ruby/1.9.1/json/common.rb:148:in `initialize' /usr/local/lib/ruby/1.9.1/json/common.rb:148:in `new' /usr/local/lib/ruby/1.9.1/json/common.rb:148:in `parse' /usr/local/lib/ruby/gems/1.9.1/gems/multi_json-1.3.6/lib/multi_json/adapters/json_common.rb:7:in `load' /usr/local/lib/ruby/gems/1.9.1/gems/multi_json-1.3.6/lib/multi_json.rb:93:in `load' /usr/local/lib/ruby/gems/1.9.1/gems/resque-1.22.0/lib/resque/helpers.rb:38:in `decode' /usr/local/lib/ruby/gems/1.9.1/gems/resque-1.22.0/lib/resque.rb:149:in `pop' /usr/local/lib/ruby/gems/1.9.1/gems/resque-1.22.0/lib/resque/job.rb:100:in `reserve' /usr/local/lib/ruby/gems/1.9.1/gems/resque-1.22.0/lib/resque.rb:303:in `reserve' /usr/local/lib/ruby/gems/1.9.1/gems/resque-1.22.0/lib/resque/worker.rb:209:in `block in reserve' /usr/local/lib/ruby/gems/1.9.1/gems/resque-1.22.0/lib/resque/worker.rb:207:in `each' /usr/local/lib/ruby/gems/1.9.1/gems/resque-1.22.0/lib/resque/worker.rb:207:in `reserve' /usr/local/lib/ruby/gems/1.9.1/gems/resque-1.22.0/lib/resque/worker.rb:136:in `block in work' /usr/local/lib/ruby/gems/1.9.1/gems/resque-1.22.0/lib/resque/worker.rb:133:in `loop' /usr/local/lib/ruby/gems/1.9.1/gems/resque-1.22.0/lib/resque/worker.rb:133:in `work' /usr/local/lib/ruby/gems/1.9.1/gems/resque-1.22.0/lib/resque/tasks.rb:36:in `block (2 levels) in &lt;top (required)&gt;' /usr/local/lib/ruby/1.9.1/rake/task.rb:205:in `call' /usr/local/lib/ruby/1.9.1/rake/task.rb:205:in `block in execute' /usr/local/lib/ruby/1.9.1/rake/task.rb:200:in `each' /usr/local/lib/ruby/1.9.1/rake/task.rb:200:in `execute' /usr/local/lib/ruby/1.9.1/rake/task.rb:158:in `block in invoke_with_call_chain' /usr/local/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' /usr/local/lib/ruby/1.9.1/rake/task.rb:151:in `invoke_with_call_chain' /usr/local/lib/ruby/1.9.1/rake/task.rb:144:in `invoke' /usr/local/lib/ruby/1.9.1/rake/application.rb:116:in `invoke_task' /usr/local/lib/ruby/1.9.1/rake/application.rb:94:in `block (2 levels) in top_level' /usr/local/lib/ruby/1.9.1/rake/application.rb:94:in `each' /usr/local/lib/ruby/1.9.1/rake/application.rb:94:in `block in top_level' /usr/local/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling' /usr/local/lib/ruby/1.9.1/rake/application.rb:88:in `top_level' /usr/local/lib/ruby/1.9.1/rake/application.rb:66:in `block in run' /usr/local/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling' /usr/local/lib/ruby/1.9.1/rake/application.rb:63:in `run' /usr/local/bin/rake:32:in `&lt;main&gt;' rake aborted! </code></pre> <p>I've wrapped all the code in the perform function from the worker in a begin/rescue block, to try to catch the exception but to no avail which leads me to think that it's from inside Resque somewhere.</p> <p>I've tried enqueueing the Resque worker with the raw data and then parse it inside the worker into JSON and setting an UTF-8 encoding:</p> <pre><code>raw_data.each do |msg| msg = msg.encode("UTF-8", :invalid =&gt; :replace, :undef =&gt; :replace, :replace =&gt; "") parsed_msg = JSON.parse(msg) rescue nil next if parsed_msg.nil? parsed_data &lt;&lt; parsed_msg end </code></pre> <p>and I still get the same error. It's not always the same character, sometimes it's \xC2 or \xD8 if I remember correctly</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload