Skip to content

Commit d63a719

Browse files
committed
Support supervision tree
1 parent b108123 commit d63a719

File tree

5 files changed

+68
-19
lines changed

5 files changed

+68
-19
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master
44

5+
## v3.3.0 [2021-10-12]
6+
7+
- [Added] The ability to start as part of a supervision tree
8+
59
## v3.2.0 [2021-10-11]
610

711
- [Improved] Updated to the new `ConsumerSupervisor` syntax

README.md

+27-17
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@ end
2828

2929
## Usage
3030

31-
A simple example:
31+
### A simple example:
3232

3333
```elixir
34-
{:ok, opq} = OPQ.init
34+
{:ok, opq} = OPQ.init()
3535

3636
OPQ.enqueue(opq, fn -> IO.inspect("hello") end)
3737
OPQ.enqueue(opq, fn -> IO.inspect("world") end)
3838
```
3939

40-
Specify module, function and arguments:
40+
### Specify module, function and arguments:
4141

4242
```elixir
43-
{:ok, opq} = OPQ.init
43+
{:ok, opq} = OPQ.init()
4444

4545
OPQ.enqueue(opq, IO, :inspect, ["hello"])
4646
OPQ.enqueue(opq, IO, :inspect, ["world"])
4747
```
4848

49-
Specify a custom name for the queue:
49+
### Specify a custom name for the queue:
5050

5151
```elixir
5252
OPQ.init(name: :items)
@@ -55,14 +55,24 @@ OPQ.enqueue(:items, fn -> IO.inspect("hello") end)
5555
OPQ.enqueue(:items, fn -> IO.inspect("world") end)
5656
```
5757

58-
Specify a custom worker to process items in the queue:
58+
### Start as part of a supervision tree:
59+
60+
Note, when starting as part of a supervision tree, the `:name` option must be provided.
61+
62+
```elixir
63+
children = [
64+
{OPQ, name: :items}
65+
]
66+
```
67+
68+
### Specify a custom worker to process items in the queue:
5969

6070
```elixir
6171
defmodule CustomWorker do
6272
def start_link(item) do
63-
Task.start_link fn ->
73+
Task.start_link(fn ->
6474
Agent.update(:bucket, &[item | &1])
65-
end
75+
end)
6676
end
6777
end
6878

@@ -76,23 +86,23 @@ OPQ.enqueue(opq, "world")
7686
Agent.get(:bucket, & &1) # => ["world", "hello"]
7787
```
7888

79-
Rate limit:
89+
### Rate limit:
8090

8191
```elixir
8292
{:ok, opq} = OPQ.init(workers: 1, interval: 1000)
8393

84-
Task.async fn ->
94+
Task.async(fn ->
8595
OPQ.enqueue(opq, fn -> IO.inspect("hello") end)
8696
OPQ.enqueue(opq, fn -> IO.inspect("world") end)
87-
end
97+
end)
8898
```
8999

90100
If no interval is supplied, the ratelimiter will be bypassed.
91101

92-
Check the queue and number of available workers:
102+
### Check the queue and number of available workers:
93103

94104
```elixir
95-
{:ok, opq} = OPQ.init
105+
{:ok, opq} = OPQ.init()
96106

97107
OPQ.enqueue(opq, fn -> Process.sleep(1000) end)
98108

@@ -103,20 +113,20 @@ Process.sleep(1200)
103113
{queue, available_workers} = OPQ.info(opq) # => {:normal, {[], []}, 10}
104114
```
105115

106-
Stop the queue:
116+
### Stop the queue:
107117

108118
```elixir
109-
{:ok, opq} = OPQ.init
119+
{:ok, opq} = OPQ.init()
110120

111121
OPQ.enqueue(opq, fn -> IO.inspect("hello") end)
112122
OPQ.stop(opq)
113123
OPQ.enqueue(opq, fn -> IO.inspect("world") end) # => (EXIT) no process...
114124
```
115125

116-
Pause and resume the queue:
126+
### Pause and resume the queue:
117127

118128
```elixir
119-
{:ok, opq} = OPQ.init
129+
{:ok, opq} = OPQ.init()
120130

121131
OPQ.enqueue(opq, fn -> IO.inspect("hello") end) # => "hello"
122132
OPQ.pause(opq)

lib/opq.ex

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ defmodule OPQ do
66
alias OPQ.{Options, Feeder, RateLimiter, WorkerSupervisor}
77
alias OPQ.OptionsHandler, as: Opt
88

9+
def child_spec(opts \\ []) do
10+
%{
11+
id: opts[:name],
12+
start: {OPQ, :start_link, [opts]}
13+
}
14+
end
15+
16+
def start_link(opts \\ []), do: init(opts)
17+
918
def init(opts \\ []) do
1019
opts
1120
|> Options.assign_defaults()

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule OPQ.Mixfile do
44
def project do
55
[
66
app: :opq,
7-
version: "3.2.0",
7+
version: "3.3.0",
88
elixir: "~> 1.5",
99
elixirc_paths: elixirc_paths(Mix.env()),
1010
package: package(),

test/lib/opq_test.exs

+27-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,33 @@ defmodule OPQTest do
33

44
doctest OPQ
55

6-
test "enqueue items" do
6+
test "enqueue items - child_spec/1" do
7+
Supervisor.start_link([{OPQ, name: :opq}], strategy: :one_for_one)
8+
9+
OPQ.enqueue(:opq, :a)
10+
OPQ.enqueue(:opq, :b)
11+
12+
wait(fn ->
13+
{_status, queue, _demand} = OPQ.info(:opq)
14+
15+
assert :queue.len(queue) == 0
16+
end)
17+
end
18+
19+
test "enqueue items - start_link/1" do
20+
{:ok, opq} = OPQ.start_link()
21+
22+
OPQ.enqueue(opq, :a)
23+
OPQ.enqueue(opq, :b)
24+
25+
wait(fn ->
26+
{_status, queue, _demand} = OPQ.info(opq)
27+
28+
assert :queue.len(queue) == 0
29+
end)
30+
end
31+
32+
test "enqueue items - init/1" do
733
{:ok, opq} = OPQ.init()
834

935
OPQ.enqueue(opq, :a)

0 commit comments

Comments
 (0)