Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
ScriptV2: BunnyCDN cache purging (#5435)
Karl-Aksel Puulmann6a06020ba7c0
diff --git a/config/runtime.exs b/config/runtime.exsindex ea15378846..b4119ce2fb 100644--- a/config/runtime.exs+++ b/config/runtime.exs@@ -786,7 +786,8 @@ cloud_queues = [check_usage: 1,notify_annual_renewal: 1,lock_sites: 1,- legacy_time_on_page_cutoff: 1+ legacy_time_on_page_cutoff: 1,+ purge_cdn_cache: 1]queues = if(is_selfhost, do: base_queues, else: base_queues ++ cloud_queues)@@ -828,6 +829,10 @@ config :plausible, Plausible.Sentry.Client,receive_timeout: get_int_from_path_or_env(config_dir, "SENTRY_FINCH_RECEIVE_TIMEOUT", 15000)]+config :plausible, Plausible.Workers.PurgeCDNCache,+ pullzone_id: get_var_from_path_or_env(config_dir, "BUNNY_PULLZONE_ID"),+ api_key: get_var_from_path_or_env(config_dir, "BUNNY_API_KEY")+config :ref_inspector,init: {Plausible.Release, :configure_ref_inspector}diff --git a/lib/plausible_web/tracker.ex b/lib/plausible_web/tracker.exindex fe03b55137..c74f8e6907 100644--- a/lib/plausible_web/tracker.ex+++ b/lib/plausible_web/tracker.ex@@ -3,6 +3,7 @@ defmodule PlausibleWeb.Tracker doHelper module for building the dynamic tracker script. Used by PlausibleWeb.TrackerPlug."""+ use Plausibleuse Plausible.Repoalias Plausible.Site.TrackerScriptConfiguration@@ -50,6 +51,16 @@ defmodule PlausibleWeb.Tracker dosync_goals(site, original_config, updated_config)+ on_ee do+ Plausible.Workers.PurgeCDNCache.new(+ %{id: updated_config.id},+ # See PurgeCDNCache.ex for more details+ schedule_in: 10,+ replace: [scheduled: [:scheduled_at]]+ )+ |> Oban.insert!()+ end+updated_configenddiff --git a/lib/workers/purge_cdn_cache.ex b/lib/workers/purge_cdn_cache.exnew file mode 100644index 0000000000..a82210e658--- /dev/null+++ b/lib/workers/purge_cdn_cache.ex@@ -0,0 +1,71 @@+defmodule Plausible.Workers.PurgeCDNCache do+ @moduledoc """+ Worker for purging CDN cache for tracker scripts on cloud.++ Uses Bunny CDN's API to purge cache by tag.+ Docs ref: https://docs.bunny.net/reference/pullzonepublic_purgecachepostbytag++ Note that purging by id "*" is equivalent to purging ALL cache.+ """++ use Oban.Worker,+ queue: :purge_cdn_cache,+ max_attempts: 5,+ # To avoid running into API rate limits, we:+ # - Schedule jobs with a delay+ # - Bump the scheduled time every time a new one is scheduled with the same args+ unique: [+ states: [:scheduled],+ fields: [:args]+ ]++ require Logger++ @impl Oban.Worker+ def perform(%Oban.Job{args: %{"id" => id}}) do+ pullzone_id = Application.get_env(:plausible, __MODULE__, [])[:pullzone_id]+ api_key = Application.get_env(:plausible, __MODULE__, [])[:api_key]++ purge_cache(id, pullzone_id, api_key)+ end++ @impl Oban.Worker+ def backoff(%Oban.Job{attempt: attempt}) do+ # Exponential backoff starting at 3 minutes+ trunc(:math.pow(2, attempt - 1) * 180)+ end++ defp purge_cache(id, pullzone_id, api_key) when is_nil(pullzone_id) or is_nil(api_key) do+ Logger.warning("Ignoring purge CDN cache for tracker script #{id}: Configuration missing")+ {:discard, "Configuration missing"}+ end++ defp purge_cache(id, pullzone_id, api_key) do+ options =+ [+ headers: [+ {"content-type", "application/json"},+ {"AccessKey", api_key}+ ],+ body: Jason.encode!(%{"CacheTag" => "tracker_script::#{id}"})+ ]+ |> Keyword.merge(Application.get_env(:plausible, __MODULE__)[:req_opts] || [])++ case Req.post("https://api.bunny.net/pullzone/#{pullzone_id}/purgeCache", options) do+ {:ok, %{status: 204}} ->+ Logger.info("Successfully purged CDN cache for tracker script #{id}")+ {:ok, :success}++ {:ok, %{status: status}} ->+ Logger.warning(+ "Failed to purge CDN cache for tracker script #{id}: Unexpected status: #{status}"+ )++ {:error, "Unexpected status: #{status}"}++ {:error, reason} ->+ Logger.warning("Failed to purge CDN cache for tracker script #{id}: #{inspect(reason)}")+ {:error, reason}+ end+ end+enddiff --git a/test/workers/purge_cdn_cache_test.exs b/test/workers/purge_cdn_cache_test.exsnew file mode 100644index 0000000000..aa87ab69f3--- /dev/null+++ b/test/workers/purge_cdn_cache_test.exs@@ -0,0 +1,77 @@+defmodule Plausible.Workers.PurgeCDNCacheTest do+ use Plausible.DataCase, async: true+ import ExUnit.CaptureLog+ import Plug.Conn++ alias Plausible.Workers.PurgeCDNCache++ describe "perform/1" do+ setup do+ # Set test configuration+ Application.put_env(:plausible, Plausible.Workers.PurgeCDNCache,+ pullzone_id: "456",+ api_key: "test-api-key",+ req_opts: [+ plug: {Req.Test, Plausible.Workers.PurgeCDNCache}+ ]+ )++ :ok+ end++ test "successfully purges cache" do+ Req.Test.expect(Plausible.Workers.PurgeCDNCache, &send_resp(&1, 204, "ok"))++ assert {:ok, :success} = PurgeCDNCache.perform(%Oban.Job{args: %{"id" => "adf"}})+ end++ test "handles missing configuration" do+ # Clear configuration+ Application.put_env(:plausible, Plausible.Workers.PurgeCDNCache, [])++ {result, log} =+ with_log(fn -> PurgeCDNCache.perform(%Oban.Job{args: %{"id" => "adf"}}) end)++ assert {:discard, "Configuration missing"} = result+ assert log =~ "Ignoring purge CDN cache for tracker script adf: Configuration missing"+ end++ test "handles unexpected status code" do+ Req.Test.expect(+ Plausible.Workers.PurgeCDNCache,+ &send_resp(&1, 500, "internal server error")+ )++ {result, log} =+ with_log(fn -> PurgeCDNCache.perform(%Oban.Job{args: %{"id" => "adf"}}) end)++ assert {:error, "Unexpected status: 500"} = result+ assert log =~ "Failed to purge CDN cache for tracker script adf: Unexpected status: 500"+ end++ test "handles network errors" do+ Req.Test.expect(+ Plausible.Workers.PurgeCDNCache,+ &Req.Test.transport_error(&1, :econnrefused)+ )++ {result, log} =+ with_log(fn -> PurgeCDNCache.perform(%Oban.Job{args: %{"id" => "adf"}}) end)++ assert {:error, %Req.TransportError{reason: :econnrefused}} = result++ assert log =~+ "Failed to purge CDN cache for tracker script adf: %Req.TransportError{reason: :econnrefused}"+ end+ end++ describe "backoff/1" do+ test "implements exponential backoff starting at 3 minutes" do+ assert PurgeCDNCache.backoff(%Oban.Job{attempt: 1}) == 180+ assert PurgeCDNCache.backoff(%Oban.Job{attempt: 2}) == 360+ assert PurgeCDNCache.backoff(%Oban.Job{attempt: 3}) == 720+ assert PurgeCDNCache.backoff(%Oban.Job{attempt: 4}) == 1440+ assert PurgeCDNCache.backoff(%Oban.Job{attempt: 5}) == 2880+ end+ end+end