Skip to content

Commit

Permalink
adds list_runners, dump_*_q methods to backend
Browse files Browse the repository at this point in the history
part of #90
  • Loading branch information
robacarp committed Jul 26, 2022
1 parent 2ca7a94 commit 5bc6a39
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/mosquito/backend.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Mosquito
abstract def store(key : String, value : Hash(String, String)) : Nil
abstract def retrieve(key : String) : Hash(String, String)
abstract def list_queues : Array(String)
abstract def list_runners : Array(String)

# from task.cr
abstract def delete(key : String, in ttl = 0) : Nil
Expand Down Expand Up @@ -67,5 +68,10 @@ module Mosquito
abstract def terminate(task : Task) # should this be called fail?
abstract def flush : Nil
abstract def size(include_dead : Bool = true) : Int64

{% for name in ["waiting", "scheduled", "pending", "dead"] %}
abstract def dump_{{name.id}}_q : Array(String)
{% end %}

end
end
24 changes: 24 additions & 0 deletions src/mosquito/redis_backend.cr
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ module Mosquito
end.uniq.flatten
end

def self.list_runners : Array(String)
runner_prefix = "mosquito:runners:"
Redis.instance.keys("#{runner_prefix}*")
.map(&.as(String))
.map(&.sub(runner_prefix, ""))
end

# is this even a good idea?
def self.flush : Nil
redis.flushall
Expand Down Expand Up @@ -132,5 +139,22 @@ module Mosquito
scheduled_size = redis.zcount scheduled_q, 0, "+inf"
queue_size + scheduled_size
end

{% for name in ["waiting", "scheduled", "pending", "dead"] %}
def dump_{{name.id}}_q : Array(String)
key = {{name.id}}_q
type = Redis.instance.type key

if type == "list"
Redis.instance.lrange(key, 0, -1).map(&.as(String))
elsif type == "zset"
Redis.instance.zrange(key, 0, -1).map(&.as(String))
elsif type == "none"
[] of String
else
raise "don't know how to dump a #{type} for {{name.id}}"
end
end
{% end %}
end
end
24 changes: 24 additions & 0 deletions test/mosquito/redis_backend_test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,28 @@ describe Mosquito::RedisBackend do
assert_equal 4, RedisBackend.increment(key, field, by: 3)
end
end

describe "dump_q" do
it "can dump the waiting q" do
skip
end

it "can dump the scheduled q" do
skip
end

it "can dump the pending q" do
skip
end

it "can dump the dead q" do
skip
end
end

describe "list_runners" do
it "can list runners" do
skip
end
end
end

0 comments on commit 5bc6a39

Please sign in to comment.