Search code examples
rubybashredisredis-cli

Pushing a JSON payload to redis via Ruby and redis-cli


I am trying to have a Ruby script lpush a json payload to redis via redis-cli, but somehow I'm not getting the quotes right:

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}`

I must be messing up the quoting somehow, but I don't see where.


Solution

  • Your json is interpolated twice, getting rid of slashes. Use single quotes around it:

    json = '{\"retry\":true,\"queue\":\"tests\",\"class\":\"TestRunnerJob\",\"args\":[#{ARGV[0].to_i}],\"jid\":\"#{jid}\",\"enqueued_at\":#{timestamp}}"'