Skip to content

Commit

Permalink
Merge pull request #160 from edgurgel/fix-enqueue-retriable-job
Browse files Browse the repository at this point in the history
Change enqueued_retriable_job to populate enqueued_at if JSON null
  • Loading branch information
edgurgel authored May 10, 2018
2 parents cd7a774 + 8d07796 commit 80050f9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
3 changes: 1 addition & 2 deletions priv/enqueue_retriable_job.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if job then
if queue then
local queue_key = string.format('queue:%s', queue)
local enqueued_at = decoded["enqueued_at"]
if not enqueued_at then
if enqueued_at == cjson.null or not enqueued_at then
decoded["enqueued_at"] = ARGV[1]
end
local reencoded = cjson.encode(decoded)
Expand All @@ -17,4 +17,3 @@ if job then
end

return job

19 changes: 15 additions & 4 deletions test/redis_scripts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,25 @@ defmodule RedisScriptsTest do
end

test "enqueue job to queue form schedule set", %{ redis: redis } do
schedulled_job = "{\"jid\":\"123\",\"queue\":\"test_queue\"}"
enqueued_shedulled_job = "{\"jid\":\"123\",\"enqueued_at\":\"42\",\"queue\":\"test_queue\"}"
scheduled_job = "{\"jid\":\"123\",\"queue\":\"test_queue\"}"
enqueued_scheduled_job = "{\"jid\":\"123\",\"enqueued_at\":\"42\",\"queue\":\"test_queue\"}"

{ :ok, _ } = Redix.command(redis, ~w(DEL schedule queue:test_queue))
{ :ok, _ } = Redix.command(redis, ~w(ZADD schedule 42 #{schedulled_job}))
{ :ok, _ } = Redix.command(redis, ~w(ZADD schedule 42 #{scheduled_job}))

assert Redix.command(redis, ["EVAL", @enqueue_retriable_job_script, 1, "schedule", "41"]) == { :ok, nil }
assert Redix.command(redis, ["EVAL", @enqueue_retriable_job_script, 1, "schedule", "42"]) == { :ok, enqueued_shedulled_job }
assert Redix.command(redis, ["EVAL", @enqueue_retriable_job_script, 1, "schedule", "42"]) == { :ok, enqueued_scheduled_job }
end

test "enqueue job to queue form null enqueued_at key", %{ redis: redis } do
scheduled_job = "{\"jid\":\"123\",\"enqueued_at\":null,\"queue\":\"test_queue\"}"
enqueued_scheduled_job = "{\"jid\":\"123\",\"enqueued_at\":\"42\",\"queue\":\"test_queue\"}"

{ :ok, _ } = Redix.command(redis, ~w(DEL schedule queue:test_queue))
{ :ok, _ } = Redix.command(redis, ~w(ZADD schedule 42 #{scheduled_job}))

assert Redix.command(redis, ["EVAL", @enqueue_retriable_job_script, 1, "schedule", "41"]) == { :ok, nil }
assert Redix.command(redis, ["EVAL", @enqueue_retriable_job_script, 1, "schedule", "42"]) == { :ok, enqueued_scheduled_job }
end
end

Expand Down

0 comments on commit 80050f9

Please sign in to comment.