request_queue 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +77 -4
- data/lib/request_queue/queue.rb +1 -1
- data/lib/request_queue/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b645c28623badcb0436259ca4400b1dad1c85d7
|
4
|
+
data.tar.gz: 318d9ec8bc059e9e3e08fedf761f58305622ebf5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e3947b656877a088d25302444999e97562f47410b36a051452f1a9fa33a77a50d2cb7def7092ea6381c4dda6656376bab0ab8901b8785d9a3444da56847cafd
|
7
|
+
data.tar.gz: 946b80b4979423cae20fb8bf0b848511d7e6faea2e7b1320117b463ecc077d525937c8d389ef871aa9b3785e15d8178ea889b3342e81f6c6f249a4b8adae6ea4
|
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# RequestQueue
|
2
2
|
|
3
|
-
|
3
|
+
This is a utility for deduping work on a per-request basis. It works like this:
|
4
4
|
|
5
|
-
|
5
|
+
1. When the request starts, create a queue.
|
6
|
+
2. Add some work to the queue by calling `RequestQueue.enqueue(some_job)`.
|
7
|
+
3. Before the request ends, you'll have opportunity to dedupe the work.
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
@@ -22,7 +24,79 @@ Or install it yourself as:
|
|
22
24
|
|
23
25
|
## Usage
|
24
26
|
|
25
|
-
|
27
|
+
A a job can be any callable, so for example:
|
28
|
+
|
29
|
+
```
|
30
|
+
job = proc { puts 'Hello' }
|
31
|
+
|
32
|
+
RequestQueue.enqueue(job)
|
33
|
+
RequestQueue.enqueue(job)
|
34
|
+
```
|
35
|
+
|
36
|
+
This would output 'Hello' once at the end of the request.
|
37
|
+
|
38
|
+
#### Using objects
|
39
|
+
|
40
|
+
Because `RequestQueue` accepts any callable, you can also use an object.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
class Job
|
44
|
+
def initialize(message)
|
45
|
+
@message = message
|
46
|
+
end
|
47
|
+
|
48
|
+
def call
|
49
|
+
puts @message
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
job = Job.new('Hello')
|
54
|
+
RequestQueue.enqueue(job)
|
55
|
+
RequestQueue.enqueue(job)
|
56
|
+
```
|
57
|
+
|
58
|
+
#### Advanced Deduping
|
59
|
+
|
60
|
+
Sometimes, you need a little more control over the deduping.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class Job
|
64
|
+
def self.filter(jobs)
|
65
|
+
jobs.uniq_by(&:message)
|
66
|
+
end
|
67
|
+
|
68
|
+
attr_reader :message
|
69
|
+
|
70
|
+
def initialize(message)
|
71
|
+
@message = message
|
72
|
+
end
|
73
|
+
|
74
|
+
def call
|
75
|
+
puts message
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
RequestQueue.enqueue Job.new('Hello')
|
80
|
+
RequestQueue.enqueue Job.new('Hello')
|
81
|
+
```
|
82
|
+
|
83
|
+
In this example, the job instances are unique, so they wouldn't be automatically deduped. But, when the class of a message responds to `filter`, it will be called and give you the opportunity to provide custom dedupe logic.
|
84
|
+
|
85
|
+
## Testing
|
86
|
+
|
87
|
+
When testing, sometimes you want a little more control. As such, `RequestQueue` offers multiple queue backends:
|
88
|
+
|
89
|
+
* `:default` - Runs jobs at the end of the request.
|
90
|
+
* `:fake` - Never runs jobs, and allows you to inspect the queue with `RequestQueue.queue.queue`.
|
91
|
+
* `:inline` - Runs jobs immediately and never adds them to the queue.
|
92
|
+
|
93
|
+
To change the queueing backend, you can just say `RequestQueue.use`:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
RequestQueue.use :inline
|
97
|
+
```
|
98
|
+
|
99
|
+
**NOTE:** Only use the `use` method in the testing environment. It is not thread-safe.
|
26
100
|
|
27
101
|
## Development
|
28
102
|
|
@@ -38,4 +112,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/Ray Za
|
|
38
112
|
## License
|
39
113
|
|
40
114
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
-
|
data/lib/request_queue/queue.rb
CHANGED