Skip to content

Commit

Permalink
tasks
Browse files Browse the repository at this point in the history
using task module, request for sensors
  • Loading branch information
hexisdylan committed Jul 25, 2022
1 parent 5430de1 commit 41eea20
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@

22. pragstudio-elixir-21-processes-1 - [Processes 1](https://github.com/skedaddl3/elixir-pragstudio/commit/3b109474f3597d08aff6983599e0576b5786218c)

23. pragstudio-elixir-21-processes-2 - [Processes 2]()
23. pragstudio-elixir-21-processes-2 - [Processes 2](https://github.com/skedaddl3/elixir-pragstudio/commit/3b109474f3597d08aff6983599e0576b5786218c)

24. pragstudio-elixir-22-messages - [Messages]()
24. pragstudio-elixir-22-messages - [Messages](https://github.com/skedaddl3/elixir-pragstudio/commit/5430de13ba6b4f9eb004bc10934d398f5a7cdadd)

25. pragstudio-elixir-23-tasks - [Tasks]()

Expand Down
13 changes: 13 additions & 0 deletions lib/fetcher.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Servy.Fetcher do
def async(fun) do
parent = self()

spawn(fn -> send(parent, {self(), :result, fun.()}) end)
end

def get_result(pid) do
receive do
{^pid, :result, value} -> value
end
end
end
35 changes: 15 additions & 20 deletions lib/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule Servy.Handler do
alias Servy.Conv
alias Servy.BearController
alias Servy.VideoCam
# alias Servy.Fetcher

import Servy.Plugins, only: [rewrite_path: 1, log: 1, track: 1]
import Servy.Parser, only: [parse: 1]
@moduledoc "Handles HTTP requests."
Expand All @@ -21,35 +23,28 @@ defmodule Servy.Handler do
# route(conv, conv.method, conv.path)
# end

def route(%Conv{method: "GET", path: "/snapshots"} = conv) do
# the request-handling process
parent = self()
def route(%Conv{method: "GET", path: "/sensors"} = conv) do
task = Task.async(fn -> Servy.Tracker.get_location("bigfoot") end)

snapshots =
["cam-1", "cam-2", "cam-3"]
|> Enum.map(&Task.async(fn -> VideoCam.get_snapshot(&1) end))
|> Enum.map(&Task.await/1)

spawn(fn -> send(parent, {:result, VideoCam.get_snapshot("cam-1")}) end)
spawn(fn -> send(parent, {:result, VideoCam.get_snapshot("cam-2")}) end)
spawn(fn -> send(parent, {:result, VideoCam.get_snapshot("cam-3")}) end)
where_is_bigfoot = Task.await(task)

snapshot1 =
receive do
{:result, filename} -> filename
end
# snapshot1 = Fetcher.get_result(pid1)

snapshot2 =
receive do
{:result, filename} -> filename
end
# snapshot2 = Fetcher.get_result(pid2)

snapshot3 =
receive do
{:result, filename} -> filename
end
# snapshot3 = Fetcher.get_result(pid3)

# snapshot2 = VideoCam.get_snapshot("cam-2")
# snapshot3 = VideoCam.get_snapshot("cam-3")

snapshots = [snapshot1, snapshot2, snapshot3]
# snapshots = [snapshot1, snapshot2, snapshot3]

%{conv | status: 200, resp_body: inspect(snapshots)}
%{conv | status: 200, resp_body: inspect({snapshots, where_is_bigfoot})}
end

def route(%Conv{method: "GET", path: "/kaboom"}) do
Expand Down
22 changes: 22 additions & 0 deletions lib/tracker.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Servy.Tracker do
@doc """
Simulates sending a request to an external API
to get the GPS coordinates of a wildthing.
"""
def get_location(wildthing) do
# CODE GOES HERE TO SEND A REQUEST TO THE EXTERNAL API

# Sleep to simulate the API delay:
:timer.sleep(500)

# Example responses returned from the API:
locations = %{
"roscoe" => %{lat: "44.4280 N", lng: "110.5885 W"},
"smokey" => %{lat: "48.7596 N", lng: "113.7870 W"},
"brutus" => %{lat: "43.7904 N", lng: "110.6818 W"},
"bigfoot" => %{lat: "29.0469 N", lng: "98.8667 W"}
}

Map.get(locations, wildthing)
end
end

0 comments on commit 41eea20

Please sign in to comment.