-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enhancement] Auto-update yt-dlp (#589)
* Added a command for updating yt-dlp * Added a yt-dlp update worker to run daily * Added a new file that runs post-boot when the app is ready to serve requests; put yt-dlp updater in there * Updated config to expose the current env globally; updated startup tasks to not run in test env * Removes unneeded test code
- Loading branch information
1 parent
62214b8
commit 6ead291
Showing
14 changed files
with
202 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |