Skip to content

Commit

Permalink
Better Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasjpr committed Jul 30, 2021
1 parent c37fbbc commit b00555a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 23 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,15 @@ Defining Queues: Queues are of type `Hash(String, Queue(T))` where the name of t

- **Name:** `queue:email`
- **Number Workers:** 10
- **Job Class:** TestJob - a class or union of classes

### Example

```crystal
module JoobQ
QUEUES = {
"queue:Email" => JoobQ::Queue(EmailJob).new("queue:Email", 1),
"queue:Fail" => JoobQ::Queue(FailJob).new("queue:Fail", 1),
"queue:Test" => JoobQ::Queue(TestJob).new("queue:Test", 1),
}
JoobQ.configure do
queue "queue:Email", 1, EmailJob
queue "queue:Fail", 1, FailJob
queue "queue:Test", 1, TestJob
end
```

Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: joobq
version: 0.2.4
version: 0.2.5

authors:
- Elias J. Perez <eliasjpr@gmail.com>
Expand Down
8 changes: 3 additions & 5 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ struct Job1
end
end

module JoobQ
QUEUES = {
"single" => Queue(Job1).new("single", 10),
"example" => Queue(ExampleJob | FailJob).new("example", 1),
}
JoobQ.configure do
queue "single", 10, Job1
queue "example", 10, ExampleJob | FailJob
end
15 changes: 7 additions & 8 deletions src/joobq.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ require "cron_parser"
require "./joobq/**"

module JoobQ
REDIS = Redis::PooledClient.new(
host: ENV.fetch("REDIS_HOST", "localhost"),
port: ENV.fetch("REDIS_PORT", "6379").to_i,
pool_size: ENV.fetch("REDIS_POOL_SIZE", "50").to_i,
pool_timeout: ENV.fetch("REDIS_TIMEOUT", "0.2").to_f
)
REDIS = Configure::INSTANCE.redis

Log.setup_from_env(default_level: :trace)

def self.configure
with Configure::INSTANCE yield
end

def self.queues
QUEUES
Configure::INSTANCE.queues
end

def self.statistics
Expand All @@ -37,7 +36,7 @@ module JoobQ
end

def self.[](name : String)
QUEUES[name]
queues[name]
end

def self.reset
Expand Down
20 changes: 20 additions & 0 deletions src/joobq/configure.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module JoobQ
class Configure
INSTANCE = new

getter redis : Redis::PooledClient = Redis::PooledClient.new(
host: ENV.fetch("REDIS_HOST", "localhost"),
port: ENV.fetch("REDIS_PORT", "6379").to_i,
pool_size: ENV.fetch("REDIS_POOL_SIZE", "50").to_i,
pool_timeout: ENV.fetch("REDIS_TIMEOUT", "0.2").to_f
)

getter queues = {} of String => BaseQueue

macro queue(name, workers, kind)
{% begin %}
queues[{{name}}] = JoobQ::Queue({{kind.id}}).new({{name}}, {{workers}})
{% end %}
end
end
end
6 changes: 5 additions & 1 deletion src/joobq/queue.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module JoobQ
class Queue(T)
abstract class BaseQueue
abstract def push(job : String)
end

class Queue(T) < BaseQueue
private TIMEOUT = 2

getter redis : Redis::PooledClient = JoobQ::REDIS
Expand Down
4 changes: 2 additions & 2 deletions src/joobq/statistics.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ module JoobQ

def self.create_series
instance.create_key "processing"
JoobQ::QUEUES.each do |key, _|
JoobQ.queues.each do |key, _|
instance.create_key key
end
end

def queues
JoobQ::QUEUES
JoobQ.queues
end

def queue(name)
Expand Down

0 comments on commit b00555a

Please sign in to comment.