From 18ddf1e7a05bf9fc6f765621b55c5698f73b29ed Mon Sep 17 00:00:00 2001 From: hq1 Date: Mon, 29 Jan 2024 13:37:30 +0100 Subject: [PATCH] Re enable accept traffic until notifications (#3737) * Revert "Remove 0-sized queue" This reverts commit 6d229b3f70cf567c3df0abfe4de7bc1c882b0355. * Revert "Temporarily remove notification cron worker (#3732)" This reverts commit 447e3929cb2d4964cd79ea00617271c8f0cffd85. * Revert "Temporarily disable accept_traffic_until actions (#3730)" This reverts commit 58afe3376f248a8746b5e2b91f33ac072c0d9fa1. * Send accept_traffic_until notifications at 8 UTC rather than 5 * Add option for dry-run * Expose dry_run in a dedicated function --- config/runtime.exs | 7 +++-- lib/plausible/site/gate_keeper.ex | 13 ++++++-- .../accept_traffic_until_notification.ex | 30 +++++++++++++------ test/plausible/ingestion/event_test.exs | 1 - test/plausible/site/gate_keeper_test.exs | 1 - 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/config/runtime.exs b/config/runtime.exs index b9afa18e2..94e826d0d 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -508,7 +508,9 @@ cloud_cron = [ # Daily at 15 {"0 15 * * *", Plausible.Workers.NotifyAnnualRenewal}, # Every midnight - {"0 0 * * *", Plausible.Workers.LockSites} + {"0 0 * * *", Plausible.Workers.LockSites}, + # Daily at 8 + {"0 8 * * *", Plausible.Workers.AcceptTrafficUntil} ] crontab = if(is_selfhost, do: base_cron, else: base_cron ++ cloud_cron) @@ -524,7 +526,8 @@ base_queues = [ # NOTE: to be removed once #3700 is released google_analytics_imports: 1, analytics_imports: 1, - domain_change_transition: 1 + domain_change_transition: 1, + check_accept_traffic_until: 1 ] cloud_queues = [ diff --git a/lib/plausible/site/gate_keeper.ex b/lib/plausible/site/gate_keeper.ex index 48f8eced5..e552f1637 100644 --- a/lib/plausible/site/gate_keeper.ex +++ b/lib/plausible/site/gate_keeper.ex @@ -43,10 +43,17 @@ defmodule Plausible.Site.GateKeeper do end defp policy(domain, opts) when is_binary(domain) do - if site = Cache.get(domain, Keyword.get(opts, :cache_opts, [])) do - check_rate_limit(site, opts) + with from_cache <- Cache.get(domain, Keyword.get(opts, :cache_opts, [])), + site = %Site{owner: %{accept_traffic_until: accept_traffic_until}} <- from_cache do + if not is_nil(accept_traffic_until) and + Date.after?(Date.utc_today(), accept_traffic_until) do + :payment_required + else + check_rate_limit(site, opts) + end else - @policy_for_non_existing_sites + _ -> + @policy_for_non_existing_sites end end diff --git a/lib/workers/accept_traffic_until_notification.ex b/lib/workers/accept_traffic_until_notification.ex index 7d6890996..d7327f09d 100644 --- a/lib/workers/accept_traffic_until_notification.ex +++ b/lib/workers/accept_traffic_until_notification.ex @@ -16,8 +16,12 @@ defmodule Plausible.Workers.AcceptTrafficUntil do alias Plausible.Repo alias Plausible.ClickhouseRepo + def dry_run(date) do + perform(nil, date, true) + end + @impl Oban.Worker - def perform(_job, today \\ Date.utc_today()) do + def perform(_job, today \\ Date.utc_today(), dry_run? \\ false) do tomorrow = today |> Date.add(+1) next_week = today |> Date.add(+7) @@ -49,16 +53,24 @@ defmodule Plausible.Workers.AcceptTrafficUntil do for notification <- notifications do case {has_stats?(notification.site_ids, today), notification.deadline} do {true, ^tomorrow} -> - notification - |> store_sent(today) - |> PlausibleWeb.Email.approaching_accept_traffic_until_tomorrow() - |> Plausible.Mailer.send() + if dry_run? do + IO.puts("Will send final notification to #{notification.email}") + else + notification + |> store_sent(today) + |> PlausibleWeb.Email.approaching_accept_traffic_until_tomorrow() + |> Plausible.Mailer.send() + end {true, ^next_week} -> - notification - |> store_sent(today) - |> PlausibleWeb.Email.approaching_accept_traffic_until() - |> Plausible.Mailer.send() + if dry_run? do + IO.puts("Will send weekly notification to #{notification.email}") + else + notification + |> store_sent(today) + |> PlausibleWeb.Email.approaching_accept_traffic_until() + |> Plausible.Mailer.send() + end _ -> nil diff --git a/test/plausible/ingestion/event_test.exs b/test/plausible/ingestion/event_test.exs index 5d82cb030..8388a4f6b 100644 --- a/test/plausible/ingestion/event_test.exs +++ b/test/plausible/ingestion/event_test.exs @@ -117,7 +117,6 @@ defmodule Plausible.Ingestion.EventTest do assert dropped.drop_reason == :dc_ip end - @tag :skip test "event pipeline drops events for site with accept_trafic_until in the past" do yesterday = Date.add(Date.utc_today(), -1) diff --git a/test/plausible/site/gate_keeper_test.exs b/test/plausible/site/gate_keeper_test.exs index d6372d5ff..13b590968 100644 --- a/test/plausible/site/gate_keeper_test.exs +++ b/test/plausible/site/gate_keeper_test.exs @@ -14,7 +14,6 @@ defmodule Plausible.Site.GateKeeperTest do assert {:deny, :not_found} = GateKeeper.check("example.com", opts) end - @tag :skip test "sites with accepted_traffic_until < now are denied", %{test: test, opts: opts} do domain = "expired.example.com" yesterday = Date.utc_today() |> Date.add(-1)