-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
defmodule CF.Jobs.DownloadCaptions do | ||
@behaviour CF.Jobs.Job | ||
|
||
require Logger | ||
import Ecto.Query | ||
import ScoutApm.Tracing | ||
|
||
alias DB.Repo | ||
alias DB.Schema.UserAction | ||
alias DB.Schema.UsersActionsReport | ||
|
||
alias CF.Jobs.ReportManager | ||
|
||
@name :download_captions | ||
@analyser_id UsersActionsReport.analyser_id(@name) | ||
|
||
# --- Client API --- | ||
|
||
def name, do: @name | ||
|
||
def start_link() do | ||
GenServer.start_link(__MODULE__, :ok, name: __MODULE__) | ||
end | ||
|
||
def init(args) do | ||
{:ok, args} | ||
end | ||
|
||
# 1 minute | ||
@timeout 60_000 | ||
def update() do | ||
GenServer.call(__MODULE__, :download_captions, @timeout) | ||
end | ||
|
||
# --- Server callbacks --- | ||
@transaction_opts [type: "background", name: "download_captions"] | ||
def handle_call(:download_captions, _from, _state) do | ||
get_videos() | ||
|> Enum.map(fn video -> | ||
Logger.info("Downloading captions for video #{video.id}") | ||
download_captions(video) | ||
end) | ||
|
||
{:reply, :ok, :ok} | ||
end | ||
|
||
# Get all videos that need new captions. We fetch new captions: | ||
# - For any videos that doesn't have any captions yet | ||
# - For videos whose captions haven't been updated in the last 30 days | ||
defp get_videos() do | ||
Repo.all( | ||
from v in DB.Schema.Video, | ||
where: v.captions_updated_at == nil or v.captions_updated_at < DateTime.utc_now() - 30, | ||
preload: [:channel] | ||
) | ||
end | ||
end |