通过 Ruby 和 redis-cli 将 JSON 有效载荷推送到 redis
Pushing a JSON payload to redis via Ruby and redis-cli
我正在尝试让一个 Ruby 脚本通过 redis-cli 将 json 有效负载推送到 redis,但不知何故我没有得到正确的引号:
timestamp = `date +%s.%N`.strip
jid = `cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 24 | head -n 1`.strip
json = "{\"retry\":true,\"queue\":\"tests\",\"class\":\"TestRunnerJob\",\"args\":[#{ARGV[0].to_i}],\"jid\":\"#{jid}\",\"enqueued_at\":#{timestamp}}"
cmd = <<-eos
echo lpush "queue:tests" \"#{json}\" |redis-cli -h localhost -n 12 -d ';'
eos
p `#{cmd.strip}`
我一定是搞砸了引用,但我看不出在哪里。
你的 json
被插值了两次,去掉了斜线。在它周围使用单引号:
json = '{\"retry\":true,\"queue\":\"tests\",\"class\":\"TestRunnerJob\",\"args\":[#{ARGV[0].to_i}],\"jid\":\"#{jid}\",\"enqueued_at\":#{timestamp}}"'
我正在尝试让一个 Ruby 脚本通过 redis-cli 将 json 有效负载推送到 redis,但不知何故我没有得到正确的引号:
timestamp = `date +%s.%N`.strip
jid = `cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 24 | head -n 1`.strip
json = "{\"retry\":true,\"queue\":\"tests\",\"class\":\"TestRunnerJob\",\"args\":[#{ARGV[0].to_i}],\"jid\":\"#{jid}\",\"enqueued_at\":#{timestamp}}"
cmd = <<-eos
echo lpush "queue:tests" \"#{json}\" |redis-cli -h localhost -n 12 -d ';'
eos
p `#{cmd.strip}`
我一定是搞砸了引用,但我看不出在哪里。
你的 json
被插值了两次,去掉了斜线。在它周围使用单引号:
json = '{\"retry\":true,\"queue\":\"tests\",\"class\":\"TestRunnerJob\",\"args\":[#{ARGV[0].to_i}],\"jid\":\"#{jid}\",\"enqueued_at\":#{timestamp}}"'