Created
April 30, 2021 21:10
-
-
Save fmundaca/bac990d0a831debbfdc4e68d96ba9c08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class EnterpriseModulesWorker < BaseWorker | |
shoryuken_options queue: queue('enterprise-modules'), | |
body_parser: :json, | |
auto_delete: false, | |
retry_intervals: [60, 120, 240] | |
attr_accessor :sqs_msg, :tenant, :event_message | |
EVENT_CREATE = 'create' | |
EVENT_UPDATE = 'update' | |
EVENT_DESTROY = 'destroy' | |
def perform(sqs_msg, message) | |
Shoryuken.logger.info("Processing message with data: '#{message.to_json}'".red) | |
@sqs_msg = sqs_msg | |
@event_data = message['message'] | |
Shoryuken.logger.info("Processing message with message: '#{@event_data.to_json}'".red) | |
unless @event_data['module_code'].eql?(Tenant::ALLOWED_MODULE_CODE) | |
# @sqs_msg.delete | |
return | |
end | |
find_or_create_tenant | |
case message['event'] | |
when EVENT_CREATE | |
create(message['message']) | |
when EVENT_DESTROY | |
destroy(message['message']) | |
else | |
Shoryuken.logger.info("Unknown event type '#{message['event']}'") | |
end | |
end | |
def find_or_create_tenant | |
@tenant = find_tenant(@event_data['enterprise_token']) | |
if @tenant.nil? | |
data = { name: @event_data['enterprise']['name'], | |
token: @event_data['enterprise']['token'], | |
scheme: @event_data['enterprise']['scheme'] } | |
Shoryuken.logger.info("Creating tenant with data: #{data.to_json}") | |
Tenant.create(data) | |
end | |
end | |
def create(data) | |
@tenant.update_attribute(:active, true) | |
@sqs_msg.delete | |
end | |
def destroy(data) | |
@tenant.update_attribute(:active, false) | |
@sqs_msg.delete | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment