Skip to content

Commit

Permalink
#4 adding run_async for lambda executions
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrudnick committed Jan 4, 2019
1 parent 90aaa90 commit 4e7eb29
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ npm-debug.log
.elixir_ls

lambdex.iml
.idea/
.idea/
.tool-versions
21 changes: 21 additions & 0 deletions apps/lambdex_core/lib/lambdex_core.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,25 @@ defmodule LambdexCore do

LambdexCore.LambdaExecution.run_sync(pid)
end

@doc """
Run asynchronously a lambda function
## Examples
iex> LambdexCore.run_async("fn -> :hello_lambda end", %{})
:ok
"""
def run_async(lambda_source, lambda_envs, lambda_params \\ [])
def run_async(lambda_source, lambda_envs, nil), do: run_async(lambda_source, lambda_envs, [])
def run_async(lambda_source, lambda_envs, lambda_params) when is_map(lambda_params) and map_size(lambda_params) == 0, do: run_async(lambda_source, lambda_envs, [])
def run_async(lambda_source, lambda_envs, lambda_params) when is_binary(lambda_source) do
{:ok, pid} =
DynamicSupervisor.start_child(
LambdexCore.ExecutionSupervisor,
{LambdexCore.LambdaExecution, {lambda_source, lambda_envs, lambda_params}}
)

LambdexCore.LambdaExecution.run_async(pid)
end
end
43 changes: 27 additions & 16 deletions apps/lambdex_core/lib/lambdex_core/lambda_execution.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ defmodule LambdexCore.LambdaExecution do

@impl true
def handle_call(:run, _from, state) do
execution = run_lambda(state)

{:reply, execution, %{state | execution: execution}}
end

@impl true
def handle_cast(:run, state) do
execution = run_lambda(state)

{:noreply, %{state | execution: execution}}
end

defp run_lambda(state) do
start_time = System.monotonic_time(:milliseconds)
at_time = :os.system_time(:seconds)

Expand All @@ -35,16 +48,19 @@ defmodule LambdexCore.LambdaExecution do

duration = System.monotonic_time(:milliseconds) - start_time
reductions = get_process_reduction_count()
execution =
state.execution
|> Execution.put_executed_at(at_time)
|> Execution.put_duration(duration)
|> Execution.put_reductions(reductions)
|> Execution.put_result(result)
|> Execution.put_params(state.lambda_params)
|> Execution.put_envs(state.lambda_envs)

{:reply, execution, %{state | execution: execution}}
state.execution
|> Execution.put_executed_at(at_time)
|> Execution.put_duration(duration)
|> Execution.put_reductions(reductions)
|> Execution.put_result(result)
|> Execution.put_params(state.lambda_params)
|> Execution.put_envs(state.lambda_envs)
end

defp get_process_reduction_count() do
{:ok, reductions} = Keyword.fetch(:erlang.process_info(self()), :reductions)
reductions
end

############
Expand All @@ -55,12 +71,7 @@ defmodule LambdexCore.LambdaExecution do
GenServer.call(pid, :run)
end

# def start_supervised_task(lambda) do
# Task.Supervisor.async_nolink(LambdexCore.LambdaTaskSupervisor, lambda.code, lambda.params)
# end

defp get_process_reduction_count() do
{:ok, reductions} = Keyword.fetch(:erlang.process_info(self()), :reductions)
reductions
def run_async(pid) do
GenServer.cast(pid, :run)
end
end
6 changes: 1 addition & 5 deletions apps/lambdex_core/test/lambdex_core_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
defmodule LambdexCoreTest do
use ExUnit.Case
doctest LambdexCore

test "greets the world" do
assert LambdexCore.hello() == :world
end

end

0 comments on commit 4e7eb29

Please sign in to comment.