Move BaseCommand to cartodb-common

pull/16057/head
Rafa de la Torre 4 years ago
parent c169bba6f8
commit d4d2a83375

@ -55,7 +55,7 @@ end
gem 'cartodb-common',
git: 'https://github.com/cartodb/cartodb-common.git',
branch: 'feature/ch127672/funnel-messages-shared-token'
branch: 'feature/ch127672/funnel-messages-shared-token-merge-base-command'
gem 'roo', '1.13.2'
gem 'state_machines-activerecord', '~> 0.5.0'
gem 'typhoeus', '1.3.1'

@ -8,8 +8,8 @@ GIT
GIT
remote: https://github.com/cartodb/cartodb-common.git
revision: 25bdb3553367bee6885d13d1097eb5747892dc22
branch: feature/ch127672/funnel-messages-shared-token
revision: 59a0855b9c990d8baa6ab76fcf0c8e0a4cb093fe
branch: feature/ch127672/funnel-messages-shared-token-merge-base-command
specs:
cartodb-common (0.5.3)
argon2 (~> 2)

@ -1,53 +0,0 @@
class BaseCommand
attr_accessor(
:logger,
:notifications_topic,
:params,
:request_id
)
def self.run(params = {})
new(params).run
end
def initialize(params = {}, extra_context = {})
self.params = params.with_indifferent_access
self.request_id = Carto::Common::CurrentRequest.request_id || extra_context[:request_id]
self.notifications_topic = extra_context[:notifications_topic]
self.logger = extra_context[:logger] || Rails.logger
end
def run
Carto::Common::CurrentRequest.with_request_id(request_id) do
begin
before_run_hooks
run_command
rescue StandardError => e
logger.error(log_context.merge(message: 'Command failed', exception: e))
raise e
ensure
after_run_hooks
end
end
end
private
def run_command
raise 'This method must be overriden'
end
def before_run_hooks
logger.info(log_context.merge(message: 'Started command'))
end
def after_run_hooks
logger.info(log_context.merge(message: 'Finished command'))
end
def log_context
{ command_class: self.class.name, request_id: request_id }
end
end

@ -0,0 +1,10 @@
class CartoCommand < Carto::Common::BaseCommand
attr_accessor :notifications_topic
def initialize(params = {}, extra_context = {})
self.notifications_topic = extra_context[:notifications_topic]
super
end
end

@ -1,5 +1,5 @@
module OrganizationCommands
class Create < ::BaseCommand
class Create < ::CartoCommand
private

@ -1,5 +1,5 @@
module OrganizationCommands
class Delete < ::BaseCommand
class Delete < ::CartoCommand
private

@ -1,5 +1,5 @@
module OrganizationCommands
class Update < ::BaseCommand
class Update < ::CartoCommand
private

@ -19,7 +19,7 @@ describe OrganizationCommands::Create do
before do
notifications_topic.expects(:publish)
BaseCommand.any_instance.expects(:notifications_topic).returns(notifications_topic)
CartoCommand.any_instance.expects(:notifications_topic).returns(notifications_topic)
end
it 'creates the organization' do

@ -9,7 +9,7 @@ describe OrganizationCommands::Delete do
describe '#run' do
context 'when everything is ok' do
before do
BaseCommand.any_instance.expects(:notifications_topic).returns(notifications_topic)
CartoCommand.any_instance.expects(:notifications_topic).returns(notifications_topic)
end
it 'deletes the organization and publishes an organization_deleted event' do
@ -23,7 +23,7 @@ describe OrganizationCommands::Delete do
context 'when the organization does not exist' do
before do
BaseCommand.any_instance.expects(:notifications_topic).returns(notifications_topic)
CartoCommand.any_instance.expects(:notifications_topic).returns(notifications_topic)
end
let(:organiation_id) { 'fake-id' }

Loading…
Cancel
Save