Skip to content

Commit

Permalink
[#40] Simplify Index.hot_swap/2 calling signature
Browse files Browse the repository at this point in the history
This will make it much simpler to call from other scripts, such as a
Distillery ReleaseTasks script.
  • Loading branch information
danielberkompas committed Aug 31, 2018
1 parent d9ef2a4 commit 707d21b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
25 changes: 15 additions & 10 deletions lib/elasticsearch/indexing/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,34 @@ defmodule Elasticsearch.Index do
## Example
iex> file = "test/support/settings/posts.json"
...> store = Elasticsearch.Test.Store
...> Index.hot_swap(Cluster, "posts", %{settings: file, store: store, sources: [Post]})
iex> Index.hot_swap(Cluster, "posts")
:ok
"""
@spec hot_swap(Cluster.t(), alias :: String.t() | atom, %{
settings: Path.t(),
store: module,
sources: [any]
}) :: :ok | {:error, Elasticsearch.Exception.t()}
def hot_swap(cluster, alias, %{settings: settings_file} = index_config) do
@spec hot_swap(Cluster.t(), alias :: String.t() | atom) ::
:ok | {:error, Elasticsearch.Exception.t()}
def hot_swap(cluster, alias) do
alias = alias_to_atom(alias)
name = build_name(alias)
config = Config.get(cluster)
%{settings: settings_file} = index_config = config[:indexes][alias]

with :ok <- create_from_file(config, name, settings_file),
:ok <- Bulk.upload(config, name, index_config),
:ok <-
Bulk.upload(
config,
name,
Map.merge(index_config, Map.take(config, [:bulk_wait_interval, :bulk_page_size]))
),
:ok <- __MODULE__.alias(config, name, alias),
:ok <- clean_starting_with(config, alias, 2),
:ok <- refresh(config, name) do
:ok
end
end

defp alias_to_atom(atom) when is_atom(atom), do: atom
defp alias_to_atom(str) when is_binary(str), do: String.to_existing_atom(str)

@doc """
Returns all indexes which start with a given string.
Expand Down
16 changes: 10 additions & 6 deletions lib/mix/elasticsearch.build.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,24 @@ defmodule Mix.Tasks.Elasticsearch.Build do
Mix.Task.run("app.start", [])

{type, cluster, indexes, settings} = parse_args!(args)
config = Config.get(cluster)

config =
cluster
|> Config.get()
|> Map.merge(settings)

for alias <- indexes do
build(type, config, alias, settings)
build(type, config, alias)
end
end

defp build(:existing, config, alias, settings) do
defp build(:existing, config, alias) do
case Index.latest_starting_with(config, alias) do
{:ok, name} ->
IO.puts("Index already exists: #{name}")

{:error, :not_found} ->
build(:rebuild, config, alias, settings)
build(:rebuild, config, alias)

{:error, exception} ->
Mix.raise("""
Expand All @@ -73,8 +77,8 @@ defmodule Mix.Tasks.Elasticsearch.Build do
end
end

defp build(:rebuild, config, alias, settings) do
with :ok <- Index.hot_swap(config, alias, Map.merge(config.indexes[alias], settings)) do
defp build(:rebuild, config, alias) do
with :ok <- Index.hot_swap(config, alias) do
:ok
else
{:error, errors} when is_list(errors) ->
Expand Down

0 comments on commit 707d21b

Please # to comment.