From 278b0ff5b14bc9dd34c0f0d29bf3fc80db458901 Mon Sep 17 00:00:00 2001 From: Maxim Pechnikov Date: Thu, 22 Oct 2015 12:11:46 +0300 Subject: [PATCH 1/3] added possible use custom sidekiq worker --- README.markdown | 23 +++++++++++++++++++++++ lib/vero/api.rb | 8 +++++++- lib/vero/config.rb | 7 +++++-- lib/vero/sender.rb | 6 +++--- lib/vero/senders/base.rb | 4 ++-- lib/vero/senders/delayed_job.rb | 4 ++-- lib/vero/senders/invalid.rb | 4 ++-- lib/vero/senders/resque.rb | 7 ++++--- lib/vero/senders/sidekiq.rb | 7 ++++--- spec/lib/api_spec.rb | 20 +++++++++++++------- spec/lib/senders/sidekiq_spec.rb | 15 +++++++++++++-- 11 files changed, 78 insertions(+), 27 deletions(-) diff --git a/README.markdown b/README.markdown index ae226d7..c82c523 100644 --- a/README.markdown +++ b/README.markdown @@ -55,6 +55,29 @@ add the following line to your initializer: 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 diff --git a/lib/vero/api.rb b/lib/vero/api.rb index 1102c46..c799f53 100644 --- a/lib/vero/api.rb +++ b/lib/vero/api.rb @@ -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 diff --git a/lib/vero/config.rb b/lib/vero/config.rb index c21b426..176091e 100644 --- a/lib/vero/config.rb +++ b/lib/vero/config.rb @@ -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 @@ -56,6 +58,7 @@ def reset! self.logging = false self.api_key = nil self.secret = nil + self.worker = nil end def update_attributes(attributes = {}) diff --git a/lib/vero/sender.rb b/lib/vero/sender.rb index 7daebd0..b51976e 100644 --- a/lib/vero/sender.rb +++ b/lib/vero/sender.rb @@ -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}") diff --git a/lib/vero/senders/base.rb b/lib/vero/senders/base.rb index 8ab731e..6f472da 100644 --- a/lib/vero/senders/base.rb +++ b/lib/vero/senders/base.rb @@ -3,7 +3,7 @@ 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") @@ -11,4 +11,4 @@ def call(api_class, domain, options) end end end -end \ No newline at end of file +end diff --git a/lib/vero/senders/delayed_job.rb b/lib/vero/senders/delayed_job.rb index c75fe5c..84f023c 100644 --- a/lib/vero/senders/delayed_job.rb +++ b/lib/vero/senders/delayed_job.rb @@ -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") @@ -18,4 +18,4 @@ def call(api_class, domain, options) end end end -end \ No newline at end of file +end diff --git a/lib/vero/senders/invalid.rb b/lib/vero/senders/invalid.rb index f338c56..e0fe19c 100644 --- a/lib/vero/senders/invalid.rb +++ b/lib/vero/senders/invalid.rb @@ -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 \ No newline at end of file +end diff --git a/lib/vero/senders/resque.rb b/lib/vero/senders/resque.rb index ead61f8..9b384cf 100644 --- a/lib/vero/senders/resque.rb +++ b/lib/vero/senders/resque.rb @@ -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 \ No newline at end of file +end diff --git a/lib/vero/senders/sidekiq.rb b/lib/vero/senders/sidekiq.rb index 919e4af..fe6e145 100644 --- a/lib/vero/senders/sidekiq.rb +++ b/lib/vero/senders/sidekiq.rb @@ -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 \ No newline at end of file +end diff --git a/spec/lib/api_spec.rb b/spec/lib/api_spec.rb index eb412a9..db1b8a8 100644 --- a/spec/lib/api_spec.rb +++ b/spec/lib/api_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 \ No newline at end of file +end diff --git a/spec/lib/senders/sidekiq_spec.rb b/spec/lib/senders/sidekiq_spec.rb index 1f66f55..009d163 100644 --- a/spec/lib/senders/sidekiq_spec.rb +++ b/spec/lib/senders/sidekiq_spec.rb @@ -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 @@ -22,4 +33,4 @@ subject.perform('Vero::Api::Workers::Events::TrackAPI', "abc", {:test => "abc"}) end end -end \ No newline at end of file +end From 51749c99050cab297c9f8aca13ac0b5afc1cc830 Mon Sep 17 00:00:00 2001 From: Maxim Pechnikov Date: Fri, 23 Oct 2015 11:22:03 +0300 Subject: [PATCH 2/3] update sucker_punch --- vero.gemspec | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vero.gemspec b/vero.gemspec index a41c23a..efa4b52 100644 --- a/vero.gemspec +++ b/vero.gemspec @@ -1,4 +1,3 @@ - # -*- encoding: utf-8 -*- $:.push('lib') require "vero/version" @@ -21,7 +20,7 @@ Gem::Specification.new do |s| [:development, 'sidekiq', "~> 2.0"], [:runtime, 'json'], [:runtime, 'rest-client'], - [:runtime, 'sucker_punch', '1.5.1'] + [:runtime, 'sucker_punch', '~> 1.6.0'] ] s.files = Dir['**/*'] From cabc0cf536b33bfabbd13453dacae9ef51bd3409 Mon Sep 17 00:00:00 2001 From: Dmitry Morozov Date: Thu, 5 May 2016 17:57:42 +0300 Subject: [PATCH 3/3] fix email_address handling --- lib/vero/trackable/base.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/vero/trackable/base.rb b/lib/vero/trackable/base.rb index 4f5e00b..f96fe9d 100644 --- a/lib/vero/trackable/base.rb +++ b/lib/vero/trackable/base.rb @@ -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