mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-11-21 15:41:00 +00:00
Remove need for timex
This commit is contained in:
parent
ffee2bf6f3
commit
e4dd585e39
4 changed files with 38 additions and 25 deletions
|
@ -366,15 +366,12 @@ defmodule LiveBeats.MediaLibrary do
|
|||
end
|
||||
end
|
||||
|
||||
def delete_expired_songs(count, interval) do
|
||||
#for substracting the interval of time when from_now/2 is invoked
|
||||
count = count * -1
|
||||
|
||||
def expire_songs_older_than(count, interval) do
|
||||
Ecto.Multi.new()
|
||||
|> Ecto.Multi.delete_all(
|
||||
:delete_expired_songs,
|
||||
from(s in Song,
|
||||
where: s.inserted_at < from_now(^count, ^interval),
|
||||
where: s.inserted_at < from_now(^(-count), ^interval),
|
||||
select: %{user_id: s.user_id, mp3_filepath: s.mp3_filepath}
|
||||
)
|
||||
)
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
defmodule LiveBeats.SongsCleaner do
|
||||
@moduledoc """
|
||||
Remove user songs that were added ... ago
|
||||
Expire user songs using a polling interval.
|
||||
"""
|
||||
use GenServer
|
||||
|
||||
alias LiveBeats.MediaLibrary
|
||||
use GenServer
|
||||
|
||||
@poll_interval :timer.minutes(60)
|
||||
|
||||
def start_link(opts) do
|
||||
GenServer.start_link(__MODULE__, opts)
|
||||
|
@ -12,24 +14,21 @@ defmodule LiveBeats.SongsCleaner do
|
|||
|
||||
@impl true
|
||||
def init(opts) do
|
||||
schedule_cleanup()
|
||||
|
||||
count = Keyword.fetch!(opts, :count)
|
||||
interval = Keyword.fetch!(opts, :interval)
|
||||
MediaLibrary.delete_expired_songs(count, interval)
|
||||
MediaLibrary.expire_songs_older_than(count, interval)
|
||||
|
||||
{:ok, %{count: count, interval: interval}}
|
||||
{:ok, schedule_cleanup(%{count: count, interval: interval})}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info(:remove_songs, %{count: count, interval: interval} = state) do
|
||||
MediaLibrary.delete_expired_songs(count, interval)
|
||||
schedule_cleanup()
|
||||
|
||||
{:noreply, state}
|
||||
MediaLibrary.expire_songs_older_than(count, interval)
|
||||
{:noreply, schedule_cleanup(state)}
|
||||
end
|
||||
|
||||
defp schedule_cleanup do
|
||||
Process.send_after(self(), :remove_songs, :timer.hours(3))
|
||||
defp schedule_cleanup(state) do
|
||||
Process.send_after(self(), :remove_songs, @poll_interval)
|
||||
state
|
||||
end
|
||||
end
|
||||
|
|
1
mix.exs
1
mix.exs
|
@ -53,7 +53,6 @@ defmodule LiveBeats.MixProject do
|
|||
{:heroicons, "~> 0.2.2"},
|
||||
{:castore, "~> 0.1.13"},
|
||||
{:tailwind, "~> 0.1"},
|
||||
{:timex, "~> 3.0", only: :test}
|
||||
]
|
||||
end
|
||||
|
||||
|
|
|
@ -9,7 +9,14 @@ defmodule LiveBeats.MediaLibraryTest do
|
|||
import LiveBeats.AccountsFixtures
|
||||
import LiveBeats.MediaLibraryFixtures
|
||||
|
||||
@invalid_attrs %{album_artist: nil, artist: nil, date_recorded: nil, date_released: nil, duration: nil, title: nil}
|
||||
@invalid_attrs %{
|
||||
album_artist: nil,
|
||||
artist: nil,
|
||||
date_recorded: nil,
|
||||
date_released: nil,
|
||||
duration: nil,
|
||||
title: nil
|
||||
}
|
||||
|
||||
test "list_profile_songs/1 returns all songs for a profile" do
|
||||
user = user_fixture()
|
||||
|
@ -25,7 +32,15 @@ defmodule LiveBeats.MediaLibraryTest do
|
|||
|
||||
test "update_song/2 with valid data updates the song" do
|
||||
song = song_fixture()
|
||||
update_attrs = %{album_artist: "some updated album_artist", artist: "some updated artist", date_recorded: ~N[2021-10-27 20:11:00], date_released: ~N[2021-10-27 20:11:00], duration: 43, title: "some updated title"}
|
||||
|
||||
update_attrs = %{
|
||||
album_artist: "some updated album_artist",
|
||||
artist: "some updated artist",
|
||||
date_recorded: ~N[2021-10-27 20:11:00],
|
||||
date_released: ~N[2021-10-27 20:11:00],
|
||||
duration: 43,
|
||||
title: "some updated title"
|
||||
}
|
||||
|
||||
assert {:ok, %Song{} = song} = MediaLibrary.update_song(song, update_attrs)
|
||||
assert song.album_artist == "some updated album_artist"
|
||||
|
@ -54,13 +69,14 @@ defmodule LiveBeats.MediaLibraryTest do
|
|||
assert %Ecto.Changeset{} = MediaLibrary.change_song(song)
|
||||
end
|
||||
|
||||
test "delete_expired_songs/2 deletes the song expired before the required interval" do
|
||||
require IEx
|
||||
test "expire_songs_older_than/2 deletes the song expired before the required interval" do
|
||||
user = user_fixture()
|
||||
today = Timex.now()
|
||||
today = DateTime.utc_now()
|
||||
|
||||
three_months_ago = add_n_months(today, -3)
|
||||
four_months_ago = add_n_months(today, -4)
|
||||
one_month_ago = add_n_months(today, 1)
|
||||
one_month_ago = add_n_months(today, -1)
|
||||
|
||||
expired_song_1 =
|
||||
song_fixture(user_id: user.id, title: "song1", inserted_at: four_months_ago)
|
||||
|
@ -70,7 +86,7 @@ defmodule LiveBeats.MediaLibraryTest do
|
|||
|
||||
active_song = song_fixture(user_id: user.id, title: "song3", inserted_at: one_month_ago)
|
||||
|
||||
MediaLibrary.delete_expired_songs(-2, "month")
|
||||
MediaLibrary.expire_songs_older_than(2, "month")
|
||||
|
||||
assert_raise Ecto.NoResultsError, fn -> MediaLibrary.get_song!(expired_song_1.id) end
|
||||
assert_raise Ecto.NoResultsError, fn -> MediaLibrary.get_song!(expired_song_2.id) end
|
||||
|
@ -79,8 +95,10 @@ defmodule LiveBeats.MediaLibraryTest do
|
|||
end
|
||||
|
||||
defp add_n_months(datetime, n) do
|
||||
seconds = 30 * (60 * 60 * 24) * n
|
||||
|
||||
datetime
|
||||
|> Timex.shift(months: n)
|
||||
|> DateTime.add(seconds, :second)
|
||||
|> DateTime.to_naive()
|
||||
|> NaiveDateTime.truncate(:second)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue