Create a queue to process matches

This commit is contained in:
Álvaro Girona Arias
2024-05-02 15:49:12 +02:00
parent ff0c05a7ed
commit 58a1aacd5a
7 changed files with 84 additions and 2 deletions
View File
@@ -0,0 +1,41 @@
defmodule Scrapper.MatchBroadway do
use Broadway
alias Broadway.Message
def start_link(_opts) do
Broadway.start_link(
__MODULE__,
name: __MODULE__,
producer: [
module:
{BroadwayRabbitMQ.Producer,
queue: "match",
connection: [
username: "guest",
password: "guest",
host: "localhost"
],
on_failure: :reject,
qos: [
prefetch_count: 3
]},
concurrency: 1
],
processors: [
default: [
concurrency: 20
]
]
)
end
@impl true
def handle_message(_, message = %Broadway.Message{}, _) do
match_id = message.data
IO.inspect(match_id)
match = Scrapper.Data.Api.MatchApi.get_match_by_id(match_id)
IO.inspect(match)
message.data
end
end
+27
View File
@@ -0,0 +1,27 @@
defmodule Scrapper.MatchQueue do
use GenServer
def start_link(_opts) do
GenServer.start_link(__MODULE__, {}, name: __MODULE__)
end
def init({}) do
{:ok, connection} = AMQP.Connection.open()
{:ok, channel} = AMQP.Channel.open(connection)
{:ok, {channel, connection}}
end
@spec queue_match(String.t()) :: any()
def queue_match(match_id) do
GenServer.call(__MODULE__, {:queue_match, match_id})
end
def handle_call({:queue_match, match_id}, from, {channel, _} = state) do
AMQP.Basic.publish(channel, "", "match", match_id)
{:reply, nil, state}
end
def terminate(_reason, {_, connection}) do
AMQP.Connection.close(connection)
end
end
@@ -8,6 +8,8 @@ defmodule Scrapper.Application do
@impl true
def start(_type, _args) do
children = [
Scrapper.MatchQueue,
{Scrapper.MatchBroadway, []}
# Starts a worker by calling: Scrapper.Worker.start_link(arg)
# {Scrapper.Worker, arg}
]
@@ -12,7 +12,7 @@ defmodule Scrapper.Data.Api.MatchApi do
url = String.replace(@match_base_endpoint, "%{matchid}", match_id)
api_key = System.get_env("RIOT_API_KEY")
headers = [{"X-Riot-Token", api_key}]
response = HTTPoison.get!(url, headers, timeout: 5000)
response = HTTPoison.get!(url, headers, timeout: 5000)
case response.status_code do
200 ->
+3 -1
View File
@@ -31,7 +31,9 @@ defmodule Scrapper.MixProject do
{:ex_aws, "~> 2.1"},
{:ex_aws_s3, "~> 2.0"},
{:hackney, "~> 1.9"},
{:sweet_xml, "~> 0.6"}
{:sweet_xml, "~> 0.6"},
{:broadway_rabbitmq, "~> 0.7"},
{:amqp, "~> 3.3"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
# {:sibling_app_in_umbrella, in_umbrella: true}