Skip to content

Instantly share code, notes, and snippets.

@adimircolen
Created August 17, 2017 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adimircolen/fb2c7fe1d7fd5ec2f7682be7a6f24ba2 to your computer and use it in GitHub Desktop.
Save adimircolen/fb2c7fe1d7fd5ec2f7682be7a6f24ba2 to your computer and use it in GitHub Desktop.
Shoryuken
class CreateEndpointJob < ActiveJob::Base
queue_as :default
def perform(data)
logger.debug { "CreateEndpointJob with params data: #{data}" }
resource_class = data[:resource].constantize
resource = resource_class.find(data[:id])
if !resource.should_create_endpoint?( data[:device_id] )
arn = CloudMessage.plataform_application( resource )
sns = Aws::SNS::Client.new
duplicate_cm = CloudMessage.where(device_id: data[:device_id]).where.not("#{data[:resource].underscore}_id" => nil)
remove_sns_endpoint(duplicate_cm)
duplicate_cm.destroy_all
endpoint = sns.create_platform_endpoint( {platform_application_arn: arn, token: data[:gcm]} )
resource.cloud_messages.create(
gcm: data[:gcm],
endpoint: endpoint[:endpoint_arn],
device_id: data[:device_id]
)
logger.debug { "Response to create_platform_endpoint #{endpoint.inspect}" }
end
end
def remove_sns_endpoint(collection_cm)
sns = Aws::SNS::Client.new
end
end
module Api
module V1
module Chef
class SessionsController < ApiController
skip_before_action :auth_with_token!, only: [:create]
def create
chef = login_chef
if chef
render json: chef, include: [:place], status: :ok
else
render_error(I18n.t('authentication.error', authentication_keys: 'email'), :unprocessable_entity)
end
end
def destroy
current_user.regenerate_auth_token
head :no_content
end
private
def login_chef
chef = ::Chef.joins(:user).where(users: {email: params[:email]}).limit(1).first
user = chef.user if chef
if chef && user && user.authenticate(params[:password])
CreateEndpointJob.perform_later({ gcm: params[:gcm], resource: chef.class.to_s, id: chef.id, device_id: params[:device_id] })
chef if user.auth_token.present?
end
end
end
end
end
end
Shoryuken.configure_client do |config|
config.aws = {
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
access_key_id: ENV["AWS_ACCESS_KEY_ID"],
region: ENV["AWS_REGION"],
receive_message: { # See http://docs.aws.amazon.com/sdkforruby/api/Aws/SQS/Client.html#receive_message-instance_method
wait_time_seconds: 20, # The number of seconds to wait for new messages when polling. Defaults to the #wait_time_seconds defined on the queue
attribute_names: [
"ApproximateReceiveCount",
"SentTimestamp"
]
}
}
end
Shoryuken.configure_server do |config|
config.aws = {
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
access_key_id: ENV["AWS_ACCESS_KEY_ID"],
region: ENV["AWS_REGION"]
}
Rails.logger = Shoryuken::Logging.logger
Rails.logger.level = Rails.application.config.log_level
end
Shoryuken.active_job_queue_name_prefixing = true
:concurrency: 3
:pidfile: tmp/pids/shoryuken.pid
:delay: 3
:queues:
- default
- [push_queue, 2]
- [paypal, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment