Skip to content

Instantly share code, notes, and snippets.

@tallysmartins
Last active August 13, 2018 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tallysmartins/96153a5fe025ee8f13d3c1a375388fed to your computer and use it in GitHub Desktop.
Save tallysmartins/96153a5fe025ee8f13d3c1a375388fed to your computer and use it in GitHub Desktop.
Runner refactoring
# Elixir Bench Runner API
defmodule ElixirBench.Runner.Api do
@api_client Application.fetch_env(:runner, api_client)
#notify caller about submission status
def submit_results(caller, credentials, job, results) do
case @api_client.submit_results(credentials, results) do
:ok -> send(caller, :ok)
_ -> send(caller, :error)
end
end
end
defmodule ElixirBench.Runner do
use GenServer
# receiving the task_function as params here, I can change it in tests so it becomes easier
defp process_job(job_data, credentials, timeout // @job_timeout, task_fun // &Job.run_job/1) do
opts = [timeout: timeout, task_fun: task_fun]
job = build_job_struct(job_data)
job = Job.start_job(job, opts)
data = %{
elixir_version: job.config.elixir_version,
erlang_version: job.config.erlang_version,
log: job.log || ""
}
data = Map.merge(data, job.context)
# Receiving a message from the Api.submit_results I can assume all the my measurements are resulting in Api calls
with {:ok, _} <- Api.submit_results(self(), credentials, job, data),
{:ok, _} <- submit_measurements(job, credentials) do
_ -> Logger.info("Job processed and results updated")
else
_ -> Logger.info("Sending results for jobfailed, it will be re-claimed for new execution")
end
end
defp submit_measurements(job, credentials)
Enum.each(job.measurements, fn {benchmark_name, metrics} ->
{:ok, _} = Api.submit_results(self(), credentials, job, %{measurements: %{benchmark_name => metrics}})
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment