diff --git a/valued-client/README.md b/valued-client/README.md index db1b45e..f804e5f 100644 --- a/valued-client/README.md +++ b/valued-client/README.md @@ -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: @@ -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. diff --git a/valued-client/lib/valued.rb b/valued-client/lib/valued.rb index 671b741..8557d21 100644 --- a/valued-client/lib/valued.rb +++ b/valued-client/lib/valued.rb @@ -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 diff --git a/valued-client/lib/valued/connection.rb b/valued-client/lib/valued/connection.rb index 8a818c3..e5e3441 100644 --- a/valued-client/lib/valued/connection.rb +++ b/valued-client/lib/valued/connection.rb @@ -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 @@ -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 diff --git a/valued-client/lib/valued/sidekiq.rb b/valued-client/lib/valued/sidekiq.rb new file mode 100644 index 0000000..f587904 --- /dev/null +++ b/valued-client/lib/valued/sidekiq.rb @@ -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 \ No newline at end of file diff --git a/valued-client/test/test_sidekiq.rb b/valued-client/test/test_sidekiq.rb new file mode 100644 index 0000000..d43dcd0 --- /dev/null +++ b/valued-client/test/test_sidekiq.rb @@ -0,0 +1,5 @@ +require_relative 'setup' + +class TestSideqik < Minitest::Test + # TODO: test that the job is enqueued +end \ No newline at end of file