Skip to content

Commit b00555a

Browse files
committed
Better Configuration
1 parent c37fbbc commit b00555a

File tree

7 files changed

+43
-23
lines changed

7 files changed

+43
-23
lines changed

README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,15 @@ Defining Queues: Queues are of type `Hash(String, Queue(T))` where the name of t
6767

6868
- **Name:** `queue:email`
6969
- **Number Workers:** 10
70+
- **Job Class:** TestJob - a class or union of classes
7071

7172
### Example
7273

7374
```crystal
74-
module JoobQ
75-
QUEUES = {
76-
"queue:Email" => JoobQ::Queue(EmailJob).new("queue:Email", 1),
77-
"queue:Fail" => JoobQ::Queue(FailJob).new("queue:Fail", 1),
78-
"queue:Test" => JoobQ::Queue(TestJob).new("queue:Test", 1),
79-
}
75+
JoobQ.configure do
76+
queue "queue:Email", 1, EmailJob
77+
queue "queue:Fail", 1, FailJob
78+
queue "queue:Test", 1, TestJob
8079
end
8180
```
8281

shard.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: joobq
2-
version: 0.2.4
2+
version: 0.2.5
33

44
authors:
55
- Elias J. Perez <[email protected]>

spec/spec_helper.cr

+3-5
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ struct Job1
4242
end
4343
end
4444

45-
module JoobQ
46-
QUEUES = {
47-
"single" => Queue(Job1).new("single", 10),
48-
"example" => Queue(ExampleJob | FailJob).new("example", 1),
49-
}
45+
JoobQ.configure do
46+
queue "single", 10, Job1
47+
queue "example", 10, ExampleJob | FailJob
5048
end

src/joobq.cr

+7-8
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@ require "cron_parser"
77
require "./joobq/**"
88

99
module JoobQ
10-
REDIS = Redis::PooledClient.new(
11-
host: ENV.fetch("REDIS_HOST", "localhost"),
12-
port: ENV.fetch("REDIS_PORT", "6379").to_i,
13-
pool_size: ENV.fetch("REDIS_POOL_SIZE", "50").to_i,
14-
pool_timeout: ENV.fetch("REDIS_TIMEOUT", "0.2").to_f
15-
)
10+
REDIS = Configure::INSTANCE.redis
1611

1712
Log.setup_from_env(default_level: :trace)
1813

14+
def self.configure
15+
with Configure::INSTANCE yield
16+
end
17+
1918
def self.queues
20-
QUEUES
19+
Configure::INSTANCE.queues
2120
end
2221

2322
def self.statistics
@@ -37,7 +36,7 @@ module JoobQ
3736
end
3837

3938
def self.[](name : String)
40-
QUEUES[name]
39+
queues[name]
4140
end
4241

4342
def self.reset

src/joobq/configure.cr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module JoobQ
2+
class Configure
3+
INSTANCE = new
4+
5+
getter redis : Redis::PooledClient = Redis::PooledClient.new(
6+
host: ENV.fetch("REDIS_HOST", "localhost"),
7+
port: ENV.fetch("REDIS_PORT", "6379").to_i,
8+
pool_size: ENV.fetch("REDIS_POOL_SIZE", "50").to_i,
9+
pool_timeout: ENV.fetch("REDIS_TIMEOUT", "0.2").to_f
10+
)
11+
12+
getter queues = {} of String => BaseQueue
13+
14+
macro queue(name, workers, kind)
15+
{% begin %}
16+
queues[{{name}}] = JoobQ::Queue({{kind.id}}).new({{name}}, {{workers}})
17+
{% end %}
18+
end
19+
end
20+
end

src/joobq/queue.cr

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
module JoobQ
2-
class Queue(T)
2+
abstract class BaseQueue
3+
abstract def push(job : String)
4+
end
5+
6+
class Queue(T) < BaseQueue
37
private TIMEOUT = 2
48

59
getter redis : Redis::PooledClient = JoobQ::REDIS

src/joobq/statistics.cr

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ module JoobQ
1212

1313
def self.create_series
1414
instance.create_key "processing"
15-
JoobQ::QUEUES.each do |key, _|
15+
JoobQ.queues.each do |key, _|
1616
instance.create_key key
1717
end
1818
end
1919

2020
def queues
21-
JoobQ::QUEUES
21+
JoobQ.queues
2222
end
2323

2424
def queue(name)

0 commit comments

Comments
 (0)