README.md in upperkut-0.7.2 vs README.md in upperkut-0.7.4
- old
+ new
@@ -2,10 +2,12 @@
[![CircleCI](https://circleci.com/gh/ResultadosDigitais/upperkut/tree/master.svg?style=svg&circle-token=693e512de6985be3b3db12279ba6ed508fb5c6f6)](https://circleci.com/gh/ResultadosDigitais/upperkut/tree/master)
[![Maintainability](https://api.codeclimate.com/v1/badges/ece40319b0db03af891d/maintainability)](https://codeclimate.com/repos/5b318a7c6d37b70272008676/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/ece40319b0db03af891d/test_coverage)](https://codeclimate.com/repos/5b318a7c6d37b70272008676/test_coverage)
+[[Docs]](https://www.rubydoc.info/gems/upperkut/0.7.2/Upperkut)
+
Background processing framework for Ruby applications.
## Installation
Add this line to your application's Gemfile:
@@ -29,43 +31,24 @@
1) Create a Worker class and the define how to process the batch;
```ruby
class MyWorker
include Upperkut::Worker
- # This is optional
-
- setup_upperkut do |config|
- # Define which redis instance you want to use
- config.strategy = Upperkut::Strategies::BufferedQueue.new(
- self,
- redis: { url: ENV['ANOTHER_REDIS_INSTANCE_URL'] },
- batch_size: 400, # How many events should be dispatched to worker.
- max_wait: 300 # How long Processor wait in seconds to process batch.
- # even though the amount of items did not reached the
- # the batch_size.
- )
-
- # How frequent the Processor should hit redis looking for elegible
- # batch. The default value is 5 seconds. You can also set the env
- # UPPERKUT_POLLING_INTERVAL.
- config.polling_interval = 4
- end
-
def perform(batch_items)
heavy_processing(batch_items)
process_metrics(batch_items)
end
end
```
-2) Start pushings items;
+2) Start pushing items;
```ruby
Myworker.push_items(
[
{
- 'id' => SecureRandom.uuid,
- 'name' => 'Robert C Hall',
+ 'id' => SecureRandom.uuid,
+ 'name' => 'Robert C Hall',
'action' => 'EMAIL_OPENNED'
}
]
)
```
@@ -92,23 +75,69 @@
process_metrics(batch_items)
end
end
```
-2) Start pushings items with `timestamp` param;
+2) Start pushing items with `timestamp` parameter;
```ruby
# timestamp is 'Thu, 10 May 2019 23:43:58 GMT'
Myworker.push_items(
[
{
- 'timestamp' => '1557531838',
- 'id' => SecureRandom.uuid,
- 'name' => 'Robert C Hall',
+ 'timestamp' => '1557531838',
+ 'id' => SecureRandom.uuid,
+ 'name' => 'Robert C Hall',
'action' => 'SEND_NOTIFICATION'
}
]
)
```
+
+3) Start Upperkut;
+ ```bash
+ $ bundle exec upperkut --worker MyWorker --concurrency 10
+ ```
+
+### Example 3 - Priority Queue:
+
+Note: priority queues requires redis 5.0.0+ as it uses ZPOP* commands.
+
+1) Create a Worker class and the define how to process the batch;
+ ```ruby
+ require 'upperkut/strategies/priority_queue'
+
+ class MyWorker
+ include Upperkut::Worker
+
+ setup_upperkut do |config|
+ config.strategy = Upperkut::Strategies::PriorityQueue.new(
+ self,
+ priority_key: -> { |item| item['tenant_id'] }
+ )
+ end
+
+ def perform(items)
+ items.each do |item|
+ puts "event dispatched: #{item.inspect}"
+ end
+ end
+ end
+ ```
+
+2) So you can enqueue items from different tenants;
+ ```ruby
+ MyWorker.push_items(
+ [
+ { 'tenant_id' => 1, 'id' => 1 },
+ { 'tenant_id' => 1, 'id' => 2 },
+ { 'tenant_id' => 1, 'id' => 3 },
+ { 'tenant_id' => 2, 'id' => 4 },
+ { 'tenant_id' => 3, 'id' => 5 },
+ ]
+ )
+ ```
+
+ The code above will enqueue items as follows `1, 4, 5, 2, 3`
3) Start Upperkut;
```bash
$ bundle exec upperkut --worker MyWorker --concurrency 10
```