diff --git a/config/config.exs b/config/config.exs index a25b014..f57e0cc 100644 --- a/config/config.exs +++ b/config/config.exs @@ -10,6 +10,7 @@ import Config config :pinchflat, ecto_repos: [Pinchflat.Repo], generators: [timestamp_type: :utc_datetime], + env: config_env(), # Specifying backend data here makes mocking and local testing SUPER easy yt_dlp_executable: System.find_executable("yt-dlp"), apprise_executable: System.find_executable("apprise"), diff --git a/lib/pinchflat/boot/post_job_startup_tasks.ex b/lib/pinchflat/boot/post_job_startup_tasks.ex index 5043a25..6eba701 100644 --- a/lib/pinchflat/boot/post_job_startup_tasks.ex +++ b/lib/pinchflat/boot/post_job_startup_tasks.ex @@ -1,7 +1,7 @@ defmodule Pinchflat.Boot.PostJobStartupTasks do @moduledoc """ This module is responsible for running startup tasks on app boot - AFTER the job runner has initiallized. + AFTER the job runner has initialized. It's a GenServer because that plays REALLY nicely with the existing Phoenix supervision tree. @@ -12,7 +12,7 @@ defmodule Pinchflat.Boot.PostJobStartupTasks do import Ecto.Query, warn: false def start_link(opts \\ []) do - GenServer.start_link(__MODULE__, %{}, opts) + GenServer.start_link(__MODULE__, %{env: Application.get_env(:pinchflat, :env)}, opts) end @doc """ @@ -25,6 +25,13 @@ defmodule Pinchflat.Boot.PostJobStartupTasks do 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 # Nothing at the moment! diff --git a/lib/pinchflat/boot/pre_job_startup_tasks.ex b/lib/pinchflat/boot/pre_job_startup_tasks.ex index 2843db9..5035e35 100644 --- a/lib/pinchflat/boot/pre_job_startup_tasks.ex +++ b/lib/pinchflat/boot/pre_job_startup_tasks.ex @@ -19,7 +19,7 @@ defmodule Pinchflat.Boot.PreJobStartupTasks do alias Pinchflat.Lifecycle.UserScripts.CommandRunner, as: UserScriptRunner def start_link(opts \\ []) do - GenServer.start_link(__MODULE__, %{}, opts) + GenServer.start_link(__MODULE__, %{env: Application.get_env(:pinchflat, :env)}, opts) end @doc """ @@ -32,6 +32,13 @@ defmodule Pinchflat.Boot.PreJobStartupTasks do 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 ensure_tmpfile_directory() reset_executing_jobs() diff --git a/lib/pinchflat/profiles/media_profile_deletion_worker.ex b/lib/pinchflat/profiles/media_profile_deletion_worker.ex index 529474d..230a085 100644 --- a/lib/pinchflat/profiles/media_profile_deletion_worker.ex +++ b/lib/pinchflat/profiles/media_profile_deletion_worker.ex @@ -14,7 +14,7 @@ defmodule Pinchflat.Profiles.MediaProfileDeletionWorker do Starts the profile deletion worker. Does not attach it to a task like `kickoff_with_task/2` since deletion also cancels all tasks for the profile - Returns {:ok, %Task{}} | {:error, %Ecto.Changeset{}} + Returns {:ok, %Oban.Job{}} | {:error, %Ecto.Changeset{}} """ def kickoff(profile, job_args \\ %{}, job_opts \\ []) do %{id: profile.id} diff --git a/lib/pinchflat/yt_dlp/update_worker.ex b/lib/pinchflat/yt_dlp/update_worker.ex index daa7aac..2d9b43f 100644 --- a/lib/pinchflat/yt_dlp/update_worker.ex +++ b/lib/pinchflat/yt_dlp/update_worker.ex @@ -7,12 +7,22 @@ defmodule Pinchflat.YtDlp.UpdateWorker do require Logger + alias __MODULE__ alias Pinchflat.Settings + @doc """ + Starts the yt-dlp update worker. Does not attach it to a task like `kickoff_with_task/2` + + Returns {:ok, %Oban.Job{}} | {:error, %Ecto.Changeset{}} + """ + def kickoff do + Oban.insert(UpdateWorker.new(%{})) + end + @doc """ Updates yt-dlp and saves the version to the settings. - This worker is scheduled to run via the Oban Cron plugin. + This worker is scheduled to run via the Oban Cron plugin as well as on app boot. Returns :ok """ diff --git a/lib/pinchflat_web/endpoint.ex b/lib/pinchflat_web/endpoint.ex index b93b36e..908699e 100644 --- a/lib/pinchflat_web/endpoint.ex +++ b/lib/pinchflat_web/endpoint.ex @@ -20,7 +20,7 @@ defmodule PinchflatWeb.Endpoint do plug Plug.Static, at: "/", from: :pinchflat, - gzip: Mix.env() == :prod, + gzip: Application.compile_env(:pinchflat, :env) == :prod, only: PinchflatWeb.static_paths() # Code reloading can be explicitly enabled under the