Skip to content

Commit

Permalink
oban: process audio in oban job (rauversion#36)
Browse files Browse the repository at this point in the history
* oban: process audio in oban job

* update active job

* track state

* test oban

* notices on show and tracks for pending state
  • Loading branch information
michelson authored Jul 22, 2022
1 parent 791e8f5 commit 2dc4b34
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 31 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,23 @@ Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
- [x] Paginate profile tracks /:username
- [x] Paginate /tracks
- [x] Centralized player, fixed (bottom) across navigation
- [x] Make GithubActions work!
- [x] Refactor audio processing, add proceesing queue for the after upload
- [x] Telemetry for dashboard/ metrics / insights
- [ ] A WYSIWYG editor for Markdown for Track/Playlist description
- [x] fallback to a formatted plain text with auto link
- [ ] Playlists
- [x] add to playlist select playlist / modal first tab
- [ ] sort songs on playlist
- [ ] like button playlist/show
- [ ] share button playlist/show
- [x] playlist player
- [ ] Fix specs on tests/rauversion-web
- [x] Make GithubActions work!
- [ ] Listening history
- [ ] Give feedback on upload preprosessing
- [ ] Refactor audio processing, add proceesing queue for the after upload
- [ ] A logo for rauversion
- [ ] Albums
- [ ] A WYSIWYG editor for Markdown for Track/Playlist description
- [ ] Telemetry for dashboard




Expand Down
13 changes: 13 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ config :phoenix_meta_tags,
# image: "https://phoenix.meta.tags.default/logo.png",
"og:text": "Rauversion"

# config :rauversion, Oban,
# repo: Rauversion.Repo,
# plugins: [Oban.Plugins.Pruner],
# queues: [default: 10]

config :active_job, Oban,
# testing: :inline,
repo: Rauversion.Repo,
notifier: Oban.Notifiers.PG,
peer: Oban.Peers.Global,
plugins: [Oban.Plugins.Pruner],
queues: [default: 10]

# fb: %{
# name: "facebook",
# size: %{
Expand Down
10 changes: 10 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ config :active_storage, :service, :local
config :active_storage, :secret_key_base, "xxxxxxxxxxx"
config :active_job, repo: Rauversion.Repo
config :active_storage, repo: Rauversion.Repo

# config :rauversion, Oban, testing: :inline

config :active_job, Oban,
testing: :inline,
repo: Rauversion.Repo,
notifier: Oban.Notifiers.PG,
peer: Oban.Peers.Global,
plugins: [Oban.Plugins.Pruner],
queues: [default: 10]
8 changes: 8 additions & 0 deletions lib/rauversion/tracks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ defmodule Rauversion.Tracks do
]
end

def with_processed(query) do
query |> where([t], t.state == "processed")
end

def list_tracks_by_ids(ids) do
list_public_tracks() |> where([p], p.id in ^ids)
end
Expand Down Expand Up @@ -254,6 +258,10 @@ defmodule Rauversion.Tracks do
end
end

def is_processed?(track) do
track.state == "processed"
end

def blob_duration_metadata(blob) do
metadata = ActiveStorage.Blob.metadata(blob)

Expand Down
69 changes: 50 additions & 19 deletions lib/rauversion/tracks/track.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ defmodule Rauversion.Tracks.Track do
schema "tracks" do
field :caption, :string
field :description, :string
field :state, :string, default: "pending"

# field :metadata, :map, default: %{}
embeds_one :metadata, Rauversion.Tracks.TrackMetadata, on_replace: :delete

Expand Down Expand Up @@ -61,6 +63,13 @@ defmodule Rauversion.Tracks.Track do
timestamps()
end

use Fsmx.Struct,
transitions: %{
"pending" => ["processing", "processed"],
"processing" => ["pending", "processed"],
"processed" => ["pending"]
}

def record_type() do
"Track"
end
Expand Down Expand Up @@ -113,21 +122,11 @@ defmodule Rauversion.Tracks.Track do
# copy temp file from live
file = generate_local_copy(file)

Rauversion.Tracks.broadcast_change({:ok, struct.data}, [:tracks, :mp3_converting])

%{path: path, blob: blob} = convert_to_mp3(struct, file)

duration = blob |> ActiveStorage.Blob.metadata() |> Map.get("duration")

Rauversion.Tracks.broadcast_change({:ok, struct.data}, [:tracks, :mp3_converted])

case Rauversion.Services.PeaksGenerator.run_ffprobe(path, duration) do
[_ | _] = data ->
put_change(struct, :metadata, %{peaks: data})
%{file: file, track_id: struct.data.id}
|> Rauversion.Workers.TrackProcessorWorker.new()
|> Oban.insert()

_ ->
struct
end
struct
else
struct
end
Expand All @@ -152,21 +151,53 @@ defmodule Rauversion.Tracks.Track do
end
end

def process_peaks(struct, blob, path) do
duration = blob |> ActiveStorage.Blob.metadata() |> Map.get("duration")

Rauversion.Tracks.broadcast_change({:ok, struct.data}, [:tracks, :mp3_converted])

struct =
case Rauversion.Services.PeaksGenerator.run_ffprobe(path, duration) do
[_ | _] = data ->
put_change(struct, :metadata, %{peaks: data})
|> put_change(:state, "processed")

_ ->
put_change(struct, :state, "processed")
end

struct |> Rauversion.Repo.update()
end

def convert_to_mp3(struct, file) do
IO.inspect("CONVERT MP3 #{file.path}")
IO.inspect(File.exists?(file.path))
path = Rauversion.Services.Mp3Converter.run(file.path)

IO.inspect("CONVERTED MP3!! #{path}")
IO.inspect(File.exists?(path))
path = transform_to_mp3(file.path)
# audio_blob = create_and_analyze_audio(struct, file)
blob = create_and_analyze_audio(struct, file, "mp3_audio")
# create_and_analyze_audio(struct, file)

%{path: path, blob: blob}
end

def create_and_analyze_audio(struct, file, kind \\ "audio") do
blob =
Rauversion.BlobUtils.create_blob(file)
|> ActiveStorage.Blob.analyze()

Rauversion.BlobUtils.attach_file(struct, "mp3_audio", blob)
Rauversion.BlobUtils.attach_file(struct, kind, blob)

%{path: path, blob: blob}
blob
end

def transform_to_mp3(path) do
IO.inspect("CONVERT MP3 #{path}")
IO.inspect(File.exists?(path))
path = Rauversion.Services.Mp3Converter.run(path)
IO.inspect("CONVERTED MP3!! #{path}")
IO.inspect(File.exists?(path))
path
end

def generate_local_copy(file) do
Expand Down
21 changes: 21 additions & 0 deletions lib/rauversion/workers/track_processor_job.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Rauversion.Workers.TrackProcessorWorker do
use Oban.Worker, queue: :default, max_attempts: 1

# alias MyApp.{Endpoint, Zipper}

def perform(%_{args: %{"track_id" => track_id, "file" => file}}) do
struct = Rauversion.Tracks.get_track!(track_id) |> Rauversion.Tracks.change_track()

Rauversion.Tracks.broadcast_change({:ok, struct.data}, [:tracks, :mp3_converting])

file = file |> Map.new(fn {k, v} -> {String.to_atom(k), v} end)

%{path: path, blob: blob} = Rauversion.Tracks.Track.convert_to_mp3(struct, file)

Rauversion.Tracks.Track.process_peaks(struct, blob, path)

:ok
end

# ...
end
1 change: 1 addition & 0 deletions lib/rauversion_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule RauversionWeb.PageController do

defp list_tracks(page) do
Rauversion.Tracks.list_public_tracks()
|> Rauversion.Tracks.with_processed()
|> Rauversion.Repo.paginate(page: page, page_size: 5)
end

Expand Down
23 changes: 21 additions & 2 deletions lib/rauversion_web/live/track_live/components/track_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule RauversionWeb.TrackLive.TrackComponent do
<div class="w-full">
<% # if track.audio.persisted? %>
<%= if Rauversion.Tracks.is_processed?(track) do %>
<%= content_tag :div, id: "track-player-#{track.id}",
"phx-hook": "TrackHook",
"phx-update": "ignore",
Expand Down Expand Up @@ -107,7 +107,26 @@ defmodule RauversionWeb.TrackLive.TrackComponent do
</div>
</div>
<% end %>
<% #end %>
<% else %>
<!-- This example requires Tailwind CSS v2.0+ -->
<div class="bg-gray-50 border-l-4 border-gray-700 p-4 mt-2 mr-2">
<div class="flex">
<div class="flex-shrink-0">
<!-- Heroicon name: solid/exclamation -->
<svg class="h-5 w-5 text-gray-700" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clip-rule="evenodd" />
</svg>
</div>
<div class="ml-3">
<p class="text-sm text-gray-700">
Hold on, this track is in <%= if @track.state do @track.state else "pending" end %> state
</p>
</div>
</div>
</div>
<% end %>
<p class="hidden mt-1 text-sm text-gray-500">
<%= track.description %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ defmodule RauversionWeb.TrackLive.TrackListComponent do
# |> assign(tracks: tracks)}
# end

defp list_tracks(page, assigns) do
IO.inspect("OEIEIEIEIEIE")
defp list_tracks(page, assigns = %{current_user: %Rauversion.Accounts.User{}}) do
Tracks.list_tracks_by_username(assigns.profile.username)
|> Tracks.preload_tracks_preloaded_by_user(assigns[:current_user])
|> Repo.paginate(page: page, page_size: 5)
end

defp list_tracks(page, assigns = %{current_user: nil}) do
Tracks.list_tracks_by_username(assigns.profile.username)
|> Tracks.preload_tracks_preloaded_by_user(assigns[:current_user])
|> Tracks.with_processed()
|> Repo.paginate(page: page, page_size: 5)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ defmodule RauversionWeb.TrackLive.TrackListingComponent do

defp list_tracks(page) do
Tracks.list_public_tracks()
|> Rauversion.Tracks.with_processed()
|> Repo.paginate(page: page, page_size: 5)
end

Expand Down
18 changes: 16 additions & 2 deletions lib/rauversion_web/live/track_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

<div class="flex flex-col sm:flex-row mb-6">
<div class="flex-grow bg-black items-center text-white">
<%= if Rauversion.Tracks.is_processed?(@track) do %>
<img class="hidden h-32 w-full object-cover lg:h-48"
src="https://images.unsplash.com/photo-1444628838545-ac4016a5418a?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&amp;ixlib=rb-1.2.1&amp;auto=format&amp;fit=crop&amp;w=1950&amp;q=80" alt="">

Expand Down Expand Up @@ -93,6 +94,21 @@
</div>
</div>
<% end %>

<% else %>

<div class='flex items-center'>
<span class="text-2xl text-center p-5 space-x-2 flex items-center">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
</svg>
<p>
Hold on, this track is in <%= if @track.state do @track.state else "pending" end %> state
</p>
</span>
</div>

<% end %>
</div>

<div class="w-1/4 hidden sm:block">
Expand Down Expand Up @@ -137,8 +153,6 @@
</div>
<div class="mt-6 flex flex-col justify-stretch space-y-3 sm:flex-row sm:space-y-0 sm:space-x-4">



<.live_component
id={"share-track-button-#{@track.id}"}
module={RauversionWeb.TrackLive.ShareTrackButtonComponent}
Expand Down
9 changes: 8 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ defmodule Rauversion.MixProject do
{:countries, "~> 1.6"},
{:browser, "~> 0.4.4"},
{:timex, "~> 3.0"},
{:oban, "~> 2.13"},
{:fsmx, "~> 0.2.0"},
{
:active_storage,
git: "https://github.com/chaskiq/ex-rails.git", sparse: "apps/active_storage"
},
{
:active_job,
git: "https://github.com/chaskiq/ex-rails.git", sparse: "apps/active_job", override: true
}
# {
# :active_job,
Expand All @@ -75,7 +81,8 @@ defmodule Rauversion.MixProject do
# {:active_storage,
# path: "/Users/michelson/Documents/chaskiq/chaskiq-phoenix/ex_rails/apps/active_storage"}
# {:active_job,
# path: "/Users/michelson/Documents/chaskiq/chaskiq-phoenix/ex_rails/apps/active_job"}
# path: "/Users/michelson/Documents/chaskiq/chaskiq-phoenix/ex_rails/apps/active_job",
# override: true}
]
end

Expand Down
4 changes: 3 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%{
"active_job": {:hex, :active_job, "0.1.1", "e0a8c5beee0f4aca70c900aee23d54caf282eb541204b43375402d6504a0ac21", [:mix], [{:ecto, "~> 3.7.2", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.7.2", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:exq, "~> 0.16.2", [hex: :exq, repo: "hexpm", optional: true]}, {:oban, "~> 2.12", [hex: :oban, repo: "hexpm", optional: true]}], "hexpm", "3c3ed5d413a174dbb63f7481ec0d34d47a95eb4ba4dee76470bc5577d9f5fd22"},
"active_job": {:git, "https://github.com/chaskiq/ex-rails.git", "07c950de12946a657bc2f637044942bf7732796c", [sparse: "apps/active_job"]},
"active_storage": {:git, "https://github.com/chaskiq/ex-rails.git", "3cd730f99f6e71e3608519e4d15038698b70c02f", [sparse: "apps/active_storage"]},
"bcrypt_elixir": {:hex, :bcrypt_elixir, "3.0.0", "851d16b901b6c94a0ceadd3470f2b58d9899007bf04a42e0e4b399e2dd6ab307", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "fe251accb51f1aa17d67009cb125bddf81fb056cacad71bfad7827a44652f4ee"},
"browser": {:hex, :browser, "0.4.4", "bd6436961a6b2299c6cb38d0e49761c1161d869cd0db46369cef2bf6b77c3665", [:mix], [{:plug, "~> 1.2", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "d476ca309d4a4b19742b870380390aabbcb323c1f6f8745e2da2dfd079b4f8d7"},
Expand Down Expand Up @@ -32,6 +32,7 @@
"faker": {:hex, :faker, "0.17.0", "671019d0652f63aefd8723b72167ecdb284baf7d47ad3a82a15e9b8a6df5d1fa", [:mix], [], "hexpm", "a7d4ad84a93fd25c5f5303510753789fc2433ff241bf3b4144d3f6f291658a6a"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"floki": {:hex, :floki, "0.32.1", "dfe3b8db3b793939c264e6f785bca01753d17318d144bd44b407fb3493acaa87", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "d4b91c713e4a784a3f7b1e3cc016eefc619f6b1c3898464222867cafd3c681a3"},
"fsmx": {:hex, :fsmx, "0.2.1", "db1b2183e7d0f30f25db0d1aa86dd91b534b82321bf2db80d61055b4eb82823a", [:mix], [{:ecto, ">= 3.0.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_sql, ">= 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}], "hexpm", "4bbf619e1ae176af866b7e7951cbf97e2bd66f2a2ee68cea75ddfa2424125143"},
"gen_smtp": {:hex, :gen_smtp, "1.2.0", "9cfc75c72a8821588b9b9fe947ae5ab2aed95a052b81237e0928633a13276fd3", [:rebar3], [{:ranch, ">= 1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "5ee0375680bca8f20c4d85f58c2894441443a743355430ff33a783fe03296779"},
"geoip": {:hex, :geoip, "0.2.7", "629a51912ed79ab4a6e9c24a77573cce414bba9ef67b8c706350a24b741642ff", [:mix], [{:cachex, "~> 3.3", [hex: :cachex, repo: "hexpm", optional: false]}, {:httpoison, "~> 1.8", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "317451997312c8bd112eacae0e775daee4b71cc9147395143be9d47b0a487a1b"},
"gettext": {:hex, :gettext, "0.19.1", "564953fd21f29358e68b91634799d9d26989f8d039d7512622efb3c3b1c97892", [:mix], [], "hexpm", "10c656c0912b8299adba9b061c06947511e3f109ab0d18b44a866a4498e77222"},
Expand All @@ -46,6 +47,7 @@
"mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"mogrify": {:git, "https://github.com/chaskiq/mogrify.git", "48e237d2332d24ddf5996f78b13d8bc97221b094", [branch: "identify-option"]},
"oban": {:hex, :oban, "2.13.0", "797575c39d7a528009e54f31ce6d43a6a12f0d24737cdc74bc6094e282fb0293", [:mix], [{:ecto_sql, "~> 3.6", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.16", [hex: :postgrex, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "79b1e447551191ec995b3731876100037d59406290b84a4754694a8b66b1de80"},
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"phoenix": {:hex, :phoenix, "1.6.10", "7a9e8348c5c62e7fd2f74a1884b88d98251f87186a430048bfbdbab3e3f46736", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "08cf70d42f61dd0ea381805bac3cddef57b7b92ade5acc6f6036aa25ecaca9a2"},
"phoenix_ecto": {:hex, :phoenix_ecto, "4.4.0", "0672ed4e4808b3fbed494dded89958e22fb882de47a97634c0b13e7b0b5f7720", [:mix], [{:ecto, "~> 3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "09864e558ed31ee00bd48fcc1d4fc58ae9678c9e81649075431e69dbabb43cc1"},
Expand Down
11 changes: 11 additions & 0 deletions priv/repo/migrations/20220722054446_add_oban_jobs_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Rauversion.Repo.Migrations.AddObanJobsTable do
use Ecto.Migration

def up do
Oban.Migrations.up()
end

def down do
Oban.Migrations.down(version: 1)
end
end
11 changes: 11 additions & 0 deletions priv/repo/migrations/20220722134234_add_state_to_track.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Rauversion.Repo.Migrations.AddStateToTrack do
use Ecto.Migration

def change do
alter table(:tracks) do
add :state, :string
end

create index(:tracks, [:state])
end
end

0 comments on commit 2dc4b34

Please sign in to comment.