From ff5a1793fa7bccbbd18c10c421a7d0ebf73bd7a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Mon, 20 May 2024 19:36:29 +0100 Subject: [PATCH 01/14] Concache supervisor in cache helpers --- lib/teiserver/application.ex | 31 +++------------------------ lib/teiserver/helpers/cache_helper.ex | 29 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/lib/teiserver/application.ex b/lib/teiserver/application.ex index eb9454171..1b2e2d853 100644 --- a/lib/teiserver/application.ex +++ b/lib/teiserver/application.ex @@ -7,6 +7,9 @@ defmodule Teiserver.Application do alias Phoenix.PubSub require Logger + import Teiserver.Helpers.CacheHelper, + only: [concache_sup: 1, concache_sup: 2, concache_perm_sup: 1] + @impl true def start(_type, _args) do # List all child processes to be supervised @@ -203,34 +206,6 @@ defmodule Teiserver.Application do end end - defp concache_sup(name, opts \\ []) do - Supervisor.child_spec( - { - ConCache, - [ - name: name, - ttl_check_interval: 10_000, - global_ttl: opts[:global_ttl] || 60_000, - touch_on_read: true - ] - }, - id: {ConCache, name} - ) - end - - defp concache_perm_sup(name) do - Supervisor.child_spec( - { - ConCache, - [ - name: name, - ttl_check_interval: false - ] - }, - id: {ConCache, name} - ) - end - def startup_sub_functions({:error, _}), do: :error def startup_sub_functions(_) do diff --git a/lib/teiserver/helpers/cache_helper.ex b/lib/teiserver/helpers/cache_helper.ex index a3eb84475..da70dbf5b 100644 --- a/lib/teiserver/helpers/cache_helper.ex +++ b/lib/teiserver/helpers/cache_helper.ex @@ -92,4 +92,33 @@ defmodule Teiserver.Helpers.CacheHelper do def store_get_or_store(table, key, func) do ConCache.get_or_store(table, key, func) end + + # Setup and supervisors + def concache_sup(name, opts \\ []) do + Supervisor.child_spec( + { + ConCache, + [ + name: name, + ttl_check_interval: 10_000, + global_ttl: opts[:global_ttl] || 60_000, + touch_on_read: true + ] + }, + id: {ConCache, name} + ) + end + + def concache_perm_sup(name) do + Supervisor.child_spec( + { + ConCache, + [ + name: name, + ttl_check_interval: false + ] + }, + id: {ConCache, name} + ) + end end From 44b7ce77f3a00c39bc974ba7eeb17417151b739f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Mon, 20 May 2024 19:05:01 +0100 Subject: [PATCH 02/14] Permissions cache --- .../account/caches/permission_cache.ex | 89 +++++++++++++++++++ lib/teiserver/application.ex | 3 +- lib/teiserver/startup.ex | 58 ------------ 3 files changed, 90 insertions(+), 60 deletions(-) create mode 100644 lib/teiserver/account/caches/permission_cache.ex diff --git a/lib/teiserver/account/caches/permission_cache.ex b/lib/teiserver/account/caches/permission_cache.ex new file mode 100644 index 000000000..4610e4b90 --- /dev/null +++ b/lib/teiserver/account/caches/permission_cache.ex @@ -0,0 +1,89 @@ +defmodule Teiserver.Account.PermissionCache do + @moduledoc """ + Define caches and set them up for permission related things + """ + + use Supervisor + import Teiserver.Account.AuthLib, only: [add_permission_set: 3] + alias Teiserver.Helpers.CacheHelper + + def start_link(opts) do + with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts), + :ok <- warm_permission_cache() do + warm_restriction_cache() + {:ok, sup} + end + end + + @impl true + def init(:ok) do + children = [ + CacheHelper.concache_perm_sup(:auth_group_store), + CacheHelper.concache_perm_sup(:restriction_lookup_store) + ] + + Supervisor.init(children, strategy: :one_for_all) + end + + defp warm_permission_cache() do + add_permission_set("admin", "debug", ~w(debug)) + add_permission_set("admin", "dev", ~w(developer structure)) + add_permission_set("admin", "admin", ~w(limited full)) + add_permission_set("admin", "report", ~w(show update delete report)) + add_permission_set("admin", "user", ~w(show create update delete report)) + add_permission_set("admin", "group", ~w(show create update delete report config)) + add_permission_set("teiserver", "admin", ~w(account battle clan queue)) + + add_permission_set( + "teiserver", + "staff", + ~w(overwatch reviewer moderator admin communication clan telemetry server) + ) + + add_permission_set("teiserver", "dev", ~w(infolog)) + add_permission_set("teiserver", "reports", ~w(client server match ratings infolog)) + add_permission_set("teiserver", "api", ~w(battle)) + + add_permission_set( + "teiserver", + "player", + ~w(account tester contributor dev streamer donor verified bot moderator) + ) + + :ok + end + + defp warm_restriction_cache() do + # Chat stuff + Teiserver.Account.UserLib.add_report_restriction_types("Chat", [ + "Bridging", + "Game chat", + "Room chat", + "All chat" + ]) + + # Lobby interaction + Teiserver.Account.UserLib.add_report_restriction_types("Game", [ + "Low priority", + "All lobbies", + "Login", + "Permanently banned" + ]) + + Teiserver.Account.UserLib.add_report_restriction_types("Other", [ + "Accolades", + "Boss", + "Reporting", + "Renaming", + "Matchmaking" + ]) + + Teiserver.Account.UserLib.add_report_restriction_types("Warnings", [ + "Warning reminder" + ]) + + Teiserver.Account.UserLib.add_report_restriction_types("Internal", [ + "Note" + ]) + end +end diff --git a/lib/teiserver/application.ex b/lib/teiserver/application.ex index 1b2e2d853..65522c073 100644 --- a/lib/teiserver/application.ex +++ b/lib/teiserver/application.ex @@ -49,8 +49,7 @@ defmodule Teiserver.Application do concache_sup(:account_avoiding_this_cache), concache_sup(:account_blocking_this_cache), concache_perm_sup(:recently_used_cache), - concache_perm_sup(:auth_group_store), - concache_perm_sup(:restriction_lookup_store), + Teiserver.Account.PermissionCache, concache_perm_sup(:config_user_type_store), concache_perm_sup(:config_site_type_store), concache_perm_sup(:config_site_cache), diff --git a/lib/teiserver/startup.ex b/lib/teiserver/startup.ex index 1c6b920ff..f13ed5833 100644 --- a/lib/teiserver/startup.ex +++ b/lib/teiserver/startup.ex @@ -31,13 +31,6 @@ defmodule Teiserver.Startup do ~w(hamster gerbil cat dog falcon eagle mole fox tiger panda elephant lion cow dove whale dolphin squid dragon snake platypus badger) ) - add_permission_set("admin", "debug", ~w(debug)) - add_permission_set("admin", "dev", ~w(developer structure)) - add_permission_set("admin", "admin", ~w(limited full)) - add_permission_set("admin", "report", ~w(show update delete report)) - add_permission_set("admin", "user", ~w(show create update delete report)) - add_permission_set("admin", "group", ~w(show create update delete report config)) - Teiserver.Logging.Startup.startup() # User Configs @@ -60,38 +53,6 @@ defmodule Teiserver.Startup do Teiserver.Bridge.CommandLib.cache_discord_commands() Teiserver.Communication.pre_cache_discord_channels() - # Chat stuff - Teiserver.Account.UserLib.add_report_restriction_types("Chat", [ - "Bridging", - "Game chat", - "Room chat", - "All chat" - ]) - - # Lobby interaction - Teiserver.Account.UserLib.add_report_restriction_types("Game", [ - "Low priority", - "All lobbies", - "Login", - "Permanently banned" - ]) - - Teiserver.Account.UserLib.add_report_restriction_types("Other", [ - "Accolades", - "Boss", - "Reporting", - "Renaming", - "Matchmaking" - ]) - - Teiserver.Account.UserLib.add_report_restriction_types("Warnings", [ - "Warning reminder" - ]) - - Teiserver.Account.UserLib.add_report_restriction_types("Internal", [ - "Note" - ]) - add_audit_types([ "Account:User password reset", "Account:Failed login", @@ -122,25 +83,6 @@ defmodule Teiserver.Startup do "Discord.text_callback" ]) - # Permissions setup - add_permission_set("teiserver", "admin", ~w(account battle clan queue)) - - add_permission_set( - "teiserver", - "staff", - ~w(overwatch reviewer moderator admin communication clan telemetry server) - ) - - add_permission_set("teiserver", "dev", ~w(infolog)) - add_permission_set("teiserver", "reports", ~w(client server match ratings infolog)) - add_permission_set("teiserver", "api", ~w(battle)) - - add_permission_set( - "teiserver", - "player", - ~w(account tester contributor dev streamer donor verified bot moderator) - ) - Teiserver.store_put( :application_metadata_cache, "random_names_3", From 867916e91d8989eaafacb0c280a5c57b853b86d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Mon, 20 May 2024 19:12:09 +0100 Subject: [PATCH 03/14] Logging permission --- lib/teiserver/account/caches/permission_cache.ex | 2 ++ lib/teiserver/logging/lib/startup.ex | 1 + lib/teiserver/startup.ex | 2 -- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/teiserver/account/caches/permission_cache.ex b/lib/teiserver/account/caches/permission_cache.ex index 4610e4b90..805df692a 100644 --- a/lib/teiserver/account/caches/permission_cache.ex +++ b/lib/teiserver/account/caches/permission_cache.ex @@ -50,6 +50,8 @@ defmodule Teiserver.Account.PermissionCache do ~w(account tester contributor dev streamer donor verified bot moderator) ) + :ok = Teiserver.Logging.Startup.startup() + :ok end diff --git a/lib/teiserver/logging/lib/startup.ex b/lib/teiserver/logging/lib/startup.ex index 822fba28c..b7e8a54c5 100644 --- a/lib/teiserver/logging/lib/startup.ex +++ b/lib/teiserver/logging/lib/startup.ex @@ -8,5 +8,6 @@ defmodule Teiserver.Logging.Startup do add_permission_set("logging", "audit", ~w(show delete report)) add_permission_set("logging", "error", ~w(show delete report)) add_permission_set("logging", "live", ~w(show)) + :ok end end diff --git a/lib/teiserver/startup.ex b/lib/teiserver/startup.ex index f13ed5833..e6eaecc39 100644 --- a/lib/teiserver/startup.ex +++ b/lib/teiserver/startup.ex @@ -31,8 +31,6 @@ defmodule Teiserver.Startup do ~w(hamster gerbil cat dog falcon eagle mole fox tiger panda elephant lion cow dove whale dolphin squid dragon snake platypus badger) ) - Teiserver.Logging.Startup.startup() - # User Configs Teiserver.Config.UserConfigTypes.ProfileConfigs.create() Teiserver.Config.UserConfigTypes.PrivacyConfigs.create() From 5dc42fb19fdbc76d6235bb01e98d77bb3a1b5fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Mon, 20 May 2024 19:16:36 +0100 Subject: [PATCH 04/14] Profile config --- lib/teiserver/application.ex | 2 +- .../config/user_config_types/cache.ex | 25 +++++++++++++++++++ .../user_config_types/profile_configs.ex | 23 ++++++++++++++++- lib/teiserver/startup.ex | 4 --- 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 lib/teiserver/config/user_config_types/cache.ex diff --git a/lib/teiserver/application.ex b/lib/teiserver/application.ex index 65522c073..531080253 100644 --- a/lib/teiserver/application.ex +++ b/lib/teiserver/application.ex @@ -50,7 +50,7 @@ defmodule Teiserver.Application do concache_sup(:account_blocking_this_cache), concache_perm_sup(:recently_used_cache), Teiserver.Account.PermissionCache, - concache_perm_sup(:config_user_type_store), + Teiserver.Config.UserConfigTypes.Cache, concache_perm_sup(:config_site_type_store), concache_perm_sup(:config_site_cache), concache_perm_sup(:application_metadata_cache), diff --git a/lib/teiserver/config/user_config_types/cache.ex b/lib/teiserver/config/user_config_types/cache.ex new file mode 100644 index 000000000..4100b0e3c --- /dev/null +++ b/lib/teiserver/config/user_config_types/cache.ex @@ -0,0 +1,25 @@ +defmodule Teiserver.Config.UserConfigTypes.Cache do + @moduledoc """ + Cache and setup for profile configuration + """ + + use Supervisor + alias Teiserver.Helpers.CacheHelper + + def start_link(opts) do + with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts), + :ok <- Teiserver.Config.UserConfigTypes.ProfileConfigs.create(), + :ok <- Teiserver.Config.UserConfigTypes.PrivacyConfigs.create() do + {:ok, sup} + end + end + + @impl true + def init(:ok) do + children = [ + CacheHelper.concache_perm_sup(:config_user_type_store) + ] + + Supervisor.init(children, strategy: :one_for_one) + end +end diff --git a/lib/teiserver/config/user_config_types/profile_configs.ex b/lib/teiserver/config/user_config_types/profile_configs.ex index 5597c4373..7c1aacf71 100644 --- a/lib/teiserver/config/user_config_types/profile_configs.ex +++ b/lib/teiserver/config/user_config_types/profile_configs.ex @@ -1,7 +1,28 @@ defmodule Teiserver.Config.UserConfigTypes.ProfileConfigs do - @moduledoc false + @moduledoc """ + Cache and setup for profile configuration + """ + + use Supervisor + import Teiserver.Config, only: [add_user_config_type: 1] + def start_link(opts) do + with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts), + :ok <- create() do + {:ok, sup} + end + end + + @impl true + def init(:ok) do + children = [ + {ConCache, [name: :config_user_type_store, ttl_check_interval: false]} + ] + + Supervisor.init(children, strategy: :one_for_one) + end + @spec create() :: :ok def create() do add_user_config_type(%{ diff --git a/lib/teiserver/startup.ex b/lib/teiserver/startup.ex index e6eaecc39..008b52c55 100644 --- a/lib/teiserver/startup.ex +++ b/lib/teiserver/startup.ex @@ -31,10 +31,6 @@ defmodule Teiserver.Startup do ~w(hamster gerbil cat dog falcon eagle mole fox tiger panda elephant lion cow dove whale dolphin squid dragon snake platypus badger) ) - # User Configs - Teiserver.Config.UserConfigTypes.ProfileConfigs.create() - Teiserver.Config.UserConfigTypes.PrivacyConfigs.create() - # System Configs Teiserver.Config.SiteConfigTypes.SystemConfigs.create() From 0c19128fb6e4d2bdebfcbcdf5ed5697f1cea4bcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Mon, 20 May 2024 19:23:31 +0100 Subject: [PATCH 05/14] Site config --- lib/teiserver/application.ex | 3 +-- .../config/site_config_types/cache.ex | 25 +++++++++++++++++++ lib/teiserver/startup.ex | 3 --- 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 lib/teiserver/config/site_config_types/cache.ex diff --git a/lib/teiserver/application.ex b/lib/teiserver/application.ex index 531080253..c504968a4 100644 --- a/lib/teiserver/application.ex +++ b/lib/teiserver/application.ex @@ -51,8 +51,7 @@ defmodule Teiserver.Application do concache_perm_sup(:recently_used_cache), Teiserver.Account.PermissionCache, Teiserver.Config.UserConfigTypes.Cache, - concache_perm_sup(:config_site_type_store), - concache_perm_sup(:config_site_cache), + Teiserver.Config.SiteConfigTypes.Cache, concache_perm_sup(:application_metadata_cache), concache_sup(:application_temp_cache), concache_sup(:config_user_cache), diff --git a/lib/teiserver/config/site_config_types/cache.ex b/lib/teiserver/config/site_config_types/cache.ex new file mode 100644 index 000000000..e67029563 --- /dev/null +++ b/lib/teiserver/config/site_config_types/cache.ex @@ -0,0 +1,25 @@ +defmodule Teiserver.Config.SiteConfigTypes.Cache do + @moduledoc """ + Cache and setup for site configuration + """ + + use Supervisor + alias Teiserver.Helpers.CacheHelper + + def start_link(opts) do + with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts), + :ok <- Teiserver.Config.SiteConfigTypes.SystemConfigs.create() do + {:ok, sup} + end + end + + @impl true + def init(:ok) do + children = [ + CacheHelper.concache_perm_sup(:config_site_type_store), + CacheHelper.concache_perm_sup(:config_site_cache) + ] + + Supervisor.init(children, strategy: :one_for_all) + end +end diff --git a/lib/teiserver/startup.ex b/lib/teiserver/startup.ex index 008b52c55..be9edbeb9 100644 --- a/lib/teiserver/startup.ex +++ b/lib/teiserver/startup.ex @@ -31,9 +31,6 @@ defmodule Teiserver.Startup do ~w(hamster gerbil cat dog falcon eagle mole fox tiger panda elephant lion cow dove whale dolphin squid dragon snake platypus badger) ) - # System Configs - Teiserver.Config.SiteConfigTypes.SystemConfigs.create() - Teiserver.TeiserverConfigs.teiserver_configs() Teiserver.Communication.build_text_callback_cache() From 3b887cbc57a5db9a1f01cb8e10af971aded21cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Mon, 20 May 2024 19:53:45 +0100 Subject: [PATCH 06/14] Metadata cache --- lib/teiserver/application.ex | 2 +- lib/teiserver/metadata_cache.ex | 50 +++++++++++++++++++++++++++++++++ lib/teiserver/startup.ex | 30 -------------------- 3 files changed, 51 insertions(+), 31 deletions(-) create mode 100644 lib/teiserver/metadata_cache.ex diff --git a/lib/teiserver/application.ex b/lib/teiserver/application.ex index c504968a4..2c71bab33 100644 --- a/lib/teiserver/application.ex +++ b/lib/teiserver/application.ex @@ -52,7 +52,7 @@ defmodule Teiserver.Application do Teiserver.Account.PermissionCache, Teiserver.Config.UserConfigTypes.Cache, Teiserver.Config.SiteConfigTypes.Cache, - concache_perm_sup(:application_metadata_cache), + Teiserver.MetadataCache, concache_sup(:application_temp_cache), concache_sup(:config_user_cache), diff --git a/lib/teiserver/metadata_cache.ex b/lib/teiserver/metadata_cache.ex new file mode 100644 index 000000000..ce18dbb04 --- /dev/null +++ b/lib/teiserver/metadata_cache.ex @@ -0,0 +1,50 @@ +defmodule Teiserver.MetadataCache do + @moduledoc """ + Cache and setup for miscellaneous metadata + """ + + use Supervisor + alias Teiserver.Helpers.CacheHelper + + def start_link(opts) do + with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts), + :ok <- random_names() do + {:ok, sup} + end + end + + @impl true + def init(:ok) do + children = [ + CacheHelper.concache_perm_sup(:application_metadata_cache) + ] + + Supervisor.init(children, strategy: :one_for_one) + end + + defp random_names() do + # Brought over from Central + Teiserver.store_put( + :application_metadata_cache, + "random_names_1", + ~w(serene energised humble auspicious decisive exemplary cheerful determined playful spry springy) + ) + + Teiserver.store_put(:application_metadata_cache, "random_names_2", ~w( + maroon cherry rose ruby + amber carrot + lemon beige + mint lime cadmium + aqua cerulean + lavender indigo + magenta amethyst + )) + + Teiserver.store_put( + :application_metadata_cache, + "random_names_3", + ~w(tick pawn lazarus rocketeer crossbow mace centurion tumbleweed smuggler compass ghost sprinter butler webber platypus hound welder recluse archangel gunslinger sharpshooter umbrella fatboy marauder vanguard razorback titan) ++ + ~w(grunt graverobber aggravator trasher thug bedbug deceiver augur spectre fiend twitcher duck skuttle sumo arbiter manticore termite commando mammoth shiva karganeth catapult behemoth juggernaught) + ) + end +end diff --git a/lib/teiserver/startup.ex b/lib/teiserver/startup.ex index be9edbeb9..125b9c55a 100644 --- a/lib/teiserver/startup.ex +++ b/lib/teiserver/startup.ex @@ -8,29 +8,6 @@ defmodule Teiserver.Startup do def startup do start_time = System.system_time(:millisecond) - # Brought over from Central - Teiserver.store_put( - :application_metadata_cache, - "random_names_1", - ~w(serene energised humble auspicious decisive exemplary cheerful determined playful spry springy) - ) - - Teiserver.store_put(:application_metadata_cache, "random_names_2", ~w( - maroon cherry rose ruby - amber carrot - lemon beige - mint lime cadmium - aqua cerulean - lavender indigo - magenta amethyst - )) - - Teiserver.store_put( - :application_metadata_cache, - "random_names_3", - ~w(hamster gerbil cat dog falcon eagle mole fox tiger panda elephant lion cow dove whale dolphin squid dragon snake platypus badger) - ) - Teiserver.TeiserverConfigs.teiserver_configs() Teiserver.Communication.build_text_callback_cache() @@ -74,13 +51,6 @@ defmodule Teiserver.Startup do "Discord.text_callback" ]) - Teiserver.store_put( - :application_metadata_cache, - "random_names_3", - ~w(tick pawn lazarus rocketeer crossbow mace centurion tumbleweed smuggler compass ghost sprinter butler webber platypus hound welder recluse archangel gunslinger sharpshooter umbrella fatboy marauder vanguard razorback titan) ++ - ~w(grunt graverobber aggravator trasher thug bedbug deceiver augur spectre fiend twitcher duck skuttle sumo arbiter manticore termite commando mammoth shiva karganeth catapult behemoth juggernaught) - ) - Teiserver.cache_put(:lists, :rooms, []) Teiserver.cache_put(:lists, :lobby_policies, []) From 11b1d9e205079077de70bdb290d21ee23a2531c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Mon, 20 May 2024 19:57:00 +0100 Subject: [PATCH 07/14] Text callbacks --- lib/teiserver/application.ex | 4 ---- lib/teiserver/communication/libs/cache.ex | 25 +++++++++++++++++++++++ lib/teiserver/startup.ex | 2 -- 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 lib/teiserver/communication/libs/cache.ex diff --git a/lib/teiserver/application.ex b/lib/teiserver/application.ex index 2c71bab33..6be7cf021 100644 --- a/lib/teiserver/application.ex +++ b/lib/teiserver/application.ex @@ -163,10 +163,6 @@ defmodule Teiserver.Application do # Telemetry {Teiserver.Telemetry.TelemetryServer, name: Teiserver.Telemetry.TelemetryServer}, - # Text callbacks - concache_perm_sup(:text_callback_trigger_lookup), - concache_perm_sup(:text_callback_store), - # Ranch servers %{ id: Teiserver.SSLSpringTcpServer, diff --git a/lib/teiserver/communication/libs/cache.ex b/lib/teiserver/communication/libs/cache.ex new file mode 100644 index 000000000..60b58c21d --- /dev/null +++ b/lib/teiserver/communication/libs/cache.ex @@ -0,0 +1,25 @@ +defmodule Teiserver.Communication.Cache do + @moduledoc """ + Cache and setup for communication stuff + """ + + use Supervisor + alias Teiserver.Helpers.CacheHelper + + def start_link(opts) do + with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts), + :ok <- Teiserver.Communication.build_text_callback_cache() do + {:ok, sup} + end + end + + @impl true + def init(:ok) do + children = [ + CacheHelper.concache_perm_sup(:text_callback_trigger_lookup), + CacheHelper.concache_perm_sup(:text_callback_store) + ] + + Supervisor.init(children, strategy: :one_for_all) + end +end diff --git a/lib/teiserver/startup.ex b/lib/teiserver/startup.ex index 125b9c55a..0e95226cf 100644 --- a/lib/teiserver/startup.ex +++ b/lib/teiserver/startup.ex @@ -10,8 +10,6 @@ defmodule Teiserver.Startup do Teiserver.TeiserverConfigs.teiserver_configs() - Teiserver.Communication.build_text_callback_cache() - Teiserver.LobbyIdServer.start_lobby_id_server() Teiserver.Tachyon.CommandDispatch.build_dispatch_cache() From b1497b8ccb5fcda46e9e16d80fea0f62434acfbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Mon, 20 May 2024 20:00:29 +0100 Subject: [PATCH 08/14] Move teiserver site config to supervisors --- lib/teiserver/config/site_config_types/cache.ex | 3 ++- lib/teiserver/startup.ex | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/teiserver/config/site_config_types/cache.ex b/lib/teiserver/config/site_config_types/cache.ex index e67029563..d870a73f6 100644 --- a/lib/teiserver/config/site_config_types/cache.ex +++ b/lib/teiserver/config/site_config_types/cache.ex @@ -8,7 +8,8 @@ defmodule Teiserver.Config.SiteConfigTypes.Cache do def start_link(opts) do with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts), - :ok <- Teiserver.Config.SiteConfigTypes.SystemConfigs.create() do + :ok <- Teiserver.Config.SiteConfigTypes.SystemConfigs.create(), + :ok <- Teiserver.TeiserverConfigs.teiserver_configs() do {:ok, sup} end end diff --git a/lib/teiserver/startup.ex b/lib/teiserver/startup.ex index 0e95226cf..42c3d77fb 100644 --- a/lib/teiserver/startup.ex +++ b/lib/teiserver/startup.ex @@ -8,8 +8,6 @@ defmodule Teiserver.Startup do def startup do start_time = System.system_time(:millisecond) - Teiserver.TeiserverConfigs.teiserver_configs() - Teiserver.LobbyIdServer.start_lobby_id_server() Teiserver.Tachyon.CommandDispatch.build_dispatch_cache() From bb90256da2942dfeeda990d7274cd5010d95bffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Mon, 20 May 2024 20:09:45 +0100 Subject: [PATCH 09/14] Warm up metadata cache with audit type --- lib/teiserver/metadata_cache.ex | 35 ++++++++++++++++++++++++++++++++- lib/teiserver/startup.ex | 30 ---------------------------- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/lib/teiserver/metadata_cache.ex b/lib/teiserver/metadata_cache.ex index ce18dbb04..6250c9c70 100644 --- a/lib/teiserver/metadata_cache.ex +++ b/lib/teiserver/metadata_cache.ex @@ -8,7 +8,8 @@ defmodule Teiserver.MetadataCache do def start_link(opts) do with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts), - :ok <- random_names() do + :ok <- random_names(), + :ok <- audit() do {:ok, sup} end end @@ -47,4 +48,36 @@ defmodule Teiserver.MetadataCache do ~w(grunt graverobber aggravator trasher thug bedbug deceiver augur spectre fiend twitcher duck skuttle sumo arbiter manticore termite commando mammoth shiva karganeth catapult behemoth juggernaught) ) end + + defp audit() do + Teiserver.Logging.AuditLogLib.add_audit_types([ + "Account:User password reset", + "Account:Failed login", + "Account:Created user", + "Account:Updated user", + "Account:Updated user permissions", + "Account:User registration", + "Account:Updated report", + "Site config:Update value", + "Moderation:Ban enabled", + "Moderation:Ban disabled", + "Moderation:Ban updated", + "Moderation:Ban enacted", + "Moderation:Action deleted", + "Moderation:Action halted", + "Moderation:Action re_posted", + "Moderation:Action updated", + "Moderation:Action created", + "Moderation:De-bridged user", + "Moderation:Mark as smurf", + "Teiserver:Updated automod action", + "Teiserver:Automod action enacted", + "Teiserver:De-bridged user", + "Teiserver:Changed user rating", + "Teiserver:Changed user name", + "Teiserver:Smurf merge", + "Microblog.delete_post", + "Discord.text_callback" + ]) + end end diff --git a/lib/teiserver/startup.ex b/lib/teiserver/startup.ex index 42c3d77fb..c25d57b56 100644 --- a/lib/teiserver/startup.ex +++ b/lib/teiserver/startup.ex @@ -17,36 +17,6 @@ defmodule Teiserver.Startup do Teiserver.Bridge.CommandLib.cache_discord_commands() Teiserver.Communication.pre_cache_discord_channels() - add_audit_types([ - "Account:User password reset", - "Account:Failed login", - "Account:Created user", - "Account:Updated user", - "Account:Updated user permissions", - "Account:User registration", - "Account:Updated report", - "Site config:Update value", - "Moderation:Ban enabled", - "Moderation:Ban disabled", - "Moderation:Ban updated", - "Moderation:Ban enacted", - "Moderation:Action deleted", - "Moderation:Action halted", - "Moderation:Action re_posted", - "Moderation:Action updated", - "Moderation:Action created", - "Moderation:De-bridged user", - "Moderation:Mark as smurf", - "Teiserver:Updated automod action", - "Teiserver:Automod action enacted", - "Teiserver:De-bridged user", - "Teiserver:Changed user rating", - "Teiserver:Changed user name", - "Teiserver:Smurf merge", - "Microblog.delete_post", - "Discord.text_callback" - ]) - Teiserver.cache_put(:lists, :rooms, []) Teiserver.cache_put(:lists, :lobby_policies, []) From 8cd5da232dcd8170756e6ba83eb4db3e4ce7109d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Mon, 20 May 2024 20:15:34 +0100 Subject: [PATCH 10/14] Tachyon caches to supervisors --- lib/teiserver/application.ex | 5 +- .../protocols/tachyon_json_ws/cache.ex | 28 +++++++++++ .../tachyon_json_ws/tachyon_schema.ex | 47 ++++++++++--------- lib/teiserver/startup.ex | 3 -- 4 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 lib/teiserver/protocols/tachyon_json_ws/cache.ex diff --git a/lib/teiserver/application.ex b/lib/teiserver/application.ex index 6be7cf021..5fe9dfdb0 100644 --- a/lib/teiserver/application.ex +++ b/lib/teiserver/application.ex @@ -55,10 +55,7 @@ defmodule Teiserver.Application do Teiserver.MetadataCache, concache_sup(:application_temp_cache), concache_sup(:config_user_cache), - - # Tachyon schemas - concache_perm_sup(:tachyon_schemas), - concache_perm_sup(:tachyon_dispatches), + Teiserver.Tachyon.Cache, # Teiserver stuff # Global/singleton registries diff --git a/lib/teiserver/protocols/tachyon_json_ws/cache.ex b/lib/teiserver/protocols/tachyon_json_ws/cache.ex new file mode 100644 index 000000000..d4b31586d --- /dev/null +++ b/lib/teiserver/protocols/tachyon_json_ws/cache.ex @@ -0,0 +1,28 @@ +defmodule Teiserver.Tachyon.Cache do + @moduledoc """ + Cache for tachyon schemas and dispatchers + """ + + use Supervisor + alias Teiserver.Helpers.CacheHelper + + def start_link(opts) do + with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts), + {:ok, _schemas} <- Teiserver.Tachyon.Schema.load_schemas(), + :ok <- Teiserver.Tachyon.CommandDispatch.build_dispatch_cache() do + {:ok, sup} + end + end + + @impl true + def init(:ok) do + children = [ + CacheHelper.concache_perm_sup(:tachyon_schemas), + CacheHelper.concache_perm_sup(:tachyon_dispatches) + ] + + # we could have schema and dispatches under their own supervision tree each + # and their own init, but this is simpler + Supervisor.init(children, strategy: :one_for_all) + end +end diff --git a/lib/teiserver/protocols/tachyon_json_ws/tachyon_schema.ex b/lib/teiserver/protocols/tachyon_json_ws/tachyon_schema.ex index f57abb105..36edba7c1 100644 --- a/lib/teiserver/protocols/tachyon_json_ws/tachyon_schema.ex +++ b/lib/teiserver/protocols/tachyon_json_ws/tachyon_schema.ex @@ -5,28 +5,31 @@ defmodule Teiserver.Tachyon.Schema do @spec load_schemas :: list def load_schemas() do - Application.get_env(:teiserver, Teiserver)[:tachyon_schema_path] - |> Path.wildcard() - |> Enum.map(fn file_path -> - contents = - file_path - |> File.read!() - |> Jason.decode!() - - command = - file_path - |> Path.split() - |> Enum.reverse() - |> Enum.take(3) - |> Enum.reverse() - |> Enum.join("/") - |> String.replace(".json", "") - - schema = JsonXema.new(contents) - - Teiserver.store_put(:tachyon_schemas, command, schema) - command - end) + schemas = + Application.get_env(:teiserver, Teiserver)[:tachyon_schema_path] + |> Path.wildcard() + |> Enum.map(fn file_path -> + contents = + file_path + |> File.read!() + |> Jason.decode!() + + command = + file_path + |> Path.split() + |> Enum.reverse() + |> Enum.take(3) + |> Enum.reverse() + |> Enum.join("/") + |> String.replace(".json", "") + + schema = JsonXema.new(contents) + + Teiserver.store_put(:tachyon_schemas, command, schema) + command + end) + + {:ok, schemas} end @spec validate!(map) :: :ok diff --git a/lib/teiserver/startup.ex b/lib/teiserver/startup.ex index c25d57b56..cda55d1ad 100644 --- a/lib/teiserver/startup.ex +++ b/lib/teiserver/startup.ex @@ -10,9 +10,6 @@ defmodule Teiserver.Startup do Teiserver.LobbyIdServer.start_lobby_id_server() - Teiserver.Tachyon.CommandDispatch.build_dispatch_cache() - Teiserver.Tachyon.Schema.load_schemas() - Teiserver.Lobby.CommandLib.cache_lobby_commands() Teiserver.Bridge.CommandLib.cache_discord_commands() Teiserver.Communication.pre_cache_discord_channels() From b4958b67eb64169648234fbf692b452b29dbb90d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Mon, 20 May 2024 20:20:35 +0100 Subject: [PATCH 11/14] Lobby command cache --- lib/teiserver/application.ex | 2 +- lib/teiserver/lobby/libs/cache.ex | 24 ++++++++++++++++++++++++ lib/teiserver/startup.ex | 1 - 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 lib/teiserver/lobby/libs/cache.ex diff --git a/lib/teiserver/application.ex b/lib/teiserver/application.ex index 5fe9dfdb0..3e98fb6fd 100644 --- a/lib/teiserver/application.ex +++ b/lib/teiserver/application.ex @@ -133,7 +133,7 @@ defmodule Teiserver.Application do concache_perm_sup(:discord_command_cache), # Lobbies - concache_perm_sup(:lobby_command_cache), + Teiserver.Lobby.Cache, {DynamicSupervisor, strategy: :one_for_one, name: Teiserver.LobbySupervisor}, {DynamicSupervisor, strategy: :one_for_one, name: Teiserver.ClientSupervisor}, {DynamicSupervisor, strategy: :one_for_one, name: Teiserver.PartySupervisor}, diff --git a/lib/teiserver/lobby/libs/cache.ex b/lib/teiserver/lobby/libs/cache.ex new file mode 100644 index 000000000..9626ba664 --- /dev/null +++ b/lib/teiserver/lobby/libs/cache.ex @@ -0,0 +1,24 @@ +defmodule Teiserver.Lobby.Cache do + @moduledoc """ + Cache for lobby cache + """ + + use Supervisor + alias Teiserver.Helpers.CacheHelper + + def start_link(opts) do + with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts), + :ok <- Teiserver.Lobby.CommandLib.cache_lobby_commands() do + {:ok, sup} + end + end + + @impl true + def init(:ok) do + children = [ + CacheHelper.concache_perm_sup(:lobby_command_cache) + ] + + Supervisor.init(children, strategy: :one_for_one) + end +end diff --git a/lib/teiserver/startup.ex b/lib/teiserver/startup.ex index cda55d1ad..ec3503302 100644 --- a/lib/teiserver/startup.ex +++ b/lib/teiserver/startup.ex @@ -10,7 +10,6 @@ defmodule Teiserver.Startup do Teiserver.LobbyIdServer.start_lobby_id_server() - Teiserver.Lobby.CommandLib.cache_lobby_commands() Teiserver.Bridge.CommandLib.cache_discord_commands() Teiserver.Communication.pre_cache_discord_channels() From c8720d7646e0620cb17c8318a6c4d688b1612e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Tue, 21 May 2024 20:17:12 +0100 Subject: [PATCH 12/14] Matchmaking caches --- lib/teiserver/application.ex | 3 +-- lib/teiserver/data/matchmaking_cache.ex | 28 +++++++++++++++++++++++++ lib/teiserver/startup.ex | 4 ---- 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 lib/teiserver/data/matchmaking_cache.ex diff --git a/lib/teiserver/application.ex b/lib/teiserver/application.ex index 3e98fb6fd..928b4bfec 100644 --- a/lib/teiserver/application.ex +++ b/lib/teiserver/application.ex @@ -77,7 +77,6 @@ defmodule Teiserver.Application do # Stores - Tables where changes are not propagated across the cluster # Possible stores - concache_perm_sup(:teiserver_queues), concache_perm_sup(:lobby_policies_cache), # Telemetry @@ -96,7 +95,7 @@ defmodule Teiserver.Application do # Caches # Caches - Meta - concache_perm_sup(:lists), + Teiserver.Data.MatchmakingCache, # Caches - User # concache_sup(:users_lookup_name_with_id, [global_ttl: 300_000]), diff --git a/lib/teiserver/data/matchmaking_cache.ex b/lib/teiserver/data/matchmaking_cache.ex new file mode 100644 index 000000000..cd4545751 --- /dev/null +++ b/lib/teiserver/data/matchmaking_cache.ex @@ -0,0 +1,28 @@ +defmodule Teiserver.Data.MatchmakingCache do + @moduledoc """ + Define cache for matchmaking and set it up + """ + + use Supervisor + alias Teiserver.Helpers.CacheHelper + + def start_link(opts) do + with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts) do + Teiserver.cache_put(:lists, :rooms, []) + Teiserver.cache_put(:lists, :lobby_policies, []) + Teiserver.Data.Matchmaking.pre_cache_queues() + + {:ok, sup} + end + end + + @impl true + def init(:ok) do + children = [ + CacheHelper.concache_perm_sup(:teiserver_queues), + CacheHelper.concache_perm_sup(:lists) + ] + + Supervisor.init(children, strategy: :one_for_all) + end +end diff --git a/lib/teiserver/startup.ex b/lib/teiserver/startup.ex index ec3503302..c33f75d15 100644 --- a/lib/teiserver/startup.ex +++ b/lib/teiserver/startup.ex @@ -13,10 +13,6 @@ defmodule Teiserver.Startup do Teiserver.Bridge.CommandLib.cache_discord_commands() Teiserver.Communication.pre_cache_discord_channels() - Teiserver.cache_put(:lists, :rooms, []) - Teiserver.cache_put(:lists, :lobby_policies, []) - - Teiserver.Data.Matchmaking.pre_cache_queues() Teiserver.Game.pre_cache_policies() # Add in achievements From 143a6317335cd0e0f7bc3fe978d81c29af8d26f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Tue, 21 May 2024 20:23:42 +0100 Subject: [PATCH 13/14] Lobby policies --- lib/teiserver/application.ex | 2 +- lib/teiserver/game/libs/lobby_policy_cache.ex | 24 +++++++++++++++++++ lib/teiserver/startup.ex | 2 -- 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 lib/teiserver/game/libs/lobby_policy_cache.ex diff --git a/lib/teiserver/application.ex b/lib/teiserver/application.ex index 928b4bfec..d80cd74f5 100644 --- a/lib/teiserver/application.ex +++ b/lib/teiserver/application.ex @@ -77,7 +77,7 @@ defmodule Teiserver.Application do # Stores - Tables where changes are not propagated across the cluster # Possible stores - concache_perm_sup(:lobby_policies_cache), + Teiserver.Data.LobbyPolicyCache, # Telemetry concache_perm_sup(:telemetry_property_types_cache), diff --git a/lib/teiserver/game/libs/lobby_policy_cache.ex b/lib/teiserver/game/libs/lobby_policy_cache.ex new file mode 100644 index 000000000..79349e2ea --- /dev/null +++ b/lib/teiserver/game/libs/lobby_policy_cache.ex @@ -0,0 +1,24 @@ +defmodule Teiserver.Data.LobbyPolicyCache do + @moduledoc """ + Define cache for lobby policies and set it up + """ + + use Supervisor + alias Teiserver.Helpers.CacheHelper + + def start_link(opts) do + with {:ok, sup} <- Supervisor.start_link(__MODULE__, :ok, opts) do + Teiserver.Game.pre_cache_policies() + {:ok, sup} + end + end + + @impl true + def init(:ok) do + children = [ + CacheHelper.concache_perm_sup(:lobby_policies_cache) + ] + + Supervisor.init(children, strategy: :one_for_one) + end +end diff --git a/lib/teiserver/startup.ex b/lib/teiserver/startup.ex index c33f75d15..206c16753 100644 --- a/lib/teiserver/startup.ex +++ b/lib/teiserver/startup.ex @@ -13,8 +13,6 @@ defmodule Teiserver.Startup do Teiserver.Bridge.CommandLib.cache_discord_commands() Teiserver.Communication.pre_cache_discord_channels() - Teiserver.Game.pre_cache_policies() - # Add in achievements Teiserver.Game.GenerateAchievementTypes.perform() From 24702e5572c4aece865514bd7dbe824aa6493778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Sat, 6 Jul 2024 18:07:34 +0100 Subject: [PATCH 14/14] fix dialyzer --- lib/teiserver/helpers/oban_logger.ex | 2 +- lib/teiserver/protocols/tachyon_json_ws/tachyon_schema.ex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/teiserver/helpers/oban_logger.ex b/lib/teiserver/helpers/oban_logger.ex index a5ddf5135..c4e1db3b1 100644 --- a/lib/teiserver/helpers/oban_logger.ex +++ b/lib/teiserver/helpers/oban_logger.ex @@ -40,6 +40,6 @@ defmodule Teiserver.Helper.ObanLogger do end def handle_event([:oban, :job, event], measure, meta, _) do - Logger.info("[Oban] #{event} #{meta.worker} ran in #{System.convert_time_unit(measure.duration, :native, :milliseconds)}ms") + Logger.info("[Oban] #{event} #{meta.worker} ran in #{System.convert_time_unit(measure.duration, :native, :millisecond)}ms") end end diff --git a/lib/teiserver/protocols/tachyon_json_ws/tachyon_schema.ex b/lib/teiserver/protocols/tachyon_json_ws/tachyon_schema.ex index 36edba7c1..642352cb6 100644 --- a/lib/teiserver/protocols/tachyon_json_ws/tachyon_schema.ex +++ b/lib/teiserver/protocols/tachyon_json_ws/tachyon_schema.ex @@ -3,7 +3,7 @@ defmodule Teiserver.Tachyon.Schema do """ - @spec load_schemas :: list + @spec load_schemas :: {:ok, list} def load_schemas() do schemas = Application.get_env(:teiserver, Teiserver)[:tachyon_schema_path]