Last active
July 28, 2018 13:48
-
-
Save manoelneto/32b9dfe6aaad1d46ba3779078d5f8f4d 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
# controller do rails | |
def webhooks_controller data | |
# pusha pra uma fila do redis | |
redis.lpush("webhooks", data.to_json) | |
end | |
# em outro processos | |
def redis_batch_push | |
loop do | |
messages = take_messages_from_redis | |
WebhookSidekiq.perform_async messages.to_json | |
end | |
end | |
class WebhookSidekiq | |
def perform messages_json | |
messages = JSON.parse(messages_json) | |
result_reduce = messages.inject({ | |
"mensagem_privada" => {}, | |
}) do |memo, message| | |
page_id = message["page_id"] | |
if message["type"] == "mensagem_privada" | |
# salva em um hash (mais performatico) | |
# as páginas que tiveram nova mensagem | |
memo["mensagem_privada"][page_id] = true | |
else | |
# vem notitifações e vários tipos | |
end | |
memo | |
end | |
page_ids_with_mensagem_privada = result_reduce["mensagem_privada"].keys | |
# atualizo tudo de uma vez | |
Page.where(page_id: page_ids_with_mensagem_privada).update_all(has_new_private_messages: true) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment