-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Needed to help with ai insight sideload needs
- Loading branch information
1 parent
7ab2f00
commit cd61065
Showing
24 changed files
with
2,131 additions
and
1,964 deletions.
There are no files selected for viewing
This file contains 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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,62 @@ | ||
defmodule Console.Deployments.Local.Cache do | ||
defstruct [:dir, cache: %{}] | ||
|
||
defmodule Line do | ||
@expiry [minutes: -10] | ||
defstruct [:file, :digest, :touched] | ||
|
||
def new(file, digest) do | ||
%__MODULE__{ | ||
file: file, | ||
digest: digest, | ||
touched: Timex.now() | ||
} | ||
end | ||
|
||
def touch(%__MODULE__{} = mod), do: %{mod | touched: Timex.now()} | ||
|
||
def expire(%__MODULE__{file: f}), do: File.rm(f) | ||
|
||
def expired?(%__MODULE__{touched: touched}) do | ||
Timex.now() | ||
|> Timex.shift(@expiry) | ||
|> Timex.after?(touched) | ||
end | ||
end | ||
|
||
def new() do | ||
{:ok, dir} = Briefly.create(directory: true) | ||
%__MODULE__{dir: dir, cache: %{}} | ||
end | ||
|
||
def fetch(%__MODULE__{cache: lines} = cache, digest, reader) when is_function(reader, 0) do | ||
case lines[digest] do | ||
%Line{} = l -> {:ok, l, put_in(cache.cache[digest], Line.touch(l))} | ||
nil -> write(cache, digest, reader) | ||
end | ||
end | ||
|
||
def write(%__MODULE__{} = cache, digest, reader) when is_function(reader, 0) do | ||
path = Path.join(cache.dir, "#{digest}.tgz") | ||
with %File.Stream{} <- safe_copy(path, reader), | ||
line <- Line.new(path, digest), | ||
do: {:ok, line, put_in(cache.cache[digest], line)} | ||
end | ||
|
||
def sweep(%__MODULE__{cache: lines} = cache) do | ||
{keep, expire} = Enum.split_with(lines, fn {_, l} -> !Line.expired?(l) end) | ||
Enum.each(expire, &Line.expire/1) | ||
%{cache | cache: Map.new(keep)} | ||
end | ||
|
||
defp safe_copy(path, reader) when is_function(reader, 0) do | ||
with {:ok, f} <- reader.() do | ||
try do | ||
IO.binstream(f, 1024) | ||
|> Enum.into(File.stream!(path)) | ||
after | ||
File.close(f) | ||
end | ||
end | ||
end | ||
end |
This file contains 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,27 @@ | ||
defmodule Console.Deployments.Local.Server do | ||
use GenServer | ||
alias Console.Deployments.Local.Cache | ||
|
||
@timeout :timer.seconds(60) | ||
|
||
def start_link(opts \\ []) do | ||
GenServer.start(__MODULE__, opts, name: __MODULE__) | ||
end | ||
|
||
def init(_) do | ||
:timer.send_interval(:timer.minutes(5), :sweep) | ||
{:ok, Cache.new()} | ||
end | ||
|
||
def fetch(digest, reader) when is_function(reader, 0), | ||
do: GenServer.call(__MODULE__, {:fetch, digest, reader}, @timeout) | ||
|
||
def handle_call({:fetch, digest, reader}, _, cache) when is_function(reader, 0) do | ||
case Cache.fetch(cache, digest, reader) do | ||
{:ok, line, cache} -> {:reply, {:ok, line.file}, cache} | ||
err -> {:reply, err, cache} | ||
end | ||
end | ||
|
||
def handle_info(:sweep, cache), do: {:noreply, Cache.sweep(cache)} | ||
end |
This file contains 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 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 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 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.