【问题标题】:Ruby ZIP file encoding for sending to Sidekiq / Redis用于发送到 Sidekiq / Redis 的 Ruby ZIP 文件编码
【发布时间】:2019-10-13 00:32:07
【问题描述】:

我使用以下代码构建了一个 ZIP 文件:

def compress_batch(directory_path)
  zip_file_path = File.join( File.expand_path("..", directory_path), SecureRandom.hex(10))
  Zip::File.open(zip_file_path, Zip::File::CREATE) do |zip_file|
    (Dir.entries(directory_path) - %w(. ..)).each do |file_name|
      zip_file.add file_name, File.join(directory_path, file_name)
    end
  end

  result = File.open(zip_file_path, 'rb').read
  File.unlink(zip_file_path)
  result
end

我将该 ZIP 文件存储在内存中:

@result = Payoff::DataFeed::Compress::ZipCompress.new.compress_batch(source_path)

我把它放入哈希中:

options = {
  data: @result
}

然后我使用perform_async 将其提交给我的 SideKiq 工作人员:

DeliveryWorker.perform_async(options)

并得到以下错误:

[DEBUG]   Starting store to: { "destination" => "sftp", "path" => "INBOUND/20191009.zip" }
Encoding::UndefinedConversionError: "\xBA" from ASCII-8BIT to UTF-8
from ruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/core_ext/object/json.rb:34:in `encode'

但是,如果我使用 .new.perform 而不是 .perform_async,绕过 SideKiq,它可以正常工作!

DeliveryWorker.new.perform(options)

我最好的猜测是我的编码有问题,以至于当工作转到 SideKiq / Redis 时,它会爆炸。我应该如何编码?我是否需要更改 ZIP 文件的创建?也许我可以在提交到 SideKiq 时转换编码?

【问题讨论】:

  • github.com/mperham/sidekiq/wiki/… 当您执行 .new.perform 时它会起作用,因为没有任何内容会被序列化为 JSON 并推送到 Redis - 直接调用该作业。

标签: ruby encoding redis sidekiq


【解决方案1】:

Sidekiq 将参数序列化为 JSON。您正在尝试将二进制数据填充到仅支持 UTF-8 字符串的 JSON 中。如果您希望通过 Redis 传递数据,则需要对数据进行 Base64 编码。

require 'base64'

encoded = Base64.encode64(filedata)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2014-04-06
    • 2019-01-06
    • 1970-01-01
    相关资源
    最近更新 更多