Skip to content

Commit 3753802

Browse files
committed
Create ElixirRuntime Prefix To Prevent Name Clashes
1 parent afe4d06 commit 3753802

19 files changed

+89
-64
lines changed

elixir_runtime/lib/application.ex renamed to elixir_runtime/lib/elixir_runtime/application.ex

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Runtime.Application do
4+
defmodule ElixirRuntime.Application do
55
@moduledoc """
66
The main OTP Application for the Elixir Runtime.
77
@@ -15,8 +15,11 @@ defmodule Runtime.Application do
1515

1616
def start(_type, _args) do
1717
children = [
18-
{Monitor.Server, [name: Monitor, client: LambdaServiceClient]},
19-
{Runtime, [client: LambdaServiceClient]}
18+
{
19+
ElixirRuntime.Monitor.Server,
20+
[name: ElixirRuntime.Monitor, client: ElixirRuntime.LambdaServiceClient]
21+
},
22+
{ElixirRuntime.Loop, [client: ElixirRuntime.LambdaServiceClient]}
2023
]
2124

2225
Supervisor.start_link(children, strategy: :one_for_one)

elixir_runtime/lib/service_client.ex renamed to elixir_runtime/lib/elixir_runtime/lambda_service_client.ex

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule LambdaServiceClient do
4+
defmodule ElixirRuntime.LambdaServiceClient do
55
@moduledoc """
66
This module represents an HTTP client for the Lambda Runtime service.
77
"""
88

99
require Logger
10-
@behaviour Runtime.Client
11-
@behaviour Monitor.Client
10+
@behaviour ElixirRuntime.Loop.Client
11+
@behaviour ElixirRuntime.Monitor.Client
1212

1313
def service_endpoint do
1414
System.get_env("AWS_LAMBDA_RUNTIME_API")
1515
end
1616

1717
@impl true
1818
@spec invocation_error(
19-
Monitor.Client.error(),
20-
Monitor.Client.id()
19+
ElixirRuntime.Monitor.Client.error(),
20+
ElixirRuntime.Monitor.Client.id()
2121
) :: no_return
2222
def invocation_error(err_msg, id) do
2323
url =

elixir_runtime/lib/runtime.ex renamed to elixir_runtime/lib/elixir_runtime/loop.ex

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Runtime do
4+
defmodule ElixirRuntime.Loop do
55
@moduledoc """
66
The main Runtime loop process.
77
@@ -13,14 +13,15 @@ defmodule Runtime do
1313
use Task, restart: :permanent
1414
require Logger
1515
alias __MODULE__
16-
alias Runtime.Handler, as: Handler
16+
alias Loop.Handler
17+
alias ElixirRuntime.Monitor
1718

1819
@type client :: module()
1920

2021
@doc "spawn a task to run the main loop asynchronously"
2122
def start_link(args \\ []) do
2223
client = Keyword.get(args, :client)
23-
Task.start_link(Runtime, :main, [client])
24+
Task.start_link(Loop, :main, [client])
2425
end
2526

2627
@doc "the main entrypoint for the runtime"

elixir_runtime/lib/runtime/client.ex renamed to elixir_runtime/lib/elixir_runtime/loop/client.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Runtime.Client do
4+
defmodule ElixirRuntime.Loop.Client do
55
@moduledoc "The Lambda Runtime Service Client behavior this runtime requires"
66

77
@type id :: String.t()

elixir_runtime/lib/runtime/handler.ex renamed to elixir_runtime/lib/elixir_runtime/loop/handler.ex

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Runtime.Handler do
4+
defmodule ElixirRuntime.Loop.Handler do
55
@moduledoc """
66
This module defines the Handler struct which is used to represent the
77
module-function atom pair which identifies a client's entrypoint.
@@ -20,8 +20,8 @@ defmodule Runtime.Handler do
2020
Manually create a handler from two atoms.
2121
## Examples
2222
23-
iex> Runtime.Handler.new(Elixir.Example, :handle)
24-
%Runtime.Handler{module: Elixir.Example, function: :handle}
23+
iex> ElixirRuntime.Loop.Handler.new(Elixir.Example, :handle)
24+
%ElixirRuntime.Loop.Handler{module: Elixir.Example, function: :handle}
2525
"""
2626
def new(module, function) when is_atom(module) and is_atom(function) do
2727
%Handler{module: module, function: function}
@@ -32,8 +32,8 @@ defmodule Runtime.Handler do
3232
## Examples
3333
3434
iex> System.put_env("_HANDLER", "Elixir.Example:handle")
35-
iex> Runtime.Handler.configured()
36-
%Runtime.Handler{module: Elixir.Example, function: :handle}
35+
iex> ElixirRuntime.Loop.Handler.configured()
36+
%ElixirRuntime.Loop.Handler{module: Elixir.Example, function: :handle}
3737
"""
3838
def configured do
3939
[module, function] = handler_string() |> String.split(":", trim: true)
@@ -46,8 +46,8 @@ defmodule Runtime.Handler do
4646
4747
Create a handler for String.trim and invoke it to get a result.
4848
iex> defmodule Example, do: def func(body, _context), do: body
49-
iex> handler = Runtime.Handler.new(Example, :func)
50-
iex> handler |> Runtime.Handler.invoke(%{msg: "hello"}, %{})
49+
iex> handler = ElixirRuntime.Loop.Handler.new(Example, :func)
50+
iex> handler |> ElixirRuntime.Loop.Handler.invoke(%{msg: "hello"}, %{})
5151
%{msg: "hello"}
5252
"""
5353
def invoke(%Handler{module: module, function: function}, body, context)

elixir_runtime/lib/runtime/monitor.ex renamed to elixir_runtime/lib/elixir_runtime/loop/monitor.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Runtime.Monitor do
4+
defmodule ElixirRuntime.Loop.Monitor do
55
@moduledoc """
66
The Runtime requires a stateful monitor which will observe any
77
failures and call the proper backend APIs.
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Monitor do
4+
defmodule ElixirRuntime.Monitor do
55
@moduledoc """
66
The monitor is responsible for reporting errors in the Elixir process to
77
the AWS Lambda runtime service.
88
The monitor is a stateful process which tracks the request ID for the
99
currently-executing function.
1010
"""
1111

12-
@behaviour Runtime.Monitor
13-
14-
alias __MODULE__
12+
@behaviour ElixirRuntime.Loop.Monitor
1513

14+
@doc """
15+
Tell the monitor server to watch the given process.
16+
"""
1617
@impl true
17-
def watch(monitor \\ Monitor, process) do
18+
def watch(monitor \\ ElixirRuntime.Monitor, process) when is_pid(process) do
1819
GenServer.call(monitor, {:watch, process})
1920
end
2021

22+
@doc """
23+
Reset the monitor back to it's initial state, forgetting any currently-known
24+
ingestor IDs.
25+
"""
2126
@impl true
22-
def reset(monitor \\ Monitor) do
27+
def reset(monitor \\ ElixirRuntime.Monitor) do
2328
GenServer.call(monitor, :reset)
2429
end
2530

31+
@doc """
32+
Notify the monitor that the runtime loop has started processing an invocation.
33+
"""
2634
@impl true
27-
def started(monitor \\ Monitor, id) do
35+
def started(monitor \\ ElixirRuntime.Monitor, id) do
2836
GenServer.call(monitor, {:start_invocation, id})
2937
end
3038
end

elixir_runtime/lib/monitor/client.ex renamed to elixir_runtime/lib/elixir_runtime/monitor/client.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Monitor.Client do
4+
defmodule ElixirRuntime.Monitor.Client do
55
@moduledoc """
66
This module defines the client behavior required by the runtime monitor.
77
"""

elixir_runtime/lib/monitor/error.ex renamed to elixir_runtime/lib/elixir_runtime/monitor/error.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Monitor.Error do
4+
defmodule ElixirRuntime.Monitor.Error do
55
@moduledoc """
66
This module defines the Error struct which is used to communicate runtime
77
errors to the Lambda Runtime Service.

elixir_runtime/lib/monitor/server.ex renamed to elixir_runtime/lib/elixir_runtime/monitor/server.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Monitor.Server do
4+
defmodule ElixirRuntime.Monitor.Server do
55
use GenServer
66

77
alias __MODULE__
8-
alias Monitor.State, as: State
8+
alias ElixirRuntime.Monitor.State, as: State
99

1010
def start_link(args) do
1111
client = Keyword.get(args, :client)

elixir_runtime/lib/monitor/state.ex renamed to elixir_runtime/lib/elixir_runtime/monitor/state.ex

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Monitor.State do
4+
defmodule ElixirRuntime.Monitor.State do
5+
alias ElixirRuntime.Monitor
6+
57
@type monitor_state ::
68
{:not_started, Monitor.Client.t()}
79
| {:in_progress, Monitor.Client.id(), Monitor.Client.t()}

elixir_runtime/mix.exs

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ defmodule Lambda.MixProject do
1919
source_url: "https://github.com/aws-samples/aws-lambda-elixir-runtime",
2020
homepage_url: "https://github.com/aws-samples/aws-lambda-elixir-runtime",
2121
docs: [
22-
source_url_pattern: "https://github.com/aws-samples/aws-lambda-elixir-runtime/blob/master/elixir_runtime/%{path}#L%{line}",
22+
source_url_pattern:
23+
"https://github.com/aws-samples/aws-lambda-elixir-runtime/blob/master/elixir_runtime/%{path}#L%{line}",
2324
main: "readme",
2425
extras: [
2526
"README.md",
@@ -32,7 +33,7 @@ defmodule Lambda.MixProject do
3233
# Run "mix help compile.app" to learn about applications.
3334
def application do
3435
[
35-
mod: {Runtime.Application, []},
36+
mod: {ElixirRuntime.Application, []},
3637
extra_applications: [:logger, :inets]
3738
]
3839
end

elixir_runtime/test/application_test.exs renamed to elixir_runtime/test/elixir_runtime/application_test.exs

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Application.Test do
4+
defmodule ElixirRuntime.Application.Test do
55
use ExUnit.Case
66

77
# This test suite is for the end-to-end behavior of the elixir runtime
88
# application. The tests leverage an in-memory client which echos requests
99
# back to the test process for validation.
1010

11-
def echo_msg(body = %{"msg" => message}, _context), do: message
11+
def echo_msg(_request = %{"msg" => message}, _context), do: message
1212

1313
defmodule MyCustomBug do
1414
defexception [:message]
@@ -29,7 +29,7 @@ defmodule Application.Test do
2929
invocation = Support.FakeInvoke.with_message("hello world")
3030
expected_id = Support.FakeInvoke.id(invocation)
3131

32-
System.put_env("_HANDLER", "Elixir.Application.Test:echo_msg")
32+
System.put_env("_HANDLER", "Elixir.ElixirRuntime.Application.Test:echo_msg")
3333
start_with_invocations([invocation])
3434

3535
assert_receive {:next, ^invocation}
@@ -40,11 +40,19 @@ defmodule Application.Test do
4040
invoke = Support.FakeInvoke.with_message("some message")
4141
expected_id = Support.FakeInvoke.id(invoke)
4242

43-
System.put_env("_HANDLER", "Elixir.Application.Test:logical_bug_handler")
43+
System.put_env(
44+
"_HANDLER",
45+
"Elixir.ElixirRuntime.Application.Test:logical_bug_handler"
46+
)
47+
4448
start_with_invocations([invoke])
4549

4650
assert_receive {:invocation_error, msg, ^expected_id}
47-
assert String.contains?(msg, "FunctionElixir.Application.Test.MyCustomBug")
51+
52+
assert String.contains?(
53+
msg,
54+
"FunctionElixir.ElixirRuntime.Application.Test.MyCustomBug"
55+
)
4856
end
4957

5058
test "a missing handler string" do
@@ -80,15 +88,16 @@ defmodule Application.Test do
8088
# This is the same process as starting the application but it's done
8189
# piece-by-piece here so the InMemoryClient can be passed into the Runtime
8290
# and monitor.
83-
defp start_with_invocations(invocations \\ []) do
91+
defp start_with_invocations(invocations) do
8492
start_supervised!(
8593
{Support.InMemoryClient, %{pending: invocations, listener: self()}}
8694
)
8795

88-
start_supervised!(
89-
{Monitor.Server, [client: Support.InMemoryClient, name: Monitor]}
90-
)
96+
start_supervised!({
97+
ElixirRuntime.Monitor.Server,
98+
[client: Support.InMemoryClient, name: ElixirRuntime.Monitor]
99+
})
91100

92-
start_supervised!({Runtime, [client: Support.InMemoryClient]})
101+
start_supervised!({ElixirRuntime.Loop, [client: Support.InMemoryClient]})
93102
end
94103
end
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Runtime.Handler.Test do
4+
defmodule ElixirRuntime.Loop.Handler.Test do
55
use ExUnit.Case
6-
doctest Runtime.Handler
6+
doctest ElixirRuntime.Loop.Handler
77
end

elixir_runtime/test/monitor/error_test.exs renamed to elixir_runtime/test/elixir_runtime/monitor/error_test.exs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Monitor.Error.Test do
4+
defmodule ElixirRuntime.Monitor.Error.Test do
55
use ExUnit.Case, async: true
6-
doctest Monitor.Error
6+
doctest ElixirRuntime.Monitor.Error
77

8-
alias Monitor.Error, as: Error
8+
alias ElixirRuntime.Monitor.Error
99

1010
setup do
1111
{:current_stacktrace, stacktrace} =

elixir_runtime/test/monitor/state_test.exs renamed to elixir_runtime/test/elixir_runtime/monitor/state_test.exs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: MIT-0
33

4-
defmodule Monitor.State.Test do
4+
defmodule ElixirRuntime.Monitor.State.Test do
55
use ExUnit.Case, async: true
6-
doctest Monitor.State
6+
doctest ElixirRuntime.Monitor.State
77

8-
alias Monitor.State, as: State
8+
alias ElixirRuntime.Monitor
9+
alias Monitor.State
910

1011
defmodule FakeClient do
11-
@behaviour Monitor.Client
12+
@behaviour ElixirRuntime.Monitor.Client
1213

1314
@impl true
1415
def init_error(err_msg) do

elixir_runtime/test/support/fake_invoke.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule Support.FakeInvoke do
1616
def body({_id, body, _context}), do: body
1717

1818
defp generated_id do
19-
id = Integer.to_string(:random.uniform(100_000_000), 32)
19+
id = Integer.to_string(:rand.uniform(100_000_000), 32)
2020
"TestId-#{id}"
2121
end
2222
end

0 commit comments

Comments
 (0)