fediverse.space/backend/lib/backend/application.ex

78 wiersze
2.3 KiB
Elixir
Czysty Zwykły widok Historia

2019-07-14 11:47:06 +00:00
defmodule Backend.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
import Backend.Util
def start(_type, _args) do
crawl_worker_count = get_config(:crawl_workers)
children = [
# Start the Ecto repository
Backend.Repo,
# Start the endpoint when the application starts
BackendWeb.Endpoint,
# Crawler children
2019-08-21 12:30:47 +00:00
:hackney_pool.child_spec(:crawler, timeout: 15_000, max_connections: crawl_worker_count),
2019-07-27 10:13:42 +00:00
Supervisor.child_spec(
{Task,
fn ->
Honeydew.start_queue(:crawl_queue, failure_mode: Honeydew.FailureMode.Abandon)
Honeydew.start_workers(:crawl_queue, Backend.Crawler, num: crawl_worker_count)
end},
id: :start_honeydew
),
Supervisor.child_spec({Task, fn -> HTTPoison.start() end}, id: :start_httpoison),
2019-07-26 22:30:11 +00:00
Backend.Scheduler,
2019-08-21 15:47:45 +00:00
Backend.Elasticsearch.Cluster,
Graph.Cache
2019-07-14 11:47:06 +00:00
]
children =
case Enum.member?(["true", 1, "1"], System.get_env("SKIP_CRAWL")) do
true -> children
false -> children ++ [Backend.Crawler.StaleInstanceManager]
end
2019-07-27 10:13:42 +00:00
add_appsignal_probes()
2019-07-14 11:47:06 +00:00
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Backend.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
BackendWeb.Endpoint.config_change(changed, removed)
:ok
end
2019-07-27 10:13:42 +00:00
defp add_appsignal_probes do
Appsignal.Probes.register(:crawler, fn ->
%{
queue: %{
mnesia: mnesia
}
} = Honeydew.status(:crawl_queue)
2019-07-27 13:22:54 +00:00
# How much memory the mnesia queue in using
2019-07-27 10:13:42 +00:00
memory = mnesia |> Map.get(:"honeydew_:crawl_queue") |> Keyword.get(:memory)
Appsignal.set_gauge("mnesia_memory", memory)
2019-07-27 13:22:54 +00:00
# How many jobs are pending in the queue
queue_length =
Honeydew.filter(
:crawl_queue,
&match?(%Honeydew.Job{completed_at: nil, task: {:run, _}}, &1)
)
|> Enum.count()
Appsignal.set_gauge("queue_length", queue_length)
2019-07-27 10:13:42 +00:00
end)
end
2019-07-14 11:47:06 +00:00
end