Skip to content

Commit

Permalink
Removed unneeded startup tasks (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
kieraneglin authored Mar 25, 2024
1 parent a03f69a commit 377bc61
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 72 deletions.
17 changes: 2 additions & 15 deletions lib/pinchflat/boot/data_backfill_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ defmodule Pinchflat.Boot.DataBackfillWorker do

alias __MODULE__
alias Pinchflat.Repo
alias Pinchflat.Media.MediaItem

@doc """
Cancels all pending backfill jobs. Useful for ensuring worker runs immediately
Expand All @@ -44,26 +43,14 @@ defmodule Pinchflat.Boot.DataBackfillWorker do
"""
def perform(%Oban.Job{}) do
Logger.info("Running data backfill worker")
backfill_shorts_data()
# Nothing to do for now - just reschedule
# Keeping in-place because we _will_ need it in the future

reschedule_backfill()

:ok
end

defp backfill_shorts_data do
query =
from(
m in MediaItem,
where: fragment("? like ?", m.original_url, "%/shorts/%"),
where: m.short_form_content == false
)

{count, _} = Repo.update_all(query, set: [short_form_content: true])

Logger.info("Backfill worker set short_form_content to true for #{count} media items.")
end

defp reschedule_backfill do
# Run hourly
next_run_in = 60 * 60
Expand Down
34 changes: 1 addition & 33 deletions lib/pinchflat/boot/pre_job_startup_tasks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ defmodule Pinchflat.Boot.PreJobStartupTasks do
reset_executing_jobs()
create_blank_cookie_file()
apply_default_settings()
rename_old_job_workers()

{:ok, state}
end
Expand All @@ -55,9 +54,7 @@ defmodule Pinchflat.Boot.PreJobStartupTasks do
base_dir = Application.get_env(:pinchflat, :extras_directory)
filepath = Path.join(base_dir, "cookies.txt")

if File.exists?(filepath) do
Logger.info("Cookies file exists")
else
if !File.exists?(filepath) do
Logger.info("Cookies does not exist - creating it")

FilesystemHelpers.write_p!(filepath, "")
Expand All @@ -68,33 +65,4 @@ defmodule Pinchflat.Boot.PreJobStartupTasks do
Settings.fetch!(:onboarding, true)
Settings.fetch!(:pro_enabled, false)
end

# As part of a large refactor, I ended up moving a bunch of workers around. This
# is a problem because the workers are stored in the database and the runner
# will try to run the OLD jobs. This is also why these tasks run before the job
# runner starts up.
#
# Can be removed after a few months (created: 2024-03-12)
defp rename_old_job_workers do
# [ [old_name, new_name], ...]
rename_map = [
["Pinchflat.Workers.MediaIndexingWorker", "Pinchflat.FastIndexing.MediaIndexingWorker"],
["Pinchflat.Workers.MediaDownloadWorker", "Pinchflat.Downloading.MediaDownloadWorker"],
["Pinchflat.Workers.FastIndexingWorker", "Pinchflat.FastIndexing.FastIndexingWorker"],
["Pinchflat.Workers.MediaCollectionIndexingWorker", "Pinchflat.SlowIndexing.MediaCollectionIndexingWorker"],
["Pinchflat.Workers.DataBackfillWorker", "Pinchflat.Boot.DataBackfillWorker"]
]

jobs_renamed =
Enum.reduce(rename_map, 0, fn [old_name, new_name], acc ->
{count, _} =
Oban.Job
|> where(worker: ^old_name)
|> Repo.update_all(set: [worker: new_name])

acc + count
end)

Logger.info("Renamed #{jobs_renamed} old job workers")
end
end
24 changes: 0 additions & 24 deletions test/pinchflat/boot/data_backfill_worker_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule Pinchflat.Boot.DataBackfillWorkerTest do
use Pinchflat.DataCase

import Pinchflat.MediaFixtures

alias Pinchflat.Boot.DataBackfillWorker
alias Pinchflat.JobFixtures.TestJobWorker

Expand Down Expand Up @@ -45,26 +43,4 @@ defmodule Pinchflat.Boot.DataBackfillWorkerTest do
assert_enqueued(worker: DataBackfillWorker, scheduled_at: now_plus(60, :minutes))
end
end

describe "perform/1 when testing backfill_shorts_data" do
test "sets short_form_content to true for media items with shorts in the URL" do
media_item = media_item_with_attachments(%{original_url: "https://example.com/shorts/123"})

refute media_item.short_form_content

perform_job(DataBackfillWorker, %{})

assert Repo.reload!(media_item).short_form_content
end

test "does not set short_form_content to true for media items without shorts in the URL" do
media_item = media_item_with_attachments(%{original_url: "https://example.com/longs/123"})

refute media_item.short_form_content

perform_job(DataBackfillWorker, %{})

refute Repo.reload!(media_item).short_form_content
end
end
end

0 comments on commit 377bc61

Please sign in to comment.