diff --git a/backend/config/config.exs b/backend/config/config.exs index 18eb2c0..e95c6df 100644 --- a/backend/config/config.exs +++ b/backend/config/config.exs @@ -20,13 +20,18 @@ config :backend, BackendWeb.Endpoint, config :backend, Backend.Repo, queue_target: 5000 +instances_config_path = + if System.get_env("MIX_ENV") == "prod", + do: "lib/backend-2.2.0/priv/elasticsearch/instances.json", + else: "instances.json" + config :backend, Backend.Elasticsearch.Cluster, url: "http://localhost:9200", api: Elasticsearch.API.HTTP, json_library: Jason, indexes: %{ instances: %{ - settings: "priv/elasticsearch/instances.json", + settings: instances_config_path, store: Backend.Elasticsearch.Store, sources: [Backend.Instance], bulk_page_size: 1000, diff --git a/backend/lib/backend/release.ex b/backend/lib/backend/release.ex index abbc747..203926f 100644 --- a/backend/lib/backend/release.ex +++ b/backend/lib/backend/release.ex @@ -1,12 +1,24 @@ defmodule Backend.Release do @app :backend + @start_apps [ + :crypto, + :ssl, + :postgrex, + :ecto, + :elasticsearch, + @app + ] - alias Elasticsearch.Index - alias Backend.Elasticsearch.Cluster + # Ecto repos to start, if any + @repos Application.get_env(:backend, :ecto_repos, []) + # Elasticsearch clusters to start + @clusters [Backend.Elasticsearch.Cluster] + # Elasticsearch indexes to build + @indexes [:instances] def run_all do migrate() - index() + build_elasticsearch_indexes() end def migrate do @@ -15,20 +27,32 @@ defmodule Backend.Release do end end - def index do - # TODO: this isn't the right way to handle this. - # See https://github.com/danielberkompas/elasticsearch-elixir/issues/76 - Application.ensure_all_started(@app) - IO.puts("Indexing...") - Index.hot_swap(Cluster, "instances") - IO.puts("Done indexing.") - :init.stop() - end - def rollback(repo, version) do {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version)) end + def build_elasticsearch_indexes() do + start_services() + IO.puts("Building indexes...") + Enum.each(@indexes, &Elasticsearch.Index.hot_swap(Backend.Elasticsearch.Cluster, &1)) + stop_services() + end + + # Ensure that all OTP apps, repos used by your Elasticsearch store, + # and your Elasticsearch Cluster(s) are started + defp start_services do + IO.puts("Starting dependencies...") + Enum.each(@start_apps, &Application.ensure_all_started/1) + IO.puts("Starting repos...") + Enum.each(@repos, & &1.start_link(pool_size: 1)) + IO.puts("Starting clusters...") + Enum.each(@clusters, & &1.start_link()) + end + + defp stop_services do + :init.stop() + end + defp repos do Application.load(@app) Application.fetch_env!(@app, :ecto_repos)