Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b62d5c201b | |||
| 6ead29182d | |||
| 62214b80a6 | |||
| 704d29dc7e | |||
| 3dd20141e0 |
@@ -148,6 +148,7 @@ If you change this setting and it works well for you, please leave a comment on
|
||||
| --------------------------- | --------- | ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `TZ` | No | `UTC` | Must follow IANA TZ format |
|
||||
| `LOG_LEVEL` | No | `debug` | Can be set to `info` but `debug` is strongly recommended |
|
||||
| `UMASK` | No | `022` | Unraid users may want to set this to `000` |
|
||||
| `BASIC_AUTH_USERNAME` | No | | See [authentication docs](https://github.com/kieraneglin/pinchflat/wiki/Username-and-Password) |
|
||||
| `BASIC_AUTH_PASSWORD` | No | | See [authentication docs](https://github.com/kieraneglin/pinchflat/wiki/Username-and-Password) |
|
||||
| `EXPOSE_FEED_ENDPOINTS` | No | `false` | See [RSS feed docs](https://github.com/kieraneglin/pinchflat/wiki/Podcast-RSS-Feeds) |
|
||||
|
||||
+2
-10
@@ -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"),
|
||||
@@ -49,16 +50,7 @@ config :pinchflat, PinchflatWeb.Endpoint,
|
||||
|
||||
config :pinchflat, Oban,
|
||||
engine: Oban.Engines.Lite,
|
||||
repo: Pinchflat.Repo,
|
||||
# Keep old jobs for 30 days for display in the UI
|
||||
plugins: [
|
||||
{Oban.Plugins.Pruner, max_age: 30 * 24 * 60 * 60},
|
||||
{Oban.Plugins.Cron,
|
||||
crontab: [
|
||||
{"0 1 * * *", Pinchflat.Downloading.MediaRetentionWorker},
|
||||
{"0 2 * * *", Pinchflat.Downloading.MediaQualityUpgradeWorker}
|
||||
]}
|
||||
]
|
||||
repo: Pinchflat.Repo
|
||||
|
||||
# Configures the mailer
|
||||
#
|
||||
|
||||
+16
-1
@@ -43,15 +43,30 @@ config :pinchflat, Pinchflat.Repo,
|
||||
# Some users may want to increase the number of workers that use yt-dlp to improve speeds
|
||||
# Others may want to decrease the number of these workers to lessen the chance of an IP ban
|
||||
{yt_dlp_worker_count, _} = Integer.parse(System.get_env("YT_DLP_WORKER_CONCURRENCY", "2"))
|
||||
# Used to set the cron for the yt-dlp update worker. The reason for this is
|
||||
# to avoid all instances of PF updating yt-dlp at the same time, which 1)
|
||||
# could result in rate limiting and 2) gives me time to react if an update
|
||||
# breaks something
|
||||
%{hour: current_hour, minute: current_minute} = DateTime.utc_now()
|
||||
|
||||
config :pinchflat, Oban,
|
||||
queues: [
|
||||
default: 10,
|
||||
fast_indexing: 6,
|
||||
fast_indexing: yt_dlp_worker_count,
|
||||
media_collection_indexing: yt_dlp_worker_count,
|
||||
media_fetching: yt_dlp_worker_count,
|
||||
remote_metadata: yt_dlp_worker_count,
|
||||
local_data: 8
|
||||
],
|
||||
plugins: [
|
||||
# Keep old jobs for 30 days for display in the UI
|
||||
{Oban.Plugins.Pruner, max_age: 30 * 24 * 60 * 60},
|
||||
{Oban.Plugins.Cron,
|
||||
crontab: [
|
||||
{"#{current_minute} #{current_hour} * * *", Pinchflat.YtDlp.UpdateWorker},
|
||||
{"0 1 * * *", Pinchflat.Downloading.MediaRetentionWorker},
|
||||
{"0 2 * * *", Pinchflat.Downloading.MediaQualityUpgradeWorker}
|
||||
]}
|
||||
]
|
||||
|
||||
if config_env() == :prod do
|
||||
|
||||
@@ -7,7 +7,7 @@ ARG DEBIAN_VERSION=bookworm-20240612-slim
|
||||
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
|
||||
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"
|
||||
|
||||
FROM ${BUILDER_IMAGE} as builder
|
||||
FROM ${BUILDER_IMAGE} AS builder
|
||||
|
||||
ARG TARGETPLATFORM
|
||||
RUN echo "Building for ${TARGETPLATFORM:?}"
|
||||
@@ -110,9 +110,9 @@ RUN apt-get update -y && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# More locale setup
|
||||
ENV LANG en_US.UTF-8
|
||||
ENV LANGUAGE en_US:en
|
||||
ENV LC_ALL en_US.UTF-8
|
||||
ENV LANG=en_US.UTF-8
|
||||
ENV LANGUAGE=en_US:en
|
||||
ENV LC_ALL=en_US.UTF-8
|
||||
|
||||
WORKDIR "/app"
|
||||
|
||||
@@ -124,6 +124,7 @@ RUN mkdir -p /config /downloads /etc/elixir_tzdata_data /etc/yt-dlp/plugins && \
|
||||
ENV MIX_ENV="prod"
|
||||
ENV PORT=${PORT}
|
||||
ENV RUN_CONTEXT="selfhosted"
|
||||
ENV UMASK=022
|
||||
EXPOSE ${PORT}
|
||||
|
||||
# Only copy the final release from the build stage
|
||||
|
||||
@@ -9,8 +9,12 @@ defmodule Pinchflat.Application do
|
||||
@impl true
|
||||
def start(_type, _args) do
|
||||
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,
|
||||
PinchflatWeb.Telemetry,
|
||||
Pinchflat.Repo,
|
||||
@@ -24,17 +28,11 @@ defmodule Pinchflat.Application do
|
||||
{Finch, name: Pinchflat.Finch},
|
||||
# Start a worker by calling: Pinchflat.Worker.start_link(arg)
|
||||
# {Pinchflat.Worker, arg},
|
||||
# Start to serve requests, typically the last entry
|
||||
PinchflatWeb.Endpoint
|
||||
# Start to serve requests, typically the last entry (except for the post-boot tasks)
|
||||
PinchflatWeb.Endpoint,
|
||||
Pinchflat.Boot.PostBootStartupTasks
|
||||
]
|
||||
|
||||
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)
|
||||
|> Supervisor.start_link(strategy: :one_for_one, name: Pinchflat.Supervisor)
|
||||
end
|
||||
|
||||
# Tell Phoenix to update the endpoint configuration
|
||||
|
||||
@@ -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
|
||||
@@ -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!
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -27,13 +27,15 @@ defmodule Pinchflat.Downloading.DownloadingHelpers do
|
||||
|
||||
Returns :ok
|
||||
"""
|
||||
def enqueue_pending_download_tasks(%Source{download_media: true} = source) do
|
||||
def enqueue_pending_download_tasks(source, job_opts \\ [])
|
||||
|
||||
def enqueue_pending_download_tasks(%Source{download_media: true} = source, job_opts) do
|
||||
source
|
||||
|> Media.list_pending_media_items_for()
|
||||
|> Enum.each(&MediaDownloadWorker.kickoff_with_task/1)
|
||||
|> Enum.each(&MediaDownloadWorker.kickoff_with_task(&1, %{}, job_opts))
|
||||
end
|
||||
|
||||
def enqueue_pending_download_tasks(%Source{download_media: false}) do
|
||||
def enqueue_pending_download_tasks(%Source{download_media: false}, _job_opts) do
|
||||
:ok
|
||||
end
|
||||
|
||||
@@ -55,13 +57,13 @@ defmodule Pinchflat.Downloading.DownloadingHelpers do
|
||||
|
||||
Returns {:ok, %Task{}} | {:error, :should_not_download} | {:error, any()}
|
||||
"""
|
||||
def kickoff_download_if_pending(%MediaItem{} = media_item) do
|
||||
def kickoff_download_if_pending(%MediaItem{} = media_item, job_opts \\ []) do
|
||||
media_item = Repo.preload(media_item, :source)
|
||||
|
||||
if media_item.source.download_media && Media.pending_download?(media_item) do
|
||||
Logger.info("Kicking off download for media item ##{media_item.id} (#{media_item.media_id})")
|
||||
|
||||
MediaDownloadWorker.kickoff_with_task(media_item)
|
||||
MediaDownloadWorker.kickoff_with_task(media_item, %{}, job_opts)
|
||||
else
|
||||
{:error, :should_not_download}
|
||||
end
|
||||
|
||||
@@ -3,6 +3,7 @@ defmodule Pinchflat.Downloading.MediaDownloadWorker do
|
||||
|
||||
use Oban.Worker,
|
||||
queue: :media_fetching,
|
||||
priority: 5,
|
||||
unique: [period: :infinity, states: [:available, :scheduled, :retryable, :executing]],
|
||||
tags: ["media_item", "media_fetching", "show_in_dashboard"]
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpers do
|
||||
Returns [%MediaItem{}] where each item is a new media item that was created _but not necessarily
|
||||
downloaded_.
|
||||
"""
|
||||
def kickoff_download_tasks_from_youtube_rss_feed(%Source{} = source) do
|
||||
def index_and_kickoff_downloads(%Source{} = source) do
|
||||
# The media_profile is needed to determine the quality options to _then_ determine a more
|
||||
# accurate predicted filepath
|
||||
source = Repo.preload(source, [:media_profile])
|
||||
@@ -53,6 +53,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpers do
|
||||
Enum.map(new_media_ids, fn media_id ->
|
||||
case create_media_item_from_media_id(source, media_id) do
|
||||
{:ok, media_item} ->
|
||||
DownloadingHelpers.kickoff_download_if_pending(media_item, priority: 0)
|
||||
media_item
|
||||
|
||||
err ->
|
||||
@@ -61,7 +62,9 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpers do
|
||||
end
|
||||
end)
|
||||
|
||||
DownloadingHelpers.enqueue_pending_download_tasks(source)
|
||||
# Pick up any stragglers. Intentionally has a lower priority than the per-media item
|
||||
# kickoff above
|
||||
DownloadingHelpers.enqueue_pending_download_tasks(source, priority: 1)
|
||||
|
||||
Enum.filter(maybe_new_media_items, & &1)
|
||||
end
|
||||
|
||||
@@ -38,8 +38,8 @@ defmodule Pinchflat.FastIndexing.FastIndexingWorker do
|
||||
|
||||
Order of operations:
|
||||
1. FastIndexingWorker (this module) periodically checks the YouTube RSS feed for new media.
|
||||
with `FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed`
|
||||
2. If the above `kickoff_download_tasks_from_youtube_rss_feed` finds new media items in the RSS feed,
|
||||
with `FastIndexingHelpers.index_and_kickoff_downloads`
|
||||
2. If the above `index_and_kickoff_downloads` finds new media items in the RSS feed,
|
||||
it indexes them with a yt-dlp call to create the media item records then kicks off downloading
|
||||
tasks (MediaDownloadWorker) for any new media items _that should be downloaded_.
|
||||
3. Once downloads are kicked off, this worker sends a notification to the apprise server if applicable
|
||||
@@ -67,7 +67,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingWorker do
|
||||
|
||||
new_media_items =
|
||||
source
|
||||
|> FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed()
|
||||
|> FastIndexingHelpers.index_and_kickoff_downloads()
|
||||
|> Enum.filter(&Media.pending_download?(&1))
|
||||
|
||||
if source.download_media do
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -39,7 +39,6 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do
|
||||
def kickoff_indexing_task(%Source{} = source, job_args \\ %{}, job_opts \\ []) do
|
||||
job_offset_seconds = if job_args[:force], do: 0, else: calculate_job_offset_seconds(source)
|
||||
|
||||
Tasks.delete_pending_tasks_for(source, "FastIndexingWorker")
|
||||
Tasks.delete_pending_tasks_for(source, "MediaCollectionIndexingWorker", include_executing: true)
|
||||
|
||||
MediaCollectionIndexingWorker.kickoff_with_task(source, job_args, job_opts ++ [schedule_in: job_offset_seconds])
|
||||
@@ -231,8 +230,9 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpers do
|
||||
# The download archive isn't useful for playlists (since those are ordered arbitrarily)
|
||||
# and we don't want to use it if the indexing was forced by the user. In other words,
|
||||
# only create an archive for channels that are being indexed as part of their regular
|
||||
# indexing schedule
|
||||
# indexing schedule. The first indexing pass should also not create an archive.
|
||||
defp build_download_archive_options(%Source{collection_type: :playlist}, _was_forced), do: []
|
||||
defp build_download_archive_options(%Source{last_indexed_at: nil}, _was_forced), do: []
|
||||
defp build_download_archive_options(_source, true), do: []
|
||||
|
||||
defp build_download_archive_options(source, _was_forced) do
|
||||
|
||||
@@ -300,6 +300,10 @@ defmodule Pinchflat.Sources do
|
||||
%{__meta__: %{state: :built}} ->
|
||||
SlowIndexingHelpers.kickoff_indexing_task(source)
|
||||
|
||||
if Ecto.Changeset.get_field(changeset, :fast_index) do
|
||||
FastIndexingHelpers.kickoff_indexing_task(source)
|
||||
end
|
||||
|
||||
# If the record has been persisted, only run indexing if the
|
||||
# indexing frequency has been changed and is now greater than 0
|
||||
%{__meta__: %{state: :loaded}} ->
|
||||
|
||||
@@ -76,6 +76,24 @@ defmodule Pinchflat.YtDlp.CommandRunner do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates yt-dlp to the latest version
|
||||
|
||||
Returns {:ok, binary()} | {:error, binary()}
|
||||
"""
|
||||
@impl YtDlpCommandRunner
|
||||
def update do
|
||||
command = backend_executable()
|
||||
|
||||
case CliUtils.wrap_cmd(command, ["--update"]) do
|
||||
{output, 0} ->
|
||||
{:ok, String.trim(output)}
|
||||
|
||||
{output, _} ->
|
||||
{:error, output}
|
||||
end
|
||||
end
|
||||
|
||||
defp generate_output_filepath(addl_opts) do
|
||||
case Keyword.get(addl_opts, :output_filepath) do
|
||||
nil -> FSUtils.generate_metadata_tmpfile(:json)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
defmodule Pinchflat.YtDlp.UpdateWorker do
|
||||
@moduledoc false
|
||||
|
||||
use Oban.Worker,
|
||||
queue: :local_data,
|
||||
tags: ["local_data"]
|
||||
|
||||
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 as well as on app boot.
|
||||
|
||||
Returns :ok
|
||||
"""
|
||||
@impl Oban.Worker
|
||||
def perform(%Oban.Job{}) do
|
||||
Logger.info("Updating yt-dlp")
|
||||
|
||||
yt_dlp_runner().update()
|
||||
|
||||
{:ok, yt_dlp_version} = yt_dlp_runner().version()
|
||||
Settings.set(yt_dlp_version: yt_dlp_version)
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
defp yt_dlp_runner do
|
||||
Application.get_env(:pinchflat, :yt_dlp_runner)
|
||||
end
|
||||
end
|
||||
@@ -9,4 +9,5 @@ defmodule Pinchflat.YtDlp.YtDlpCommandRunner do
|
||||
@callback run(binary(), atom(), keyword(), binary()) :: {:ok, binary()} | {:error, binary(), integer()}
|
||||
@callback run(binary(), atom(), keyword(), binary(), keyword()) :: {:ok, binary()} | {:error, binary(), integer()}
|
||||
@callback version() :: {:ok, binary()} | {:error, binary()}
|
||||
@callback update() :: {:ok, binary()} | {:error, binary()}
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -4,7 +4,7 @@ defmodule Pinchflat.MixProject do
|
||||
def project do
|
||||
[
|
||||
app: :pinchflat,
|
||||
version: "2025.1.17",
|
||||
version: "2025.1.27",
|
||||
elixir: "~> 1.17",
|
||||
elixirc_paths: elixirc_paths(Mix.env()),
|
||||
start_permanent: Mix.env() == :prod,
|
||||
|
||||
@@ -6,6 +6,9 @@ if [ $? -ne 0 ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Setting umask to ${UMASK}"
|
||||
umask ${UMASK}
|
||||
|
||||
/app/bin/migrate
|
||||
|
||||
cd -P -- "$(dirname -- "$0")"
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
defmodule Pinchflat.Boot.PostBootStartupTasksTest do
|
||||
use Pinchflat.DataCase
|
||||
|
||||
alias Pinchflat.YtDlp.UpdateWorker
|
||||
alias Pinchflat.Boot.PostBootStartupTasks
|
||||
|
||||
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
|
||||
@@ -10,7 +10,7 @@ defmodule Pinchflat.Downloading.DownloadingHelpersTest do
|
||||
alias Pinchflat.Downloading.MediaDownloadWorker
|
||||
|
||||
describe "enqueue_pending_download_tasks/1" do
|
||||
test "it enqueues a job for each pending media item" do
|
||||
test "enqueues a job for each pending media item" do
|
||||
source = source_fixture()
|
||||
media_item = media_item_fixture(source_id: source.id, media_filepath: nil)
|
||||
|
||||
@@ -19,7 +19,7 @@ defmodule Pinchflat.Downloading.DownloadingHelpersTest do
|
||||
assert_enqueued(worker: MediaDownloadWorker, args: %{"id" => media_item.id})
|
||||
end
|
||||
|
||||
test "it does not enqueue a job for media items with a filepath" do
|
||||
test "does not enqueue a job for media items with a filepath" do
|
||||
source = source_fixture()
|
||||
_media_item = media_item_fixture(source_id: source.id, media_filepath: "some/filepath.mp4")
|
||||
|
||||
@@ -28,7 +28,7 @@ defmodule Pinchflat.Downloading.DownloadingHelpersTest do
|
||||
refute_enqueued(worker: MediaDownloadWorker)
|
||||
end
|
||||
|
||||
test "it attaches a task to each enqueued job" do
|
||||
test "attaches a task to each enqueued job" do
|
||||
source = source_fixture()
|
||||
media_item = media_item_fixture(source_id: source.id, media_filepath: nil)
|
||||
|
||||
@@ -39,7 +39,7 @@ defmodule Pinchflat.Downloading.DownloadingHelpersTest do
|
||||
assert [_] = Tasks.list_tasks_for(media_item)
|
||||
end
|
||||
|
||||
test "it does not create a job if the source is set to not download" do
|
||||
test "does not create a job if the source is set to not download" do
|
||||
source = source_fixture(download_media: false)
|
||||
|
||||
assert :ok = DownloadingHelpers.enqueue_pending_download_tasks(source)
|
||||
@@ -47,17 +47,26 @@ defmodule Pinchflat.Downloading.DownloadingHelpersTest do
|
||||
refute_enqueued(worker: MediaDownloadWorker)
|
||||
end
|
||||
|
||||
test "it does not attach tasks if the source is set to not download" do
|
||||
test "does not attach tasks if the source is set to not download" do
|
||||
source = source_fixture(download_media: false)
|
||||
media_item = media_item_fixture(source_id: source.id, media_filepath: nil)
|
||||
|
||||
assert :ok = DownloadingHelpers.enqueue_pending_download_tasks(source)
|
||||
assert [] = Tasks.list_tasks_for(media_item)
|
||||
end
|
||||
|
||||
test "can pass job options" do
|
||||
source = source_fixture()
|
||||
media_item = media_item_fixture(source_id: source.id, media_filepath: nil)
|
||||
|
||||
assert :ok = DownloadingHelpers.enqueue_pending_download_tasks(source, priority: 1)
|
||||
|
||||
assert_enqueued(worker: MediaDownloadWorker, args: %{"id" => media_item.id}, priority: 1)
|
||||
end
|
||||
end
|
||||
|
||||
describe "dequeue_pending_download_tasks/1" do
|
||||
test "it deletes all pending tasks for a source's media items" do
|
||||
test "deletes all pending tasks for a source's media items" do
|
||||
source = source_fixture()
|
||||
media_item = media_item_fixture(source_id: source.id, media_filepath: nil)
|
||||
|
||||
@@ -109,6 +118,14 @@ defmodule Pinchflat.Downloading.DownloadingHelpersTest do
|
||||
|
||||
refute_enqueued(worker: MediaDownloadWorker)
|
||||
end
|
||||
|
||||
test "can pass job options" do
|
||||
media_item = media_item_fixture(media_filepath: nil)
|
||||
|
||||
assert {:ok, _} = DownloadingHelpers.kickoff_download_if_pending(media_item, priority: 1)
|
||||
|
||||
assert_enqueued(worker: MediaDownloadWorker, args: %{"id" => media_item.id}, priority: 1)
|
||||
end
|
||||
end
|
||||
|
||||
describe "kickoff_redownload_for_existing_media/1" do
|
||||
|
||||
@@ -46,13 +46,20 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
|
||||
assert_enqueued(worker: MediaDownloadWorker, args: %{"id" => media_item.id, "force" => true})
|
||||
end
|
||||
|
||||
test "can be called with additional job options", %{media_item: media_item} do
|
||||
job_opts = [max_attempts: 5]
|
||||
|
||||
assert {:ok, _} = MediaDownloadWorker.kickoff_with_task(media_item, %{}, job_opts)
|
||||
test "has a priority of 5 by default", %{media_item: media_item} do
|
||||
assert {:ok, _} = MediaDownloadWorker.kickoff_with_task(media_item)
|
||||
|
||||
[job] = all_enqueued(worker: MediaDownloadWorker, args: %{"id" => media_item.id})
|
||||
assert job.max_attempts == 5
|
||||
|
||||
assert job.priority == 5
|
||||
end
|
||||
|
||||
test "priority can be set", %{media_item: media_item} do
|
||||
assert {:ok, _} = MediaDownloadWorker.kickoff_with_task(media_item, %{}, priority: 0)
|
||||
|
||||
[job] = all_enqueued(worker: MediaDownloadWorker, args: %{"id" => media_item.id})
|
||||
|
||||
assert job.priority == 0
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -38,36 +38,48 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "kickoff_download_tasks_from_youtube_rss_feed/1" do
|
||||
test "enqueues a new worker for each new media_id in the source's RSS feed", %{source: source} do
|
||||
describe "index_and_kickoff_downloads/1" do
|
||||
test "enqueues a worker for each new media_id in the source's RSS feed", %{source: source} do
|
||||
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
|
||||
|
||||
assert [media_item] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [media_item] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
|
||||
assert [worker] = all_enqueued(worker: MediaDownloadWorker)
|
||||
assert worker.args["id"] == media_item.id
|
||||
assert worker.priority == 0
|
||||
end
|
||||
|
||||
test "does not enqueue a new worker for the source's media IDs we already know about", %{source: source} do
|
||||
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
|
||||
media_item_fixture(source_id: source.id, media_id: "test_1")
|
||||
|
||||
assert [] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
|
||||
refute_enqueued(worker: MediaDownloadWorker)
|
||||
end
|
||||
|
||||
test "kicks off a download task for all pending media but at a lower priority", %{source: source} do
|
||||
pending_item = media_item_fixture(source_id: source.id, media_filepath: nil)
|
||||
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
|
||||
assert [worker_1, _worker_2] = all_enqueued(worker: MediaDownloadWorker)
|
||||
assert worker_1.args["id"] == pending_item.id
|
||||
assert worker_1.priority == 1
|
||||
end
|
||||
|
||||
test "returns the found media items", %{source: source} do
|
||||
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "does not enqueue a download job if the source does not allow it" do
|
||||
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
|
||||
source = source_fixture(%{download_media: false})
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
|
||||
refute_enqueued(worker: MediaDownloadWorker)
|
||||
end
|
||||
@@ -75,7 +87,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
||||
test "creates a download task record", %{source: source} do
|
||||
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
|
||||
|
||||
assert [media_item] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [media_item] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
|
||||
assert [_] = Tasks.list_tasks_for(media_item, "MediaDownloadWorker")
|
||||
end
|
||||
@@ -89,7 +101,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
||||
{:ok, media_attributes_return_fixture()}
|
||||
end)
|
||||
|
||||
FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "sets use_cookies if the source uses cookies" do
|
||||
@@ -103,7 +115,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
||||
|
||||
source = source_fixture(%{use_cookies: true})
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "does not set use_cookies if the source does not use cookies" do
|
||||
@@ -117,7 +129,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
||||
|
||||
source = source_fixture(%{use_cookies: false})
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "does not enqueue a download job if the media item does not match the format rules" do
|
||||
@@ -142,7 +154,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
||||
{:ok, output}
|
||||
end)
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
|
||||
refute_enqueued(worker: MediaDownloadWorker)
|
||||
end
|
||||
@@ -154,7 +166,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
||||
{:ok, "{}"}
|
||||
end)
|
||||
|
||||
assert [] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "does not blow up if a media item causes a yt-dlp error", %{source: source} do
|
||||
@@ -164,11 +176,11 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
||||
{:error, "message", 1}
|
||||
end)
|
||||
|
||||
assert [] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
end
|
||||
|
||||
describe "kickoff_download_tasks_from_youtube_rss_feed/1 when testing backends" do
|
||||
describe "index_and_kickoff_downloads/1 when testing backends" do
|
||||
test "uses the YouTube API if it is enabled", %{source: source} do
|
||||
expect(HTTPClientMock, :get, fn url, _headers ->
|
||||
assert url =~ "https://youtube.googleapis.com/youtube/v3/playlistItems"
|
||||
@@ -178,7 +190,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
||||
|
||||
Settings.set(youtube_api_key: "test_key")
|
||||
|
||||
assert [] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "the YouTube API creates records as expected", %{source: source} do
|
||||
@@ -188,7 +200,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
||||
|
||||
Settings.set(youtube_api_key: "test_key")
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "RSS is used as a backup if the API fails", %{source: source} do
|
||||
@@ -197,7 +209,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
||||
|
||||
Settings.set(youtube_api_key: "test_key")
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
|
||||
test "RSS is used if the API is not enabled", %{source: source} do
|
||||
@@ -209,7 +221,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
|
||||
|
||||
Settings.set(youtube_api_key: nil)
|
||||
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.kickoff_download_tasks_from_youtube_rss_feed(source)
|
||||
assert [%MediaItem{}] = FastIndexingHelpers.index_and_kickoff_downloads(source)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -96,26 +96,6 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(task) end
|
||||
end
|
||||
|
||||
test "deletes any pending media tasks for the source" do
|
||||
source = source_fixture()
|
||||
{:ok, job} = Oban.insert(FastIndexingWorker.new(%{"id" => source.id}))
|
||||
task = task_fixture(source_id: source.id, job_id: job.id)
|
||||
|
||||
assert {:ok, _} = SlowIndexingHelpers.kickoff_indexing_task(source)
|
||||
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(task) end
|
||||
end
|
||||
|
||||
test "deletes any fast indexing tasks for the source" do
|
||||
source = source_fixture()
|
||||
{:ok, job} = Oban.insert(FastIndexingWorker.new(%{"id" => source.id}))
|
||||
task = task_fixture(source_id: source.id, job_id: job.id)
|
||||
|
||||
assert {:ok, _} = SlowIndexingHelpers.kickoff_indexing_task(source)
|
||||
|
||||
assert_raise Ecto.NoResultsError, fn -> Repo.reload!(task) end
|
||||
end
|
||||
|
||||
test "can be called with additional job arguments" do
|
||||
source = source_fixture(index_frequency_minutes: 1)
|
||||
job_args = %{"force" => true}
|
||||
@@ -475,7 +455,9 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
|
||||
end
|
||||
|
||||
describe "index_and_enqueue_download_for_media_items when testing the download archive" do
|
||||
test "a download archive is used if the source is a channel", %{source: source} do
|
||||
test "a download archive is used if the source is a channel that has been indexed before" do
|
||||
source = source_fixture(%{collection_type: :channel, last_indexed_at: now()})
|
||||
|
||||
expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, opts, _ot, _addl_opts ->
|
||||
assert :break_on_existing in opts
|
||||
assert Keyword.has_key?(opts, :download_archive)
|
||||
@@ -499,6 +481,19 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
|
||||
SlowIndexingHelpers.index_and_enqueue_download_for_media_items(source)
|
||||
end
|
||||
|
||||
test "a download archive is not used if the source has never been indexed before" do
|
||||
source = source_fixture(%{collection_type: :channel, last_indexed_at: nil})
|
||||
|
||||
expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, opts, _ot, _addl_opts ->
|
||||
refute :break_on_existing in opts
|
||||
refute Keyword.has_key?(opts, :download_archive)
|
||||
|
||||
{:ok, source_attributes_return_fixture()}
|
||||
end)
|
||||
|
||||
SlowIndexingHelpers.index_and_enqueue_download_for_media_items(source)
|
||||
end
|
||||
|
||||
test "a download archive is not used if the index has been forced to run" do
|
||||
source = source_fixture(%{collection_type: :channel})
|
||||
|
||||
@@ -512,7 +507,9 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
|
||||
SlowIndexingHelpers.index_and_enqueue_download_for_media_items(source, was_forced: true)
|
||||
end
|
||||
|
||||
test "the download archive is formatted correctly and contains the right video", %{source: source} do
|
||||
test "the download archive is formatted correctly and contains the right video" do
|
||||
source = source_fixture(%{collection_type: :channel, last_indexed_at: now()})
|
||||
|
||||
media_items =
|
||||
1..21
|
||||
|> Enum.map(fn n ->
|
||||
|
||||
@@ -294,6 +294,34 @@ defmodule Pinchflat.SourcesTest do
|
||||
assert_enqueued(worker: MediaCollectionIndexingWorker, args: %{"id" => source.id})
|
||||
end
|
||||
|
||||
test "creation will schedule a fast indexing job if the fast_index option is set" do
|
||||
expect(YtDlpRunnerMock, :run, &channel_mock/5)
|
||||
|
||||
valid_attrs = %{
|
||||
media_profile_id: media_profile_fixture().id,
|
||||
original_url: "https://www.youtube.com/channel/abc123",
|
||||
fast_index: true
|
||||
}
|
||||
|
||||
assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs)
|
||||
|
||||
assert_enqueued(worker: FastIndexingWorker, args: %{"id" => source.id})
|
||||
end
|
||||
|
||||
test "creation will not schedule a fast indexing job if the fast_index option is not set" do
|
||||
expect(YtDlpRunnerMock, :run, &channel_mock/5)
|
||||
|
||||
valid_attrs = %{
|
||||
media_profile_id: media_profile_fixture().id,
|
||||
original_url: "https://www.youtube.com/channel/abc123",
|
||||
fast_index: false
|
||||
}
|
||||
|
||||
assert {:ok, %Source{}} = Sources.create_source(valid_attrs)
|
||||
|
||||
refute_enqueued(worker: FastIndexingWorker)
|
||||
end
|
||||
|
||||
test "creation schedules an index test even if the index frequency is 0" do
|
||||
expect(YtDlpRunnerMock, :run, &channel_mock/5)
|
||||
|
||||
|
||||
@@ -154,6 +154,14 @@ defmodule Pinchflat.YtDlp.CommandRunnerTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "update/0" do
|
||||
test "adds the update arg" do
|
||||
assert {:ok, output} = Runner.update()
|
||||
|
||||
assert String.contains?(output, "--update")
|
||||
end
|
||||
end
|
||||
|
||||
defp wrap_executable(new_executable, fun) do
|
||||
Application.put_env(:pinchflat, :yt_dlp_executable, new_executable)
|
||||
fun.()
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
defmodule Pinchflat.YtDlp.UpdateWorkerTest do
|
||||
use Pinchflat.DataCase
|
||||
|
||||
alias Pinchflat.Settings
|
||||
alias Pinchflat.YtDlp.UpdateWorker
|
||||
|
||||
describe "perform/1" do
|
||||
test "calls the yt-dlp runner to update yt-dlp" do
|
||||
expect(YtDlpRunnerMock, :update, fn -> {:ok, ""} end)
|
||||
expect(YtDlpRunnerMock, :version, fn -> {:ok, ""} end)
|
||||
|
||||
perform_job(UpdateWorker, %{})
|
||||
end
|
||||
|
||||
test "saves the new version to the database" do
|
||||
expect(YtDlpRunnerMock, :update, fn -> {:ok, ""} end)
|
||||
expect(YtDlpRunnerMock, :version, fn -> {:ok, "1.2.3"} end)
|
||||
|
||||
perform_job(UpdateWorker, %{})
|
||||
|
||||
assert {:ok, "1.2.3"} = Settings.get(:yt_dlp_version)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user