Added a new file that runs post-boot when the app is ready to serve requests; put yt-dlp updater in there
This commit is contained in:
parent
8e4a223700
commit
15bb8d4190
3 changed files with 81 additions and 11 deletions
|
|
@ -9,8 +9,12 @@ defmodule Pinchflat.Application do
|
||||||
@impl true
|
@impl true
|
||||||
def start(_type, _args) do
|
def start(_type, _args) do
|
||||||
check_and_update_timezone()
|
check_and_update_timezone()
|
||||||
|
attach_oban_telemetry()
|
||||||
|
Logger.add_handlers(:pinchflat)
|
||||||
|
|
||||||
children = [
|
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||||
|
# for other strategies and supported options
|
||||||
|
[
|
||||||
Pinchflat.PromEx,
|
Pinchflat.PromEx,
|
||||||
PinchflatWeb.Telemetry,
|
PinchflatWeb.Telemetry,
|
||||||
Pinchflat.Repo,
|
Pinchflat.Repo,
|
||||||
|
|
@ -24,17 +28,11 @@ defmodule Pinchflat.Application do
|
||||||
{Finch, name: Pinchflat.Finch},
|
{Finch, name: Pinchflat.Finch},
|
||||||
# Start a worker by calling: Pinchflat.Worker.start_link(arg)
|
# Start a worker by calling: Pinchflat.Worker.start_link(arg)
|
||||||
# {Pinchflat.Worker, arg},
|
# {Pinchflat.Worker, arg},
|
||||||
# Start to serve requests, typically the last entry
|
# Start to serve requests, typically the last entry (except for the post-boot tasks)
|
||||||
PinchflatWeb.Endpoint
|
PinchflatWeb.Endpoint,
|
||||||
|
Pinchflat.Boot.PostBootStartupTasks
|
||||||
]
|
]
|
||||||
|
|> Supervisor.start_link(strategy: :one_for_one, name: Pinchflat.Supervisor)
|
||||||
attach_oban_telemetry()
|
|
||||||
Logger.add_handlers(:pinchflat)
|
|
||||||
|
|
||||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
|
||||||
# for other strategies and supported options
|
|
||||||
opts = [strategy: :one_for_one, name: Pinchflat.Supervisor]
|
|
||||||
Supervisor.start_link(children, opts)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Tell Phoenix to update the endpoint configuration
|
# Tell Phoenix to update the endpoint configuration
|
||||||
|
|
|
||||||
46
lib/pinchflat/boot/post_boot_startup_tasks.ex
Normal file
46
lib/pinchflat/boot/post_boot_startup_tasks.ex
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
defmodule Pinchflat.Boot.PostBootStartupTasks do
|
||||||
|
@moduledoc """
|
||||||
|
This module is responsible for running startup tasks on app boot
|
||||||
|
AFTER all other boot steps have taken place and the app is ready to serve requests.
|
||||||
|
|
||||||
|
It's a GenServer because that plays REALLY nicely with the existing
|
||||||
|
Phoenix supervision tree.
|
||||||
|
"""
|
||||||
|
|
||||||
|
alias Pinchflat.YtDlp.UpdateWorker, as: YtDlpUpdateWorker
|
||||||
|
|
||||||
|
# restart: :temporary means that this process will never be restarted (ie: will run once and then die)
|
||||||
|
use GenServer, restart: :temporary
|
||||||
|
import Ecto.Query, warn: false
|
||||||
|
|
||||||
|
def start_link(opts \\ []) do
|
||||||
|
GenServer.start_link(__MODULE__, %{env: Application.get_env(:pinchflat, :env)}, opts)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Runs post-boot application startup tasks.
|
||||||
|
|
||||||
|
Any code defined here will run every time the application starts. You must
|
||||||
|
make sure that the code is idempotent and safe to run multiple times.
|
||||||
|
|
||||||
|
This is a good place to set up default settings, create initial records, stuff like that.
|
||||||
|
Should be fast - anything with the potential to be slow should be kicked off as a job instead.
|
||||||
|
"""
|
||||||
|
@impl true
|
||||||
|
def init(%{env: :test} = state) do
|
||||||
|
# Do nothing _as part of the app bootup process_.
|
||||||
|
# Since bootup calls `start_link` and that's where the `env` state is injected,
|
||||||
|
# you can still call `.init()` manually to run these tasks for testing purposes
|
||||||
|
{:ok, state}
|
||||||
|
end
|
||||||
|
|
||||||
|
def init(state) do
|
||||||
|
update_yt_dlp()
|
||||||
|
|
||||||
|
{:ok, state}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp update_yt_dlp do
|
||||||
|
YtDlpUpdateWorker.kickoff()
|
||||||
|
end
|
||||||
|
end
|
||||||
26
test/pinchflat/boot/post_boot_startup_tasks_test.exs
Normal file
26
test/pinchflat/boot/post_boot_startup_tasks_test.exs
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
defmodule Pinchflat.Boot.PostBootStartupTasksTest do
|
||||||
|
use Pinchflat.DataCase
|
||||||
|
|
||||||
|
# import Pinchflat.JobFixtures
|
||||||
|
|
||||||
|
# alias Pinchflat.Settings
|
||||||
|
alias Pinchflat.YtDlp.UpdateWorker
|
||||||
|
alias Pinchflat.Boot.PostBootStartupTasks
|
||||||
|
|
||||||
|
setup do
|
||||||
|
stub(YtDlpRunnerMock, :update, fn -> {:ok, "1"} end)
|
||||||
|
stub(YtDlpRunnerMock, :version, fn -> {:ok, "1"} end)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "update_yt_dlp" do
|
||||||
|
test "enqueues an update job" do
|
||||||
|
assert [] = all_enqueued(worker: UpdateWorker)
|
||||||
|
|
||||||
|
PostBootStartupTasks.init(%{})
|
||||||
|
|
||||||
|
assert [%Oban.Job{}] = all_enqueued(worker: UpdateWorker)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue