-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
9 changed files
with
235 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
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
43 changes: 43 additions & 0 deletions
43
server-phoenix/lib/helios/workers/weather_poller_worker.ex
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,43 @@ | ||
defmodule Helios.Workers.WeatherPollerWorker do | ||
use Absinthe.Schema.Notation | ||
import Ecto.Query | ||
alias Helios.{Repo, Location} | ||
alias HeliosWeb.Schema.Types.Sub | ||
alias HeliosWeb.Clients.WeatherClient | ||
|
||
def perform do | ||
IO.puts("Running weather poll") | ||
|
||
Enum.each(locations, fn location -> | ||
get_forecast(location) | ||
end) | ||
end | ||
|
||
def subscriptions do | ||
Sub.get_args() |> Enum.uniq() | ||
end | ||
|
||
defmodule LocationParams do | ||
defstruct [:latitude, :longitude] | ||
end | ||
|
||
def locations do | ||
Enum.map(subscriptions, fn subscription -> | ||
%LocationParams{latitude: subscription.latitude, longitude: subscription.longitude} | ||
end) | ||
end | ||
|
||
def get_forecast(location) do | ||
case WeatherClient.forecast(%{latitude: location.latitude, longitude: location.longitude}) do | ||
{:ok, forecast_data} -> | ||
Absinthe.Subscription.publish( | ||
HeliosWeb.Endpoint, | ||
forecast_data, | ||
weather_published: [location.latitude, location.longitude] | ||
) | ||
|
||
{:error, message} -> | ||
IO.puts("Failure polling #{location.latitude} and #{location.longitude}: #{message}") | ||
end | ||
end | ||
end |
101 changes: 101 additions & 0 deletions
101
server-phoenix/lib/helios_web/AbsintheChannelDecorator.ex
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,101 @@ | ||
# Reference code | ||
# https://github.com/absinthe-graphql/absinthe_phoenix/issues/39#issuecomment-982310059 | ||
|
||
defmodule HeliosWeb.AbsintheChannelDecorator do | ||
use Phoenix.Channel | ||
alias HeliosWeb.AbsintheSocketDecorator, as: Socket | ||
require Logger | ||
|
||
def join(topic, msg, socket) do | ||
IO.puts("channel decorator JOIN") | ||
Absinthe.Phoenix.Channel.join(topic, msg, socket) | ||
end | ||
|
||
def terminate(reason, socket) do | ||
IO.puts("channel decorator TERMINATE") | ||
|
||
case reason do | ||
{:shutdown, :closed} -> | ||
socket = run_unsubscribe(socket, "terminate") | ||
{:noreply, socket} | ||
|
||
_ -> | ||
IO.puts("nothing") | ||
end | ||
end | ||
|
||
defp run_unsubscribe(socket, status) do | ||
# status (enter, leave and terminate) | ||
config = socket.assigns[:absinthe] | ||
|
||
Map.get(socket.assigns.absinthe.opts[:context], "payload", "") | ||
|> case do | ||
payload when payload != "" -> | ||
with variables when is_map(variables) <- extract_variables(payload) do | ||
query = Map.get(payload, "query", "") | ||
|
||
config_opts = [ | ||
context: | ||
Map.merge( | ||
config.opts[:context], | ||
%{"status" => status} | ||
) | ||
] | ||
|
||
opts = Keyword.put(config_opts, :variables, variables) | ||
context = socket.assigns.absinthe.opts[:context] | ||
run(query, config[:schema], config[:pipeline], opts) | ||
end | ||
|
||
_ -> | ||
socket | ||
end | ||
end | ||
|
||
defp run(document, schema, pipeline, options) do | ||
{module, fun} = pipeline | ||
|
||
case Absinthe.Pipeline.run(document, apply(module, fun, [schema, options])) do | ||
{:ok, %{result: result, execution: res}, _phases} -> | ||
{:ok, result, res.context} | ||
|
||
{:error, msg, _phases} -> | ||
{:error, msg} | ||
end | ||
end | ||
|
||
defp extract_variables(payload) do | ||
case Map.get(payload, "variables", %{}) do | ||
nil -> %{} | ||
map -> map | ||
end | ||
end | ||
|
||
def handle_in("doc", payload, socket) do | ||
socket = | ||
Socket.put_options( | ||
socket, | ||
context: | ||
Map.merge( | ||
socket.assigns.absinthe.opts[:context], | ||
%{ | ||
"status" => "enter", | ||
"payload" => %{ | ||
"query" => Map.get(payload, "query", "") | ||
} | ||
} | ||
) | ||
) | ||
|
||
Absinthe.Phoenix.Channel.handle_in("doc", payload, socket) | ||
end | ||
|
||
def handle_in("unsubscribe", %{"subscriptionId" => doc_id}, socket) do | ||
socket = run_unsubscribe(socket, "leave") | ||
Absinthe.Phoenix.Channel.handle_in("unsubscribe", %{"subscriptionId" => doc_id}, socket) | ||
end | ||
|
||
defdelegate handle_in(event, msg, arg2), to: Absinthe.Phoenix.Channel | ||
|
||
defdelegate default_pipeline(schema, options), to: Absinthe.Phoenix.Channel | ||
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,21 @@ | ||
defmodule HeliosWeb.AbsintheSocketDecorator do | ||
use Phoenix.Socket | ||
|
||
channel("__absinthe__:*", HeliosWeb.AbsintheChannelDecorator, | ||
assigns: %{ | ||
__absinthe_schema__: HeliosWeb.Schema, | ||
__absinthe_pipeline__: nil | ||
} | ||
) | ||
|
||
def connect(params, socket) do | ||
IO.puts("socket decorator connect") | ||
{:ok, socket} | ||
end | ||
|
||
def id(_socket), do: nil | ||
|
||
defdelegate put_options(socket, opts), to: Absinthe.Phoenix.Socket | ||
|
||
defdelegate put_schema(socket, schema), to: Absinthe.Phoenix.Socket | ||
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,19 @@ | ||
defmodule HeliosWeb.Channel do | ||
use HeliosWeb, :channel | ||
alias HeliosWeb.Presence | ||
|
||
def join("some:topic", _params, socket) do | ||
send(self(), :after_join) | ||
{:ok, assign(socket, :user_id)} | ||
end | ||
|
||
def handle_info(:after_join, socket) do | ||
{:ok, _} = | ||
Presence.track(socket, socket.assigns.user_id, %{ | ||
online_at: inspect(System.system_time(:second)) | ||
}) | ||
|
||
push(socket, "presence_state", Presence.list(socket)) | ||
{:noreply, socket} | ||
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
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