Skip to content

Commit

Permalink
feat: rename tasks context to operations context
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher2K committed Dec 2, 2024
1 parent 7cd634c commit cb88045
Show file tree
Hide file tree
Showing 27 changed files with 106 additions and 94 deletions.
4 changes: 2 additions & 2 deletions swapify_api/lib/swapify_api/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule SwapifyApi.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
alias SwapifyApi.Tasks.TaskEventHandler
alias SwapifyApi.Operations.TaskEventHandler

require Logger
require TaskEventHandler
Expand All @@ -14,7 +14,7 @@ defmodule SwapifyApi.Application do
# OpenTelemetry setup
OpentelemetryBandit.setup()
OpentelemetryPhoenix.setup(adapter: :bandit)
OpentelemetryEcto.setup([:swapify_api, :repo], db_statement: :enabled)
# OpentelemetryEcto.setup([:swapify_api, :repo], db_statement: :enabled)
# OpentelemetryOban.setup(trace: [:jobs])

# Rate limiting storage setup
Expand Down
2 changes: 1 addition & 1 deletion swapify_api/lib/swapify_api/music_providers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule SwapifyApi.MusicProviders do
alias SwapifyApi.MusicProviders.Jobs.SyncLibraryJob
alias SwapifyApi.MusicProviders.Jobs.SyncPlatformJob
alias SwapifyApi.MusicProviders.PlaylistRepo
alias SwapifyApi.Tasks.JobRepo
alias SwapifyApi.Operations.JobRepo
alias SwapifyApi.Repo

@doc """
Expand Down
2 changes: 1 addition & 1 deletion swapify_api/lib/swapify_api/music_providers/apple_music.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule SwapifyApi.MusicProviders.AppleMusic do
@moduledoc "Interfact to talk to Apple Music"
require Logger

alias SwapifyApi.Tasks.MatchedTrack
alias SwapifyApi.Operations.MatchedTrack
alias SwapifyApi.Utils
alias SwapifyApi.MusicProviders.Track

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ defmodule SwapifyApi.MusicProviders.Jobs.FindPlaylistTracksJob do
alias SwapifyApi.MusicProviders.Track
alias SwapifyApi.Notifications.JobErrorNotification
alias SwapifyApi.Notifications.JobUpdateNotification
alias SwapifyApi.Tasks
alias SwapifyApi.Tasks.MatchedTrack
alias SwapifyApi.Tasks.TaskEventHandler
alias SwapifyApi.Tasks.TransferRepo
alias SwapifyApi.Operations
alias SwapifyApi.Operations.MatchedTrack
alias SwapifyApi.Operations.TaskEventHandler
alias SwapifyApi.Operations.TransferRepo
alias SwapifyApi.Utils
alias SwapifyApiWeb.JobUpdateChannel

Expand Down Expand Up @@ -99,7 +99,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.FindPlaylistTracksJob do
) do
Task.Supervisor.start_child(Task.Supervisor, fn ->
with {:ok, %{email: email, username: username}} <- Accounts.get_by_id(user_id),
{:ok, transfer} <- Tasks.get_transfer_infos(transfer_id) do
{:ok, transfer} <- Operations.get_transfer_infos(transfer_id) do
SwapifyApi.Emails.transfer_ready(email, username,
username: username,
source_name: PlatformConnection.get_name(transfer.source),
Expand Down Expand Up @@ -128,9 +128,9 @@ defmodule SwapifyApi.MusicProviders.Jobs.FindPlaylistTracksJob do
)
)

with {:ok, _} <- Tasks.update_job_status(job_id, :error),
with {:ok, _} <- Operations.update_job_status(job_id, :error),
{:ok, %{username: username}} <- Accounts.get_by_id(user_id),
{:ok, transfer} <- Tasks.get_transfer_infos(transfer_id) do
{:ok, transfer} <- Operations.get_transfer_infos(transfer_id) do
SwapifyApi.Emails.transfer_error(transfer.email, username,
username: username,
source_name: PlatformConnection.get_name(transfer.source),
Expand All @@ -157,7 +157,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.FindPlaylistTracksJob do
{:error, %ErrorMessage{code: :not_found}} ->
with {:ok, _} <- process_match_results(unsaved_tracks, transfer_id, true),
{:ok, _} <- process_error_results(unsaved_not_found_tracks, transfer_id, true),
{:ok, _} <- Tasks.update_job_status(job_id, :done),
{:ok, _} <- Operations.update_job_status(job_id, :done),
{:ok, _} <- on_job_success(user_id, transfer_id) do
{:ok,
notification:
Expand Down Expand Up @@ -265,7 +265,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.FindPlaylistTracksJob do
{:error, %ErrorMessage{code: :not_found}} ->
with {:ok, _} <- process_match_results(unsaved_tracks, transfer_id, true),
{:ok, _} <- process_error_results(unsaved_not_found_tracks, transfer_id, true),
{:ok, _} <- Tasks.update_job_status(job_id, :done),
{:ok, _} <- Operations.update_job_status(job_id, :done),
{:ok, _} <- on_job_success(user_id, transfer_id) do
{:ok,
notification:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.SyncLibraryJob do
On success, returns a `{:ok, %JobUpdateNotification{}}`
"""
alias SwapifyApi.Tasks
alias SwapifyApi.Operations
alias SwapifyApi.MusicProviders
alias SwapifyApi.Accounts
alias SwapifyApi.MusicProviders.AppleMusic
Expand All @@ -25,7 +25,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.SyncLibraryJob do
alias SwapifyApi.MusicProviders.Spotify
alias SwapifyApi.Notifications.JobErrorNotification
alias SwapifyApi.Notifications.JobUpdateNotification
alias SwapifyApi.Tasks.TaskEventHandler
alias SwapifyApi.Operations.TaskEventHandler
alias SwapifyApi.Utils
alias SwapifyApiWeb.JobUpdateChannel

Expand Down Expand Up @@ -80,7 +80,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.SyncLibraryJob do
|> __MODULE__.new()
|> Oban.insert()
else
Tasks.update_job_status(job_id, :done)
Operations.update_job_status(job_id, :done)
end) do
Logger.debug("Fetched library items",
platform_name: platform_name,
Expand Down Expand Up @@ -274,7 +274,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.SyncLibraryJob do
MusicProviders.mark_playlist_transfer_as_failed(playlist_id)
end),
Task.async(fn ->
Tasks.update_job_status(job_id, :error)
Operations.update_job_status(job_id, :error)
end)
])
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ defmodule SwapifyApi.MusicProviders.Jobs.SyncPlatformJob do
On success, returns a `{:ok, %JobUpdateNotification{}}`
"""
alias SwapifyApi.Tasks
alias SwapifyApi.Operations
alias SwapifyApi.MusicProviders
alias SwapifyApi.Utils
alias SwapifyApi.Accounts
alias SwapifyApi.MusicProviders.AppleMusic
alias SwapifyApi.MusicProviders.AppleMusicTokenWorker
alias SwapifyApi.MusicProviders.Playlist
alias SwapifyApi.MusicProviders.Spotify
alias SwapifyApi.Tasks.TaskEventHandler
alias SwapifyApi.Operations.TaskEventHandler
alias SwapifyApi.Notifications.JobErrorNotification
alias SwapifyApi.Notifications.JobUpdateNotification
alias SwapifyApiWeb.JobUpdateChannel
Expand Down Expand Up @@ -78,7 +78,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.SyncPlatformJob do
total = response.body["total"]

with {:ok, _} <- MusicProviders.sync_playlist_metadata(:spotify, user_id, total),
{:ok, _} <- Tasks.update_job_status(job_id, :done) do
{:ok, _} <- Operations.update_job_status(job_id, :done) do
{:ok, notification: JobUpdateNotification.new_platform_sync_update("spotify", :done)}
end

Expand Down Expand Up @@ -122,7 +122,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.SyncPlatformJob do
total = response.body["meta"]["total"]

with {:ok, _} <- MusicProviders.sync_playlist_metadata(:applemusic, user_id, total),
{:ok, _} <- Tasks.update_job_status(job_id, :done) do
{:ok, _} <- Operations.update_job_status(job_id, :done) do
{:ok, notification: JobUpdateNotification.new_platform_sync_update("applemusic", :done)}
end

Expand Down Expand Up @@ -152,7 +152,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.SyncPlatformJob do
service: job_args["platform_name"]
)

Tasks.update_job_status(job_args["job_id"], :error)
Operations.update_job_status(job_args["job_id"], :error)
end

handle :success do
Expand Down Expand Up @@ -198,7 +198,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.SyncPlatformJob do
)

Task.async(fn ->
Tasks.update_job_status(job_id, :error)
Operations.update_job_status(job_id, :error)
end)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ defmodule SwapifyApi.MusicProviders.Jobs.TransferTracksJob do
alias SwapifyApi.MusicProviders.Spotify
alias SwapifyApi.Notifications.JobErrorNotification
alias SwapifyApi.Notifications.JobUpdateNotification
alias SwapifyApi.Tasks
alias SwapifyApi.Tasks.TaskEventHandler
alias SwapifyApi.Tasks.TransferRepo
alias SwapifyApi.Operations
alias SwapifyApi.Operations.TaskEventHandler
alias SwapifyApi.Operations.TransferRepo
alias SwapifyApi.Utils
alias SwapifyApiWeb.JobUpdateChannel

Expand All @@ -45,7 +45,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.TransferTracksJob do
defp on_job_success(user_id, transfer_id) do
Task.Supervisor.start_child(Task.Supervisor, fn ->
with {:ok, %{username: username}} <- Accounts.get_by_id(user_id),
{:ok, transfer} <- Tasks.get_transfer_infos(transfer_id) do
{:ok, transfer} <- Operations.get_transfer_infos(transfer_id) do
SwapifyApi.Emails.transfer_done(transfer.email, username,
app_url: Application.fetch_env!(:swapify_api, :app_url),
username: username,
Expand All @@ -67,9 +67,9 @@ defmodule SwapifyApi.MusicProviders.Jobs.TransferTracksJob do
JobErrorNotification.new_transfer_tracks_error(transfer_id)
)

with {:ok, _} <- Tasks.update_job_status(job_id, :error),
with {:ok, _} <- Operations.update_job_status(job_id, :error),
{:ok, %{username: username}} <- Accounts.get_by_id(user_id),
{:ok, transfer} <- Tasks.get_transfer_infos(transfer_id) do
{:ok, transfer} <- Operations.get_transfer_infos(transfer_id) do
SwapifyApi.Emails.transfer_error(transfer.email, username,
app_url: Application.fetch_env!(:swapify_api, :app_url),
username: username,
Expand All @@ -94,7 +94,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.TransferTracksJob do
) do
case TransferRepo.get_matched_tracks(transfer_id, offset, @spotify_add_limit) do
{:ok, []} ->
with {:ok, _} <- Tasks.update_job_status(job_id, :done),
with {:ok, _} <- Operations.update_job_status(job_id, :done),
{:ok, _} <- on_job_success(transfer_id, job_id) do
{:ok,
notification:
Expand Down Expand Up @@ -164,7 +164,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.TransferTracksJob do
) do
case TransferRepo.get_matched_track_by_index(transfer_id, offset) do
{:error, %ErrorMessage{code: :not_found}} ->
with {:ok, _} <- Tasks.update_job_status(job_id, :done),
with {:ok, _} <- Operations.update_job_status(job_id, :done),
{:ok, _} <- on_job_success(transfer_id, job_id) do
{:ok,
notification:
Expand Down Expand Up @@ -264,7 +264,7 @@ defmodule SwapifyApi.MusicProviders.Jobs.TransferTracksJob do
service: job_args["platform_name"]
)

Tasks.update_job_status(job_args["job_id"], :error)
Operations.update_job_status(job_args["job_id"], :error)
end

handle :success do
Expand Down
2 changes: 1 addition & 1 deletion swapify_api/lib/swapify_api/music_providers/spotify.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule SwapifyApi.MusicProviders.Spotify do
@moduledoc "Interface to talk to Spotify"
require Logger

alias SwapifyApi.Tasks.MatchedTrack
alias SwapifyApi.Operations.MatchedTrack
alias SwapifyApi.Utils
alias SwapifyApi.Oauth
alias SwapifyApi.MusicProviders.Track
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule SwapifyApi.Notifications.JobUpdateNotification do
@derive Jason.Encoder

alias SwapifyApi.Tasks.Job
alias SwapifyApi.Operations.Job

defstruct [:name, :data, tag: "JobUpdateNotification"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule SwapifyApi.Tasks do
defmodule SwapifyApi.Operations do
@moduledoc """
Task contexts
"""
Expand All @@ -8,10 +8,10 @@ defmodule SwapifyApi.Tasks do
alias SwapifyApi.MusicProviders.Jobs.FindPlaylistTracksJob
alias SwapifyApi.MusicProviders.Jobs.TransferTracksJob
alias SwapifyApi.MusicProviders.PlaylistRepo
alias SwapifyApi.Tasks.Job
alias SwapifyApi.Tasks.JobRepo
alias SwapifyApi.Tasks.Transfer
alias SwapifyApi.Tasks.TransferRepo
alias SwapifyApi.Operations.Job
alias SwapifyApi.Operations.JobRepo
alias SwapifyApi.Operations.Transfer
alias SwapifyApi.Operations.TransferRepo

@doc "Update a job status and timestamps if needed"
@spec update_job_status(String.t(), Job.job_status()) ::
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule SwapifyApi.Tasks.Job do
defmodule SwapifyApi.Operations.Job do
use SwapifyApi.Schema

alias SwapifyApi.Accounts.User
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule SwapifyApi.Tasks.JobJSON do
alias SwapifyApi.Tasks.Job
defmodule SwapifyApi.Operations.JobJSON do
alias SwapifyApi.Operations.Job

@fields [
:id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule SwapifyApi.Tasks.JobRepo do
defmodule SwapifyApi.Operations.JobRepo do
alias SwapifyApi.Utils
alias SwapifyApi.Tasks.Job
alias SwapifyApi.Operations.Job
alias SwapifyApi.Repo

def create(args),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule SwapifyApi.Tasks.MatchedTrack do
defmodule SwapifyApi.Operations.MatchedTrack do
@moduledoc "Business representation of a track found on another platform during a transfer"
@derive Jason.Encoder

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule SwapifyApi.Tasks.TaskEventHandler do
defmodule SwapifyApi.Operations.TaskEventHandler do
@moduledoc """
Macros defining events handlers
## Examples
use SwapifyApi.Tasks.TaskEventHandler, job_module: "Swapify.MyObanJob"
use SwapifyApi.Operations.TaskEventHandler, job_module: "Swapify.MyObanJob"
handle :success do
# do something with the `args` variable
Expand All @@ -15,7 +15,7 @@ defmodule SwapifyApi.Tasks.TaskEventHandler do
"""
defmacro __using__(opts) do
quote do
import SwapifyApi.Tasks.TaskEventHandler
import SwapifyApi.Operations.TaskEventHandler
@jobname unquote(opts[:job_module])
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule SwapifyApi.Tasks.Transfer do
defmodule SwapifyApi.Operations.Transfer do
@moduledoc "Business a representation of a playlist transfer"
use SwapifyApi.Schema

alias SwapifyApi.Accounts.PlatformConnection
alias SwapifyApi.Accounts.User
alias SwapifyApi.MusicProviders.Playlist
alias SwapifyApi.MusicProviders.Track
alias SwapifyApi.Tasks.Job
alias SwapifyApi.Tasks.MatchedTrack
alias SwapifyApi.Operations.Job
alias SwapifyApi.Operations.MatchedTrack

@type t :: %__MODULE__{
id: String.t(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule SwapifyApi.Tasks.TransferJSON do
alias SwapifyApi.Tasks.JobJSON
defmodule SwapifyApi.Operations.TransferJSON do
alias SwapifyApi.Operations.JobJSON
alias SwapifyApi.MusicProviders.PlaylistJSON
alias SwapifyApi.Tasks.Transfer
alias SwapifyApi.Operations.Transfer

def show(%Transfer{} = t) do
%{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule SwapifyApi.Tasks.TransferRepo do
defmodule SwapifyApi.Operations.TransferRepo do
@doc "Transfer table repository"
use OpenTelemetryDecorator

Expand All @@ -7,8 +7,8 @@ defmodule SwapifyApi.Tasks.TransferRepo do
alias SwapifyApi.MusicProviders.Playlist
alias SwapifyApi.MusicProviders.Track
alias SwapifyApi.Repo
alias SwapifyApi.Tasks.MatchedTrack
alias SwapifyApi.Tasks.Transfer
alias SwapifyApi.Operations.MatchedTrack
alias SwapifyApi.Operations.Transfer
alias SwapifyApi.Utils

@spec create(map()) :: {:ok, Transfer.t()} | {:error, Ecto.Changeset.t()}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
defmodule SwapifyApiWeb.AdminDashboardController do
use SwapifyApiWeb, :controller

alias SwapifyApi.Tasks
alias SwapifyApi.Operations
alias SwapifyApi.Accounts

plug :put_layout, {SwapifyApiWeb.Layouts, :admin}

def index(conn, _) do
with {:ok, user_count} <- Accounts.count_users(),
{:ok, transfer_count} <- Tasks.count_transfers() do
{:ok, transfer_count} <- Operations.count_transfers() do
conn |> render(:index, user_count: user_count, transfer_count: transfer_count)
end
end
Expand Down
Loading

0 comments on commit cb88045

Please sign in to comment.