-
-
Notifications
You must be signed in to change notification settings - Fork 97
fix: build a single engine for multiple projects using the same toolchain versions #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
doorgan
wants to merge
1
commit into
main
Choose a base branch
from
doorgan/deduplicate-engine-builder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,9 @@ | ||
| defmodule Expert.EngineBuild.DynamicSupervisor do | ||
| def name do | ||
| Expert.EngineBuildSupervisor | ||
| end | ||
|
|
||
| def options do | ||
| [name: name(), strategy: :one_for_one] | ||
| end | ||
| end |
This file contains hidden or 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,179 @@ | ||
| defmodule Expert.EngineBuilds do | ||
| use GenServer | ||
|
|
||
| alias Expert.EngineNode.Builder | ||
| alias Forge.Project | ||
|
|
||
| require Logger | ||
|
|
||
| defmodule State do | ||
| defstruct ready: %{}, pending: %{} | ||
| end | ||
|
|
||
| @type build_result :: {[String.t()], String.t() | nil} | ||
|
|
||
| def child_spec(_) do | ||
| %{ | ||
| id: __MODULE__, | ||
| start: {__MODULE__, :start_link, []} | ||
| } | ||
| end | ||
|
|
||
| def start_link do | ||
| GenServer.start_link(__MODULE__, [], name: __MODULE__) | ||
| end | ||
|
|
||
| @spec request_engine(Project.t()) :: {:ok, build_result()} | {:error, term()} | ||
| def request_engine(%Project{} = project) do | ||
| with {:ok, key, build} <- resolve_build(project) do | ||
| GenServer.call(__MODULE__, {:request_engine, key, project, build}, :infinity) | ||
| end | ||
| end | ||
|
|
||
| @impl GenServer | ||
| def init(_) do | ||
| {:ok, %State{}} | ||
| end | ||
|
|
||
| @impl GenServer | ||
| def handle_call({:request_engine, key, project, build}, from, state) do | ||
| case Map.fetch(state.ready, key) do | ||
| {:ok, result} -> | ||
| {:reply, {:ok, result}, state} | ||
|
|
||
| :error -> | ||
| case Map.fetch(state.pending, key) do | ||
| {:ok, pending} -> | ||
| pending = %{pending | waiters: [from | pending.waiters]} | ||
| pending_map = Map.put(state.pending, key, pending) | ||
|
|
||
| {:noreply, %State{state | pending: pending_map}} | ||
|
|
||
| :error -> | ||
| case start_builder(project, build, key) do | ||
| {:ok, pid} -> | ||
| pending = %{pid: pid, ref: Process.monitor(pid), waiters: [from]} | ||
| pending_map = Map.put(state.pending, key, pending) | ||
|
|
||
| {:noreply, %State{state | pending: pending_map}} | ||
|
|
||
| {:error, reason} -> | ||
| {:reply, {:error, reason}, state} | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @impl GenServer | ||
| def handle_info({:engine_build_complete, key, pid, result}, state) do | ||
| case pop_pending_by_key(state, key, pid) do | ||
| {:ok, pending, state} -> | ||
| Process.demonitor(pending.ref, [:flush]) | ||
| reply_all(pending.waiters, result) | ||
|
|
||
| state = | ||
| case result do | ||
| {:ok, engine} -> %State{state | ready: Map.put(state.ready, key, engine)} | ||
| _ -> state | ||
| end | ||
|
|
||
| {:noreply, state} | ||
|
|
||
| :error -> | ||
| {:noreply, state} | ||
| end | ||
| end | ||
|
|
||
| def handle_info({:DOWN, ref, :process, pid, reason}, state) do | ||
| case pop_pending_by_ref(state, ref, pid) do | ||
| {:ok, pending, state} -> | ||
| reply_all(pending.waiters, {:error, reason}) | ||
| {:noreply, state} | ||
|
|
||
| :error -> | ||
| {:noreply, state} | ||
| end | ||
| end | ||
|
|
||
| defp resolve_build(%Project{} = project) do | ||
| with {:ok, elixir, env} <- Expert.Port.project_executable(project, "elixir"), | ||
| {:ok, erl, _env} <- Expert.Port.project_executable(project, "erl") do | ||
| Logger.info("Using path: #{System.get_env("PATH")}", project: project) | ||
| Logger.info("Found elixir executable at #{elixir}", project: project) | ||
| Logger.info("Found erl executable at #{erl}", project: project) | ||
|
|
||
| with {:ok, key} <- build_key(project, elixir, env) do | ||
| {:ok, key, [elixir: elixir, env: env]} | ||
| end | ||
| else | ||
| {:error, name, message} -> | ||
| Logger.error(message, project: project) | ||
| Expert.terminate("Failed to find an #{name} executable, shutting down", 1) | ||
| {:error, message} | ||
| end | ||
| end | ||
|
|
||
| defp build_key(%Project{} = project, elixir, env) do | ||
| case toolchain_versions(project, elixir, env) do | ||
| {output, 0} -> | ||
| output = String.trim(output) | ||
|
|
||
| with {:ok, binary} <- Base.decode64(output), | ||
| {elixir_version, erts_version} <- :erlang.binary_to_term(binary) do | ||
| {:ok, {elixir_version, erts_version}} | ||
| else | ||
| _ -> {:error, "Failed to determine Elixir/OTP runtime for project"} | ||
| end | ||
|
doorgan marked this conversation as resolved.
|
||
|
|
||
| {output, status} -> | ||
| {:error, | ||
| "Failed to determine Elixir/OTP runtime for project: #{String.trim(output)} (status #{status})"} | ||
| end | ||
| end | ||
|
|
||
| defp toolchain_versions(%Project{} = project, elixir, env) do | ||
| cmd = | ||
| "{System.version(), to_string(:erlang.system_info(:version))} |> :erlang.term_to_binary() |> Base.encode64() |> IO.write()" | ||
|
|
||
| System.cmd(to_string(elixir), ["--eval", cmd], | ||
|
doorgan marked this conversation as resolved.
|
||
| env: env, | ||
| cd: Project.root_path(project), | ||
| stderr_to_stdout: true | ||
| ) | ||
| end | ||
|
|
||
| defp start_builder(project, build, key) do | ||
| DynamicSupervisor.start_child( | ||
| Expert.EngineBuild.DynamicSupervisor.name(), | ||
| {Builder, {project, build, self(), key}} | ||
| ) | ||
| end | ||
|
|
||
| defp pop_pending_by_key(state, key, pid) do | ||
| case state.pending do | ||
| %{^key => %{pid: ^pid} = pending} -> | ||
| pending_map = Map.delete(state.pending, key) | ||
| {:ok, pending, %State{state | pending: pending_map}} | ||
|
|
||
| %{} -> | ||
| :error | ||
| end | ||
| end | ||
|
|
||
| defp pop_pending_by_ref(state, ref, pid) do | ||
| case Enum.find(state.pending, fn {_key, pending} -> | ||
| pending.ref == ref and pending.pid == pid | ||
| end) do | ||
| {key, pending} -> | ||
| pending_map = Map.delete(state.pending, key) | ||
| {:ok, pending, %State{state | pending: pending_map}} | ||
|
|
||
| nil -> | ||
| :error | ||
| end | ||
| end | ||
|
|
||
| defp reply_all(waiters, result) do | ||
| Enum.each(waiters, &GenServer.reply(&1, result)) | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.