-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ian Vaughan
committed
Oct 5, 2018
1 parent
5881802
commit 03760bf
Showing
14 changed files
with
400 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
export KAFKA_BROKERS="localhost:9092,localhost:9093" | ||
export KAFKA_HEARTBEAT_INTERVAL=1000 | ||
export KAFKA_COMMIT_INTERVAL=1000 | ||
export KAFKA_SCHEMA_REGISTRY_URL="http://localhost:8081" | ||
export KAFKA_TOPIC_NAME="uk.london.quiqup.slots" | ||
export START_WORKERS=true | ||
export SCHEMA_REGISTRY_URL="http://localhost:8081" | ||
export AVLIZER_CONFLUENT_SCHEMAREGISTRY_URL="http://localhost:8081" | ||
export STATSD_HOST="localhost" | ||
export STATSD_PORT=8125 | ||
export WIW_KEY=123 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# shift = %{"account_id" => 77967, "acknowledged" => 1, "acknowledged_at" => "Mon, 01 Oct 2018 15:27:30 +0100", "actionable" => false, "alerted" => false, "block_id" => 0, "break_time" => 0, "color" => "74a611", "created_at" => "Wed, 26 Sep 2018 12:24:58 +0100", "creator_id" => 5526232, "end_time" => "Mon, 08 Oct 2018 00:15:00 +0100", "id" => 2076303948, "instances" => 0, "is_open" => false, "linked_users" => nil, "location_id" => 3999871, "notes" => "", "notified_at" => "Tue, 02 Oct 2018 22:32:58 +0100", "position_id" => 709909, "published" => true, "published_date" => "Wed, 26 Sep 2018 16:59:05 +0100", "shiftchain_key" => "", "site_id" => 3530221, "start_time" => "Sun, 07 Oct 2018 21:00:00 +0100", "updated_at" => "Mon, 01 Oct 2018 15:27:30 +0100", "user_id" => 29205213} | ||
|
||
shift = %{ | ||
"account_id" => 77967, | ||
"acknowledged" => 1, | ||
"acknowledged_at" => nil, | ||
"actionable" => false, | ||
"alerted" => false, | ||
"block_id" => 0, | ||
"break_time" => 0, | ||
"color" => "74a611", | ||
"created_at" => "Wed, 26 Sep 2018 12:24:58 +0100", | ||
"creator_id" => 5_526_232, | ||
"end_time" => "Mon, 08 Oct 2018 00:15:00 +0100", | ||
"id" => 2_076_303_948, | ||
"instances" => 0, | ||
"is_open" => false, | ||
"linked_users" => nil, | ||
"location_id" => 3_999_871, | ||
"notes" => "", | ||
"notified_at" => "Tue, 02 Oct 2018 22:32:58 +0100", | ||
"position_id" => 709_909, | ||
"published" => true, | ||
"published_date" => "Wed, 26 Sep 2018 16:59:05 +0100", | ||
"shiftchain_key" => "", | ||
"site_id" => 3_530_221, | ||
"start_time" => "Sun, 07 Oct 2018 21:00:00 +0100", | ||
"updated_at" => "Mon, 01 Oct 2018 15:27:30 +0100", | ||
"user_id" => 29_205_213 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# Slot Sync | ||
|
||
* Syncs slot data from external provider | ||
* Caches the data locally in Redis | ||
* If remote data changed then publishes to Kafka |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
use Mix.Config |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use Mix.Config | ||
|
||
# Do not print debug messages in production | ||
config :logger, level: :info | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use Mix.Config | ||
config :logger, level: :debug | ||
|
||
config :slot_sync, SlotSync.Application, start_workers: false | ||
|
||
|
||
config :event_serializer, enabled: false |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule SlotSync.KafkaConfig do | ||
require Logger | ||
|
||
def brokers do | ||
:slot_sync | ||
|> Confex.get_env(:brokers) | ||
|> brokers() | ||
|> IO.inspect(label: "brokers") | ||
end | ||
|
||
defp brokers(nil), do: nil | ||
defp brokers(list) when is_list(list), do: list | ||
|
||
defp brokers(value) when is_binary(value) do | ||
for line <- String.split(value, ","), into: [] do | ||
case line |> String.trim() |> String.split(":") do | ||
[host] -> | ||
msg = "Port not set for kafka broker #{host}" | ||
Logger.warn(msg) | ||
raise msg | ||
|
||
[host, port] -> | ||
{port, _} = Integer.parse(port) | ||
{host, port} | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
defmodule SlotSync.Publishers.Kafka do | ||
@moduledoc """ | ||
Formats and encodes the payload and the publishes it to Kafka | ||
""" | ||
use Confex, otp_app: :slot_sync | ||
|
||
alias SlotSync.TrackingLocationsFormatter | ||
alias KafkaEx.Protocol.Produce.Request | ||
alias KafkaEx.Protocol.Produce.Message | ||
require Logger | ||
|
||
# @type key() :: String.t() | ||
# @type event() :: String.t() | ||
# @type topic_name() :: String.t() | atom() | ||
# @type encoded_message() :: String.t() | ||
|
||
@doc """ | ||
This is the main function to trigger a message to be published to Kafka | ||
## Examples | ||
""" | ||
|
||
# @type message_number() :: integer() | ||
# @spec call(map(), integer(), topic_name()) :: {:ok, message_number()} | {:error, String.t()} | ||
def call(data, id) do | ||
id | ||
|> build_event_key() | ||
|> encode_event_message(data) | ||
|> build_request(data) | ||
|> publish() | ||
end | ||
|
||
defp build_event_key(id), do: [{"id", id}] | ||
|
||
# @spec encode_event_message(key, event, topic_name) :: | ||
# {:encoded_key, encoded_message, encoded_message: encoded_message} | {:error, key} | ||
defp encode_event_message(key, event) do | ||
with {:ok, encoded_key} <- encoder().call(key_schema_name(), key), | ||
{:ok, encoded_message} <- encoder().call(value_schema_name(), transformed(event)) do | ||
%{encoded_key: encoded_key, encoded_message: encoded_message} | ||
else | ||
{:error, error} -> {:error, error} | ||
end | ||
end | ||
|
||
defp transformed(event) do | ||
event["linked_users"] |> IO.inspect(label: "linked_users") | ||
|
||
[ | ||
{"account_id", event["account_id"]}, | ||
{"acknowledged", event["acknowledged"]}, | ||
{"acknowledged_at", string_or_nil(event["acknowledged_at"])}, | ||
{"actionable", event["actionable"]}, | ||
{"alerted", event["alerted"]}, | ||
{"block_id", event["block_id"]}, | ||
{"break_time", event["break_time"]}, | ||
{"color", event["color"]}, | ||
{"created_at", event["created_at"]}, | ||
{"creator_id", event["creator_id"]}, | ||
{"end_time", event["end_time"]}, | ||
{"id", event["id"]}, | ||
{"instances", event["instances"]}, | ||
{"is_open", event["is_open"]}, | ||
# {"linked_users", event["linked_users"]}, | ||
{"linked_users", :null}, | ||
{"location_id", event["location_id"]}, | ||
{"notes", event["notes"]}, | ||
{"notified_at", string_or_nil(event["notified_at"])}, | ||
{"position_id", event["position_id"]}, | ||
{"published", event["published"]}, | ||
{"published_date", event["published_date"]}, | ||
{"shiftchain_key", event["shiftchain_key"]}, | ||
{"site_id", event["site_id"]}, | ||
{"start_time", event["start_time"]}, | ||
{"updated_at", event["updated_at"]}, | ||
{"user_id", event["user_id"]} | ||
] | ||
end | ||
|
||
defp string_or_nil(_), do: {"null", :null} | ||
defp string_or_nil(value), do: {"string", value} | ||
|
||
defp build_request({:error, error}, event) do | ||
stats("publisher.encoding.error") | ||
Logger.warn("Failed to decode event:#{inspect(event)}, error:#{inspect(error)}") | ||
{:error, error} | ||
end | ||
|
||
defp build_request(%{encoded_key: encoded_key, encoded_message: encoded_message}, _event) do | ||
stats("publisher.encoding.success") | ||
|
||
messages = [%Message{key: encoded_key, value: encoded_message}] | ||
|
||
{:ok, | ||
%Request{ | ||
topic: topic_name(), | ||
partition: 0, | ||
required_acks: 1, | ||
messages: messages | ||
}} | ||
end | ||
|
||
defp publish({:error, error}), do: {:error, error} | ||
defp publish({:ok, request}), do: request |> kafka_client().produce() | ||
defp kafka_client(), do: config()[:kafka_client] | ||
|
||
defp key_schema_name, do: topic_name() <> "-key" | ||
defp value_schema_name, do: topic_name() <> "-value" | ||
|
||
defp topic_name, do: config()[:topic_name] | ||
|
||
defp encoder(), do: config()[:event_serializer_encoder] | ||
|
||
defp stats(name), do: DogStatsd.increment(:datadogstatsd, name) | ||
end |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"type": "record", | ||
"name": "Key", | ||
"namespace": "uk.london.quiqup.slots", | ||
"fields": [{ | ||
"name": "id", | ||
"type": "int", | ||
"doc": " The id of the user from which the slot was generated" | ||
}], | ||
"connect.name": "uk.london.quiqup.slots.Key" | ||
} |
Oops, something went wrong.