Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add all necessary notification info in the oban job metadata #297

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ defmodule Orcasite.Notifications.Notification do
create :notify_confirmed_candidate do
description "Create a notification for confirmed candidate (i.e. detection group)"
accept [:candidate_id]
argument :candidate_id, :integer
argument :candidate_id, :string
argument :node, :string, allow_nil?: false

argument :message, :string do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ defmodule Orcasite.Notifications.NotificationInstance do
%{
notification_instance_id: record.id,
notification_id: record.notification_id,
subscription_id: record.subscription_id
subscription_id: record.subscription_id,
meta: record.meta
}
|> Orcasite.Notifications.Workers.SendNotificationEmail.new()
|> Oban.insert()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ defmodule Orcasite.Notifications.Workers.SendNotificationEmail do

@impl Oban.Worker
def perform(%Oban.Job{
args: %{"notification_instance_id" => notification_instance_id} = _args
args:
%{
"notification_id" => notification_id,
"subscription_id" => subscription_id,
"meta" => meta,
"notification_instance_id" => notification_instance_id
} = _args
}) do
notif_instance =
NotificationInstance
|> Notifications.get!(notification_instance_id)
|> Notifications.load!([:notification, subscription: [:subscriber]])
notification = Notification |> Notifications.get!(notification_id)

subscription =
Subscription |> Notifications.get!(subscription_id) |> Notifications.load!(:subscriber)

params =
notif_instance.meta
|> Map.merge(notif_instance.subscription.meta)
|> Map.merge(notif_instance.notification.meta)
|> stringify_map()
[meta, subscription.meta, notification.meta]
|> Enum.map(&stringify_map/1)
|> Enum.reduce(&Map.merge/2)

unsubscribe_token =
Orcasite.Notifications.Subscription.unsubscribe_token(notif_instance.subscription)
Orcasite.Notifications.Subscription.unsubscribe_token(subscription)

notifications_since =
notif_instance.subscription.last_notification_id
subscription.last_notification_id
|> case do
nil ->
[]
Expand All @@ -33,7 +38,7 @@ defmodule Orcasite.Notifications.Workers.SendNotificationEmail do
|> Ash.Query.for_read(:since_notification, %{notification_id: notif_id})
|> Orcasite.Notifications.read!()
end
|> Enum.filter(&(&1.id != notif_instance.notification_id))
|> Enum.filter(&(&1.id != notification_id))

:ok = continue?()

Expand All @@ -49,13 +54,21 @@ defmodule Orcasite.Notifications.Workers.SendNotificationEmail do
|> email_for_notif(stringify(params["event_type"]))
|> Orcasite.Mailer.deliver()

notif_instance.subscription
|> Subscription.update_last_notification(notif_instance.notification_id)
subscription
|> Subscription.update_last_notification(notification_id)

Task.Supervisor.async_nolink(Orcasite.TaskSupervisor, fn ->
notif_instance
|> Ash.Changeset.for_destroy(:destroy)
|> Notifications.destroy!()
NotificationInstance
|> Notifications.get!(notification_instance_id)
|> case do
nil ->
nil

notif_instance ->
notif_instance
|> Ash.Changeset.for_destroy(:destroy)
|> Notifications.destroy!()
end
end)

:ok
Expand Down Expand Up @@ -83,7 +96,9 @@ defmodule Orcasite.Notifications.Workers.SendNotificationEmail do

def continue?() do
case Hammer.check_rate("ses_email", 1_000, 14) do
{:allow, _count} -> :ok
{:allow, _count} ->
:ok

{:deny, _limit} ->
Process.sleep(250)
continue?()
Expand Down
Loading