From 6372599493072600aea7bd32a147b2ee6f5dbd46 Mon Sep 17 00:00:00 2001 From: miffigriffi Date: Mon, 16 Sep 2019 01:13:20 +0200 Subject: [PATCH] Refactoring of Application module --- lib/mobilizon.ex | 76 +++++++++++++++++ lib/mobilizon/application.ex | 100 ----------------------- lib/{ => mobilizon_web}/mobilizon_web.ex | 0 lib/mobilizon_web/reverse_proxy.ex | 2 +- lib/service/federator.ex | 2 +- mix.exs | 2 +- 6 files changed, 79 insertions(+), 103 deletions(-) delete mode 100644 lib/mobilizon/application.ex rename lib/{ => mobilizon_web}/mobilizon_web.ex (100%) diff --git a/lib/mobilizon.ex b/lib/mobilizon.ex index 36e0d6c61..7461a267c 100644 --- a/lib/mobilizon.ex +++ b/lib/mobilizon.ex @@ -9,4 +9,80 @@ defmodule Mobilizon do Mobilizon relies on `Guardian` for auth and `Geo`/Postgis for geographical information. """ + + use Application + + import Cachex.Spec + + alias Mobilizon.Config + alias Mobilizon.Service.Export.{Feed, ICalendar} + + @name Mix.Project.config()[:name] + @version Mix.Project.config()[:version] + + @spec named_version :: String.t() + def named_version, do: "#{@name} #{@version}" + + @spec user_agent :: String.t(:w) + def user_agent do + info = "#{MobilizonWeb.Endpoint.url()} <#{Config.get([:instance, :email], "")}>" + + "#{named_version()}; #{info}" + end + + @spec start(:normal | {:takeover, node} | {:failover, node}, term) :: + {:ok, pid} | {:ok, pid, term} | {:error, term} + def start(_type, _args) do + children = [ + # supervisors + Mobilizon.Storage.Repo, + MobilizonWeb.Endpoint, + # workers + Guardian.DB.Token.SweeperServer, + Mobilizon.Service.Federator, + cachex_spec(:feed, 2500, 60, 60, &Feed.create_cache/1), + cachex_spec(:ics, 2500, 60, 60, &ICalendar.create_cache/1), + cachex_spec(:activity_pub, 2500, 3, 15) + ] + + opts = [strategy: :one_for_one, name: Mobilizon.Supervisor] + + Supervisor.start_link(children, opts) + end + + @spec config_change(keyword, keyword, [atom]) :: :ok + def config_change(changed, _new, removed) do + MobilizonWeb.Endpoint.config_change(changed, removed) + + :ok + end + + @spec cachex_spec(atom, integer, integer, integer, function | nil) :: Supervisor.child_spec() + defp cachex_spec(name, limit, default, interval, fallback \\ nil) do + %{ + id: :"cache_#{name}", + start: + {Cachex, :start_link, + [ + name, + Keyword.merge(cachex_options(limit, default, interval), fallback_options(fallback)) + ]} + } + end + + @spec cachex_options(integer, integer, integer) :: keyword + defp cachex_options(limit, default, interval) do + [ + limit: limit, + expiration: + expiration( + default: :timer.minutes(default), + interval: :timer.seconds(interval) + ) + ] + end + + @spec fallback_options(function | nil) :: keyword + defp fallback_options(nil), do: [] + defp fallback_options(fallback), do: [fallback: fallback(default: fallback)] end diff --git a/lib/mobilizon/application.ex b/lib/mobilizon/application.ex deleted file mode 100644 index 19f3d9718..000000000 --- a/lib/mobilizon/application.ex +++ /dev/null @@ -1,100 +0,0 @@ -defmodule Mobilizon.Application do - @moduledoc """ - The Mobilizon application - """ - - use Application - - import Cachex.Spec - - alias Mobilizon.Config - alias Mobilizon.Service.Export.{Feed, ICalendar} - - @name Mix.Project.config()[:name] - @version Mix.Project.config()[:version] - - # See https://hexdocs.pm/elixir/Application.html - # for more information on OTP Applications - def start(_type, _args) do - import Supervisor.Spec - - # Define workers and child supervisors to be supervised - children = [ - # Start the Ecto repository - supervisor(Mobilizon.Storage.Repo, []), - # Start the endpoint when the application starts - supervisor(MobilizonWeb.Endpoint, []), - # Start your own worker by calling: Mobilizon.Worker.start_link(arg1, arg2, arg3) - # worker(Mobilizon.Worker, [arg1, arg2, arg3]), - worker( - Cachex, - [ - :feed, - [ - limit: 2500, - expiration: - expiration( - default: :timer.minutes(60), - interval: :timer.seconds(60) - ), - fallback: fallback(default: &Feed.create_cache/1) - ] - ], - id: :cache_feed - ), - worker( - Cachex, - [ - :ics, - [ - limit: 2500, - expiration: - expiration( - default: :timer.minutes(60), - interval: :timer.seconds(60) - ), - fallback: fallback(default: &ICalendar.create_cache/1) - ] - ], - id: :cache_ics - ), - worker( - Cachex, - [ - :activity_pub, - [ - limit: 2500, - expiration: - expiration( - default: :timer.minutes(3), - interval: :timer.seconds(15) - ) - ] - ], - id: :cache_activity_pub - ), - worker(Guardian.DB.Token.SweeperServer, []), - worker(Mobilizon.Service.Federator, []) - ] - - # See https://hexdocs.pm/elixir/Supervisor.html - # for other strategies and supported options - opts = [strategy: :one_for_one, name: Mobilizon.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 - MobilizonWeb.Endpoint.config_change(changed, removed) - :ok - end - - def named_version, do: @name <> " " <> @version - - def user_agent do - info = "#{MobilizonWeb.Endpoint.url()} <#{Config.get([:instance, :email], "")}>" - - named_version() <> "; " <> info - end -end diff --git a/lib/mobilizon_web.ex b/lib/mobilizon_web/mobilizon_web.ex similarity index 100% rename from lib/mobilizon_web.ex rename to lib/mobilizon_web/mobilizon_web.ex diff --git a/lib/mobilizon_web/reverse_proxy.ex b/lib/mobilizon_web/reverse_proxy.ex index 98c240d04..30347097a 100644 --- a/lib/mobilizon_web/reverse_proxy.ex +++ b/lib/mobilizon_web/reverse_proxy.ex @@ -260,7 +260,7 @@ defmodule MobilizonWeb.ReverseProxy do headers, "user-agent", 0, - {"user-agent", Mobilizon.Application.user_agent()} + {"user-agent", Mobilizon.user_agent()} ) else headers diff --git a/lib/service/federator.ex b/lib/service/federator.ex index 4d8b99cd0..041e87fcb 100644 --- a/lib/service/federator.ex +++ b/lib/service/federator.ex @@ -23,7 +23,7 @@ defmodule Mobilizon.Service.Federator do {:ok, args} end - def start_link do + def start_link(_) do spawn(fn -> # 1 minute Process.sleep(1000 * 60) diff --git a/mix.exs b/mix.exs index 9386944c1..34fd71674 100644 --- a/mix.exs +++ b/mix.exs @@ -36,7 +36,7 @@ defmodule Mobilizon.Mixfile do # Type `mix help compile.app` for more information. def application do [ - mod: {Mobilizon.Application, []}, + mod: {Mobilizon, []}, extra_applications: [:logger, :runtime_tools, :guardian, :bamboo, :geolix, :crypto, :cachex] ] end