Skip to content

Instantly share code, notes, and snippets.

@fmundaca
Created April 30, 2021 21:10
Show Gist options
  • Save fmundaca/bac990d0a831debbfdc4e68d96ba9c08 to your computer and use it in GitHub Desktop.
Save fmundaca/bac990d0a831debbfdc4e68d96ba9c08 to your computer and use it in GitHub Desktop.
# 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