Skip to content

Commit

Permalink
Fixed error logging and fixed scheduling of notifications
Browse files Browse the repository at this point in the history
Signed-off-by: tiksan <[email protected]>
  • Loading branch information
dssecret committed Jan 29, 2025
1 parent b4ad846 commit 6cb0fbd
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
2 changes: 0 additions & 2 deletions commons/tornium_commons/models/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
BigIntegerField,
BooleanField,
CharField,
DateTimeField,
ForeignKeyField,
IntegerField,
UUIDField,
Expand All @@ -44,7 +43,6 @@ class Notification(BaseModel):
one_shot = BooleanField(default=True, null=False)
parameters = JSONField(default={}, null=False)

next_execution = DateTimeField(default=None, null=True)
error = CharField(default=None, null=True)
previous_state = JSONField(default={}, null=False)

Expand Down
2 changes: 2 additions & 0 deletions commons/tornium_commons/models/notification_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from peewee import (
BooleanField,
CharField,
DateTimeField,
ForeignKeyField,
IntegerField,
TextField,
Expand All @@ -37,6 +38,7 @@ class Meta:
owner = ForeignKeyField(User, null=False)

cron = CharField(default="* * * * *", null=False)
next_execution = DateTimeField(default=None, null=True)
resource = CharField(null=False, choices=["user", "faction", "company", "torn", "faction_v2"])
selections = ArrayField(CharField, default=[], index=False)
code = TextField(null=False)
Expand Down
17 changes: 13 additions & 4 deletions worker/lib/notification/audit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ defmodule Tornium.Notification.Audit do
nil
end

defp get_audit_channel(%Tornium.Schema.Notification{server: server_id}) do
defp get_audit_channel(%Tornium.Schema.Notification{server_id: server_id}) do
# TODO: Add caching to this
# Probably use LRU caching with TTL
# https://github.com/whitfin/cachex
Expand All @@ -146,16 +146,25 @@ defmodule Tornium.Notification.Audit do

%Tornium.Schema.ServerNotificationsConfig{log_channel: log_channel} = _ ->
log_channel

log_channel when is_integer(log_channel) ->
log_channel
end
end

@spec create_audit_message(
audit_channel :: integer(),
audit_channel :: integer() | nil,
action :: atom(),
description :: String.t(),
color :: integer() | nil
) :: {:ok, Nostrum.Struct.Message.t()} | Nostrum.Api.error()
defp create_audit_message(audit_channel, action, description, color \\ nil) do
) :: {:ok, Nostrum.Struct.Message.t()} | Nostrum.Api.error() | nil
defp create_audit_message(audit_channel, action, description, color \\ nil)

defp create_audit_message(audit_channel, _action, _description, _color) when is_nil(audit_channel) do
nil
end

defp create_audit_message(audit_channel, action, description, color) when is_integer(audit_channel) do
now =
DateTime.utc_now()
|> DateTime.to_iso8601()
Expand Down
6 changes: 4 additions & 2 deletions worker/lib/notification/notification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ defmodule Tornium.Notification do
end
end

# TODO: Handle when the channel_id is nil

defp try_message(
%{} = message,
:update,
Expand Down Expand Up @@ -427,8 +429,8 @@ defmodule Tornium.Notification do
@doc """
Update the next execution timestamp of the notification.
"""
@spec update_next_execution(notification :: Tornium.Schema.Notification.t()) :: nil
def update_next_execution(%Tornium.Schema.Notification{trigger: trigger, trigger_id: trigger_id} = _notification) do
@spec update_next_execution(notification :: Tornium.Schema.Trigger.t()) :: nil
def update_next_execution(%Tornium.Schema.Trigger{tid: trigger_id} = trigger) do
Tornium.Schema.Trigger
|> where([t], t.tid == ^trigger_id)
|> update([t], set: [next_execution: ^parse_next_execution(trigger)])
Expand Down
6 changes: 6 additions & 0 deletions worker/lib/workers/notification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ defmodule Tornium.Workers.Notification do
|> preload([n, t, s], trigger: t, server: s)
|> Repo.all()

notifications
|> Enum.uniq_by(fn %Tornium.Schema.Notification{} = notification -> notification.trigger end)
|> Enum.map(fn %Tornium.Schema.Notification{trigger: %Tornium.Schema.Trigger{} = trigger} = _notification ->
Tornium.Notification.update_next_execution(trigger)
end)

Tornium.Notification.execute_resource(String.to_atom(resource), resource_id, notifications)

:ok
Expand Down
3 changes: 3 additions & 0 deletions worker/lib/workers/notification_scheduler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ defmodule Tornium.Workers.NotificationScheduler do
@impl Oban.Worker
def perform(%Oban.Job{} = _job) do
Logger.debug("Scheduling notifications")
now = DateTime.utc_now()

# Schedule notifications for Discord servers where notifications are enabled
# TODO: Determine how this can be modified into query to include notifications outside of Discord servers
Tornium.Schema.Notification
|> where([n], not is_nil(n.server_id))
|> where([n], n.enabled == true)
|> join(:inner, [n], t in assoc(n, :trigger))
|> where([n, t, s, c], t.next_execution <= ^now or is_nil(t.next_execution))
|> join(:inner, [n, t], s in assoc(n, :server))
|> join(:inner, [n, t, s], c in assoc(s, :notifications_config))
|> where([n, t, s, c], c.enabled == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule Tornium.Repo.Migrations.AddTriggerTable do
add :owner_id, references(:user, column: :tid, type: :integer), null: false

add :cron, :string, default: "* * * * *", null: false
add :next_execution, :utc_datetime, default: nil, null: true

add :resource, :string, null: false # Enum is represented as a string in the database
add :selections, {:array, :string}, default: [], null: false
add :code, :text, default: "", null: false
Expand Down Expand Up @@ -37,7 +39,6 @@ defmodule Tornium.Repo.Migrations.AddTriggerTable do
add :one_shot, :boolean, default: true, null: false
add :parameters, :map, default: %{}, null: false

add :next_execution, :utc_datetime, default: nil, null: true
add :error, :text, default: nil, null: true
add :previous_state, :map, default: %{}, null: false
end
Expand Down

0 comments on commit 6cb0fbd

Please sign in to comment.