Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions valued-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ Valued::Connection.executor = Concurrent::ImmediateExecutor.new

You can also reuse an existing executor to avoid a dedicated background thread for Valued.

### Custom executors

You do not need to use any of the executors provided by concurrent-ruby. The only thing the `executor` needs to implement is a method called `post` that will schedule the passed block to be executed some time soon.

Here is an example for using EventMachine's `defer` method:
Expand All @@ -227,6 +229,14 @@ end
Valued::Connection.executor = MyExecutor.new
```

### Sidekiq

You can also used Sidekiq to send events to Valued. This is especially handy if you already have Sidekiq set up in your application.

``` ruby
Valued::Connection.executor = Sidekiq
```

## Known issues

This gem is incompatible with the [valued](https://rubygems.org/gems/valued) gem.
3 changes: 3 additions & 0 deletions valued-client/lib/valued.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def self.disconnect = @client = @scope = nil
# @return [true, false] whether a shared connection has been created
def self.connected? = !!@client

# @return [Valued::Connection, #call, nil] the shared connection
def self.connection = @client&.connection

# @return [Valued::Client] the shared client
# @see .connect
def self.client
Expand Down
16 changes: 15 additions & 1 deletion valued-client/lib/valued/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ class Valued::Connection
# @param data [Hash] The data to send.
# @return [void]
# @see #executor
def self.call(connection, data) = executor.post { connection.call(data) }
def self.call(connection, data)
if executor.respond_to?(:perform_async)
if connection != Valued.connection
token = connection.token if connection.respond_to?(:token)
endpoint = connection.endpoint if connection.respond_to?(:endpoint)
end
return executor.perform_async(data, token, endpoint)
else
executor.post { connection.call(data) }
end
end

# @return [Concurrent::Executor, #pool] executor used to send requests in the background
# @see https://ruby-concurrency.github.io/concurrent-ruby/master/file.thread_pools.html
Expand All @@ -49,6 +59,10 @@ def self.executor
# @see https://ruby-concurrency.github.io/concurrent-ruby/master/file.thread_pools.html
# @return [void]
def self.executor=(executor)
if defined? ::Sidekiq and executor == ::Sidekiq
require "valued/sidekiq"
executor = Valued::Sidekiq
end
@executor = executor
end

Expand Down
17 changes: 17 additions & 0 deletions valued-client/lib/valued/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "valued"
require "sidekiq"

module Valued::Sidekiq
include Sidekiq::Job

def perform(data, token = nil, endpoint = nil)
if token
endpoint ||= Valued::Connection::DEFAULT_ENDPOINT
Valued::Connection.new(token, endpoint).call(data)
else
Valued.client.connection.call(data)
end
end
end
5 changes: 5 additions & 0 deletions valued-client/test/test_sidekiq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require_relative 'setup'

class TestSideqik < Minitest::Test
# TODO: test that the job is enqueued
end