Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added possible use custom sidekiq worker #56

Closed
wants to merge 4 commits into from
Closed
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
23 changes: 23 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ config.disabled = Rails.env.test?

If you have any additional questions, please contact support@getvero.com.

## Custom Sidekiq worker

You can use custom sidekiq worker:

config.worker = GetVeroWorker


class GetVeroWorker
include Sidekiq::Worker

sidekiq_options retry: 5, queue: :vero

sidekiq_retries_exhausted do |msg|
Rails.logger.error "*"*90
Rails.logger.error "Failed ----#{msg['jid']}---- #{msg['class']} with #{msg['args']}: #{msg['error_message']}"
end

def perform(api_class, domain, options)
api_class.constantize.new(domain, options).perform
Vero::App.log(self, "method: #{api_class}, options: #{options.to_json}, response: sidekiq job queued")
end
end

## Setup tracking

You will need to define who should be tracked and what information about them
Expand Down
8 changes: 7 additions & 1 deletion lib/vero/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ def run_api(api_klass, options)
return if config.disabled
validate_configured!
options.merge!(config.request_params)
Vero::Sender.send(api_klass, config.async, config.domain, options)
Vero::Sender.send(
api_klass,
config.async,
config.domain,
options,
config
)
end

protected
Expand Down
7 changes: 5 additions & 2 deletions lib/vero/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
module Vero
class Config
attr_writer :domain
attr_accessor :api_key, :secret, :development_mode, :async, :disabled, :logging
attr_accessor :api_key, :secret, :development_mode, :async, :worker,
:disabled, :logging

def self.available_attributes
[:api_key, :secret, :development_mode, :async, :disabled, :logging, :domain]
[:api_key, :secret, :development_mode, :async, :disabled, :logging,
:domain, :worker]
end

def initialize
Expand Down Expand Up @@ -56,6 +58,7 @@ def reset!
self.logging = false
self.api_key = nil
self.secret = nil
self.worker = nil
end

def update_attributes(attributes = {})
Expand Down
6 changes: 3 additions & 3 deletions lib/vero/sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def self.senders
t
end

def self.send(api_class, sender_strategy, domain, options)
def self.send(api_class, sender_strategy, domain, options, config)
sender_class = if self.senders[sender_strategy]
self.senders[sender_strategy]
else
self.senders[false]
end
(sender_class.new).call(api_class, domain, options)

(sender_class.new).call(api_class, domain, options, config)
rescue => e
options_s = JSON.dump(options)
Vero::App.log(self.new, "method: #{api_class.name}, options: #{options_s}, error: #{e.message}")
Expand Down
4 changes: 2 additions & 2 deletions lib/vero/senders/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
module Vero
module Senders
class Base
def call(api_class, domain, options)
def call(api_class, domain, options, _config)
response = api_class.perform(domain, options)
options_s = JSON.dump(options)
Vero::App.log(self, "method: #{api_class.name}, options: #{options_s}, response: job performed")
response
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/vero/senders/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Vero
module Senders
class DelayedJob
def call(api_class, domain, options)
def call(api_class, domain, options, _config)
response = ::Delayed::Job.enqueue api_class.new(domain, options)
options_s = JSON.dump(options)
Vero::App.log(self, "method: #{api_class.name}, options: #{options_s}, response: delayed job queued")
Expand All @@ -18,4 +18,4 @@ def call(api_class, domain, options)
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/vero/senders/invalid.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Vero
module Senders
class Invalid
def call(api_class, domain, options)
def call(api_class, domain, options, _config)
raise "Vero sender not supported by your version of Ruby. Please change `config.async` to a valid sender. See https://github.com/getvero/vero for more information."
end
end
end
end
end
7 changes: 4 additions & 3 deletions lib/vero/senders/resque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ def self.perform(api_class_name, domain, options)

module Senders
class Resque
def call(api_class, domain, options)
::Resque.enqueue(ResqueWorker, api_class.to_s, domain, options)
def call(api_class, domain, options, config)
worker = config.worker ? Module.const_get(config.worker) : ResqueWorker
::Resque.enqueue(worker, api_class.to_s, domain, options)
end
end
end
end
end
7 changes: 4 additions & 3 deletions lib/vero/senders/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ def perform(api_class, domain, options)

module Senders
class Sidekiq
def call(api_class, domain, options)
response = ::Vero::SidekiqWorker.perform_async(api_class.to_s, domain, options)
def call(api_class, domain, options, config)
worker = config.worker ? Module.const_get(config.worker.to_s) : ::Vero::SidekiqWorker
response = worker.send(:perform_async, api_class.to_s, domain, options)
Vero::App.log(self, "method: #{api_class.name}, options: #{options.to_json}, response: sidekiq job queued")
response
end
end
end
end
end
6 changes: 5 additions & 1 deletion lib/vero/trackable/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ def to_vero
end
end

result[:email] = result.delete(:email_address) if result.has_key?(:email_address)
if result.has_key?(:email_address)
result[:email] = result.delete(:email_address)
result['email'] = result[:email]
end

result[:_user_type] = self.class.name
result
end
Expand Down
20 changes: 13 additions & 7 deletions spec/lib/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

allow(Vero::App).to receive(:default_context).and_return(mock_context)

expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Events::TrackAPI, true, "https://api.getvero.com", expected)
expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Events::TrackAPI,
true, "https://api.getvero.com", expected, mock_context.config)

subject.track!(input)
end
Expand All @@ -39,7 +40,8 @@
let(:input) { {:email => "james@getvero.com", :data => {:age => 25}} }

specify do
expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::TrackAPI, true, "https://api.getvero.com", expected)
expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::TrackAPI,
true, "https://api.getvero.com", expected, mock_context.config)
subject.track!(input)
end
end
Expand All @@ -50,7 +52,8 @@
let(:input) { {:email => "james@getvero.com", :changes => {:age => 25}} }

specify do
expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::EditAPI, true, "https://api.getvero.com", expected)
expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::EditAPI,
true, "https://api.getvero.com", expected, mock_context.config)
subject.edit_user!(input)
end
end
Expand All @@ -61,7 +64,8 @@
let(:input) { {:add => ["boom"], :remove => ["tish"]} }

specify do
expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::EditTagsAPI, true, "https://api.getvero.com", expected)
expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::EditTagsAPI,
true, "https://api.getvero.com", expected, mock_context.config)
subject.edit_user_tags!(input)
end
end
Expand All @@ -72,7 +76,8 @@
let(:input) { {:email => "james@getvero"} }

specify do
expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::UnsubscribeAPI, true, "https://api.getvero.com", expected)
expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::UnsubscribeAPI,
true, "https://api.getvero.com", expected, mock_context.config)
subject.unsubscribe!(input)
end
end
Expand All @@ -83,9 +88,10 @@
let(:input) { {:email => "james@getvero"} }

specify do
Vero::Sender.should_receive(:send).with(Vero::Api::Workers::Users::ResubscribeAPI, true, "https://api.getvero.com", expected)
Vero::Sender.should_receive(:send).with(Vero::Api::Workers::Users::ResubscribeAPI,
true, "https://api.getvero.com", expected, mock_context.config)
subject.resubscribe!(input)
end
end
end
end
end
15 changes: 13 additions & 2 deletions spec/lib/senders/sidekiq_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
require 'spec_helper'
class VeroSidekiqWorker;end

describe Vero::Senders::Sidekiq do
subject { Vero::Senders::Sidekiq.new }
describe :call do
it "should perform_async a Vero::SidekiqWorker" do
mock_context = Vero::Context.new
Vero::SidekiqWorker.should_receive(:perform_async).with('Vero::Api::Workers::Events::TrackAPI', "abc", {:test => "abc"}).once
subject.call(Vero::Api::Workers::Events::TrackAPI, "abc", {:test => "abc"})
subject.call(Vero::Api::Workers::Events::TrackAPI, "abc", {:test => "abc"}, mock_context.config)
end
context 'use custom worker' do
it "should perform_async a VeroCustomWorker" do
mock_context = Vero::Context.new
worker = VeroSidekiqWorker
allow(mock_context.config).to receive(:worker).and_return(worker)
worker.should_receive(:perform_async).with('Vero::Api::Workers::Events::TrackAPI', "abc", {:test => "abc"}).once
subject.call(Vero::Api::Workers::Events::TrackAPI, "abc", {:test => "abc"}, mock_context.config)
end
end
end
end
Expand All @@ -22,4 +33,4 @@
subject.perform('Vero::Api::Workers::Events::TrackAPI', "abc", {:test => "abc"})
end
end
end
end
1 change: 0 additions & 1 deletion vero.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# -*- encoding: utf-8 -*-
$:.push('lib')
require "vero/version"
Expand Down