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
Schedule regular jobs with Oban (#69)
Uku Taht25d8f2f3f9ec
diff --git a/.gitignore b/.gitignoreindex 7030c1061f..a5df679f7d 100644--- a/.gitignore+++ b/.gitignore@@ -44,3 +44,4 @@ npm-debug.log# Ignore Elixir Language Server files.elixir_ls+plausible-report.xmldiff --git a/config/config.exs b/config/config.exsindex eaebd6492a..fe6276fc26 100644--- a/config/config.exs+++ b/config/config.exs@@ -71,8 +71,7 @@ config :plausible, :paddle,vendor_id: "49430",vendor_auth_code: System.get_env("PADDLE_VENDOR_AUTH_CODE")-config :plausible,- Plausible.Repo,+config :plausible, Plausible.Repo,pool_size: String.to_integer(System.get_env("DATABASE_POOL_SIZE", "10")),timeout: 300_000,connect_timeout: 300_000,@@ -80,10 +79,29 @@ config :plausible,url:System.get_env("DATABASE_URL",- "postgres://postgres:[email protected]:5432/plausible_test?currentSchema=default"+ "postgres://postgres:[email protected]:5432/plausible_dev?currentSchema=default"),ssl: String.to_existing_atom(System.get_env("DATABASE_TLS_ENABLED", "false"))++crontab = if String.to_existing_atom(System.get_env("CRON_ENABLED", "false")) do+ [+ {"@hourly", Plausible.Workers.SendSiteSetupEmails},+ {"@hourly", Plausible.Workers.SendEmailReports},+ {"@daily", Plausible.Workers.FetchTweets},+ {"0 12 * * *", Plausible.Workers.SendTrialNotifications}, # Daily at midday+ {"0 12 * * *", Plausible.Workers.SendCheckStatsEmails}, # Daily at midday+ {"* /10 * * *", Plausible.Workers.ProvisionSslCertificates}, # Every 10 minutes+ ]+else+ false+end++config :plausible, Oban,+ repo: Plausible.Repo,+ queues: [default: 10],+ crontab: crontab+config :plausible, :google,client_id: System.get_env("GOOGLE_CLIENT_ID"),client_secret: System.get_env("GOOGLE_CLIENT_SECRET")diff --git a/config/test.exs b/config/test.exsindex eae3c2c116..8bf4d7310c 100644--- a/config/test.exs+++ b/config/test.exs@@ -15,10 +15,17 @@ config :bcrypt_elixir, :log_rounds, 4# Configure your databaseconfig :plausible,Plausible.Repo,+ url:+ System.get_env(+ "DATABASE_URL",+ "postgres://postgres:[email protected]:5432/plausible_test=default"+ ),pool: Ecto.Adapters.SQL.Sandboxconfig :plausible, Plausible.Mailer, adapter: Bamboo.TestAdapter+config :plausible, Oban, crontab: false, queues: false+config :plausible,paddle_api: Plausible.PaddleApi.Mock,google_api: Plausible.Google.Api.Mockdiff --git a/lib/mix/tasks/hydrate_clickhouse.ex b/lib/mix/tasks/hydrate_clickhouse.exindex ff687ac135..0a0f03c294 100644--- a/lib/mix/tasks/hydrate_clickhouse.ex+++ b/lib/mix/tasks/hydrate_clickhouse.ex@@ -4,6 +4,8 @@ defmodule Mix.Tasks.HydrateClickhouse dorequire Logger@hash_key Keyword.fetch!(Application.get_env(:plausible, PlausibleWeb.Endpoint), :secret_key_base) |> binary_part(0, 16)+ # coveralls-ignore-start+def run(args) doApplication.ensure_all_started(:db_connection)Application.ensure_all_started(:hackney)diff --git a/lib/mix/tasks/send_trial_notifications.ex b/lib/mix/tasks/send_trial_notifications.exdeleted file mode 100644index 41b844f29b..0000000000--- a/lib/mix/tasks/send_trial_notifications.ex+++ /dev/null@@ -1,88 +0,0 @@-defmodule Mix.Tasks.SendTrialNotifications do- use Mix.Task- use Plausible.Repo- require Logger-- @doc """- This is scheduled to run every day.- """-- def run(args) do- Application.ensure_all_started(:plausible)- execute(args)- end-- def execute(args \\ []) do- base_query =- from(u in Plausible.Auth.User,- left_join: s in Plausible.Billing.Subscription, on: s.user_id == u.id,- where: is_nil(s.id),- order_by: u.inserted_at- )-- users = Repo.all(base_query)-- for user <- users do- case Timex.diff(user.trial_expiry_date, Timex.today(), :days) do- 7 ->- if Plausible.Auth.user_completed_setup?(user) do- send_one_week_reminder(args, user)- end- 1 ->- if Plausible.Auth.user_completed_setup?(user) do- send_tomorrow_reminder(args, user)- end- 0 ->- if Plausible.Auth.user_completed_setup?(user) do- send_today_reminder(args, user)- end- -1 ->- if Plausible.Auth.user_completed_setup?(user) do- send_over_reminder(args, user)- end- _ ->- nil- end- end- end-- defp send_one_week_reminder(["--dry-run"], user) do- Logger.info("DRY RUN: one week trial notification email to #{user.name} [inserted=#{user.inserted_at}]")- end-- defp send_one_week_reminder(_, user) do- PlausibleWeb.Email.trial_one_week_reminder(user)- |> Plausible.Mailer.send_email()- end-- defp send_tomorrow_reminder(["--dry-run"], user) do- Logger.info("DRY RUN: tomorrow trial upgrade email to #{user.name} [inserted=#{user.inserted_at}]")- end-- defp send_tomorrow_reminder(_, user) do- usage = Plausible.Billing.usage(user)-- PlausibleWeb.Email.trial_upgrade_email(user, "tomorrow", usage)- |> Plausible.Mailer.send_email()- end-- defp send_today_reminder(["--dry-run"], user) do- Logger.info("DRY RUN: today trial upgrade email to #{user.name} [inserted=#{user.inserted_at}]")- end-- defp send_today_reminder(_, user) do- usage = Plausible.Billing.usage(user)-- PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)- |> Plausible.Mailer.send_email()- end-- defp send_over_reminder(["--dry-run"], user) do- Logger.info("DRY RUN: over trial notification email to #{user.name} [inserted=#{user.inserted_at}]")- end-- defp send_over_reminder(_, user) do- PlausibleWeb.Email.trial_over_email(user)- |> Plausible.Mailer.deliver_now()- end-enddiff --git a/lib/plausible/application.ex b/lib/plausible/application.exindex 11a8549260..4c49a5feff 100644--- a/lib/plausible/application.ex+++ b/lib/plausible/application.ex@@ -11,7 +11,8 @@ defmodule Plausible.Application doPlausible.Event.WriteBuffer,Plausible.Session.WriteBuffer,Clickhousex.child_spec(Keyword.merge([scheme: :http, port: 8123, name: :clickhouse], clickhouse_config)),- Plausible.Session.Store+ Plausible.Session.Store,+ {Oban, Application.get_env(:plausible, Oban)}]opts = [strategy: :one_for_one, name: Plausible.Supervisor]diff --git a/lib/mix/tasks/fetch_tweets.ex b/lib/workers/fetch_tweets.exsimilarity index 93%rename from lib/mix/tasks/fetch_tweets.exrename to lib/workers/fetch_tweets.exindex c4fa3203c3..a0f1778e97 100644--- a/lib/mix/tasks/fetch_tweets.ex+++ b/lib/workers/fetch_tweets.ex@@ -1,14 +1,11 @@-defmodule Mix.Tasks.FetchTweets do+defmodule Plausible.Workers.FetchTweets douse Plausible.Repo+ use Oban.Worker, queue: :fetch_tweetsalias Plausible.Twitter.Tweet@oauth_credentials Application.get_env(:plausible, :twitter, %{}) |> OAuther.credentials()- def run(_args) do- Application.ensure_all_started(:plausible)- execute()- end-- def execute() do+ @impl Oban.Worker+ def perform(_args, _job) donew_links = Repo.all(from e in Plausible.Event,where: e.timestamp > fragment("(now() - '6 days'::interval)") and e.timestamp < fragment("(now() - '5 days'::interval)"),@@ -36,6 +33,7 @@ defmodule Mix.Tasks.FetchTweets do}) |> Repo.insert!(on_conflict: :nothing)endend+ :okenddef html_body(tweet) dodiff --git a/lib/mix/tasks/obtain_ssl_certificates.ex b/lib/workers/provision_ssl_certificates.exsimilarity index 76%rename from lib/mix/tasks/obtain_ssl_certificates.exrename to lib/workers/provision_ssl_certificates.exindex 8cae652480..2ef0906f0a 100644--- a/lib/mix/tasks/obtain_ssl_certificates.ex+++ b/lib/workers/provision_ssl_certificates.ex@@ -1,12 +1,9 @@-defmodule Mix.Tasks.ObtainSslCertificates do+defmodule Plausible.Workers.ProvisionSslCertificates douse Plausible.Repo+ use Oban.Worker, queue: :provision_ssl_certificates- def run(_args) do- Application.ensure_all_started(:plausible)- execute()- end-- def execute(system \\ System) do+ @impl Oban.Worker+ def perform(_args, _job, system \\ System) dorecent_custom_domains = Repo.all(from cd in Plausible.Site.CustomDomain,where: cd.updated_at > fragment("now() - '3 days'::interval"),@@ -22,9 +19,11 @@ defmodule Mix.Tasks.ObtainSslCertificates dodefp report_result({_, 0}, domain) doEcto.Changeset.change(domain, has_ssl_certificate: true) |> Repo.update!Plausible.Slack.notify("Obtained SSL cert for #{domain.domain}")+ :okenddefp report_result({error_msg, error_code}, domain) doSentry.capture_message("Error obtaining SSL certificate", extra: %{error_msg: error_msg, error_code: error_code, domain: domain.domain})+ :ok # Failing to obtain is expected, not a failure for the job queueendenddiff --git a/lib/mix/tasks/send_check_stats_emails.ex b/lib/workers/send_check_stats_emails.exsimilarity index 66%rename from lib/mix/tasks/send_check_stats_emails.exrename to lib/workers/send_check_stats_emails.exindex 8532dd8d01..b449742197 100644--- a/lib/mix/tasks/send_check_stats_emails.ex+++ b/lib/workers/send_check_stats_emails.ex@@ -1,18 +1,9 @@-defmodule Mix.Tasks.SendCheckStatsEmails do- use Mix.Task+defmodule Plausible.Workers.SendCheckStatsEmails douse Plausible.Repo- require Logger+ use Oban.Worker, queue: :check_stats_emails- @doc """- This is scheduled to run daily.- """-- def run(args) do- Application.ensure_all_started(:plausible)- execute(args)- end-- def execute(args \\ []) do+ @impl Oban.Worker+ def perform(_args, _job) doq =from(u in Plausible.Auth.User,left_join: ce in "check_stats_emails", on: ce.user_id == u.id,@@ -28,16 +19,14 @@ defmodule Mix.Tasks.SendCheckStatsEmails doenabled_report = Enum.any?(user.sites, fn site -> site.weekly_report end)if Plausible.Auth.user_completed_setup?(user) && !enabled_report do- send_check_stats_email(args, user)+ send_check_stats_email(user)endend- end- defp send_check_stats_email(["--dry-run"], user) do- Logger.info("DRY RUN: check stats email to #{user.name}")+ :okend- defp send_check_stats_email(_, user) do+ defp send_check_stats_email(user) doPlausibleWeb.Email.check_stats_email(user)|> Plausible.Mailer.send_email()diff --git a/lib/mix/tasks/send_email_reports.ex b/lib/workers/send_email_reports.exsimilarity index 86%rename from lib/mix/tasks/send_email_reports.exrename to lib/workers/send_email_reports.exindex 3c1ab90f27..2c23a66926 100644--- a/lib/mix/tasks/send_email_reports.ex+++ b/lib/workers/send_email_reports.ex@@ -1,21 +1,22 @@-defmodule Mix.Tasks.SendEmailReports do- use Mix.Task+defmodule Plausible.Workers.SendEmailReports douse Plausible.Repo+ use Oban.Worker, queue: :email_reportsrequire Logger- alias Plausible.Stats-- def run(_args) do- Application.ensure_all_started(:plausible)- execute(Timex.now())- end+ alias Plausible.Stats.Query+ alias Plausible.Stats.Clickhouse, as: Stats+ @impl Oban.Worker@doc"""The email report should be sent on Monday at 9am according to the timezoneof the site. This job runs every hour to be able to send it with hourly precision."""- def execute(job_start) do- send_weekly_emails(job_start)- send_monthly_emails(job_start)+ def perform(args, _job) do+ current_time = if args["current_time"], do: Timex.parse!(args["current_time"], "{ISO:Extended}"), else: Timex.now()++ send_weekly_emails(current_time)+ send_monthly_emails(current_time)++ :okenddefp send_weekly_emails(job_start) do@@ -30,7 +31,7 @@ defmodule Mix.Tasks.SendEmailReports do)for site <- sites do- query = Stats.Query.from(site.timezone, %{"period" => "7d"})+ query = Query.from(site.timezone, %{"period" => "7d"})for email <- site.weekly_report.recipients doLogger.info("Sending weekly report for #{URI.encode_www_form(site.domain)} to #{email}")@@ -55,7 +56,7 @@ defmodule Mix.Tasks.SendEmailReports dofor site <- sites dolast_month = job_start |> Timex.Timezone.convert(site.timezone) |> Timex.shift(months: -1) |> Timex.beginning_of_month- query = Stats.Query.from(site.timezone, %{"period" => "month", "date" => Timex.format!(last_month, "{ISOdate}")})+ query = Query.from(site.timezone, %{"period" => "month", "date" => Timex.format!(last_month, "{ISOdate}")})for email <- site.monthly_report.recipients doLogger.info("Sending monthly report for #{site.domain} to #{email}")@@ -71,7 +72,7 @@ defmodule Mix.Tasks.SendEmailReports do{pageviews, unique_visitors} = Stats.pageviews_and_visitors(site, query){change_pageviews, change_visitors} = Stats.compare_pageviews_and_visitors(site, query, {pageviews, unique_visitors})bounce_rate = Stats.bounce_rate(site, query)- prev_bounce_rate = Stats.bounce_rate(site, Stats.Query.shift_back(query))+ prev_bounce_rate = Stats.bounce_rate(site, Query.shift_back(query))change_bounce_rate = if prev_bounce_rate > 0, do: bounce_rate - prev_bounce_ratereferrers = Stats.top_referrers(site, query)pages = Stats.top_pages(site, query)diff --git a/lib/mix/tasks/send_site_setup_emails.ex b/lib/workers/send_site_setup_emails.exsimilarity index 65%rename from lib/mix/tasks/send_site_setup_emails.exrename to lib/workers/send_site_setup_emails.exindex b9720cff40..171ec811d2 100644--- a/lib/mix/tasks/send_site_setup_emails.ex+++ b/lib/workers/send_site_setup_emails.ex@@ -1,25 +1,19 @@-defmodule Mix.Tasks.SendSiteSetupEmails do- use Mix.Task+defmodule Plausible.Workers.SendSiteSetupEmails douse Plausible.Repo+ use Oban.Worker, queue: :site_setup_emailsrequire Loggeralias Plausible.Stats.Clickhouse, as: Stats- @doc """- This is scheduled to run every 6 hours.- """+ @impl Oban.Worker+ def perform(_args, _job) do+ send_create_site_emails()+ send_setup_help_emails()+ send_setup_success_emails()- def run(args) do- Application.ensure_all_started(:plausible)- execute(args)+ :okend- def execute(args \\ []) do- send_create_site_emails(args)- send_setup_help_emails(args)- send_setup_success_emails(args)- end-- defp send_create_site_emails(args) do+ defp send_create_site_emails() doq =from(s in Plausible.Auth.User,left_join: se in "create_site_emails", on: se.user_id == s.id,@@ -30,12 +24,12 @@ defmodule Mix.Tasks.SendSiteSetupEmails dofor user <- Repo.all(q) doif Enum.count(user.sites) == 0 do- send_create_site_email(args, user)+ send_create_site_email(user)endendend- defp send_setup_help_emails(args) do+ defp send_setup_help_emails() doq =from(s in Plausible.Site,left_join: se in "setup_help_emails", on: se.site_id == s.id,@@ -51,12 +45,12 @@ defmodule Mix.Tasks.SendSiteSetupEmails dohours_passed = Timex.diff(Timex.now(), site.inserted_at, :hours)if !setup_completed && hours_passed > 47 do- send_setup_help_email(args, owner, site)+ send_setup_help_email(owner, site)endendend- defp send_setup_success_emails(args) do+ defp send_setup_success_emails() doq =from(s in Plausible.Site,left_join: se in "setup_success_emails", on: se.site_id == s.id,@@ -69,16 +63,12 @@ defmodule Mix.Tasks.SendSiteSetupEmails doowner = List.first(site.members)if Stats.has_pageviews?(site) do- send_setup_success_email(args, owner, site)+ send_setup_success_email(owner, site)endendend- defp send_create_site_email(["--dry-run"], user) do- Logger.info("DRY RUN: create site email for #{user.name}")- end-- defp send_create_site_email(_, user) do+ defp send_create_site_email(user) doPlausibleWeb.Email.create_site_email(user)|> Plausible.Mailer.send_email()@@ -88,11 +78,7 @@ defmodule Mix.Tasks.SendSiteSetupEmails do}])end- defp send_setup_success_email(["--dry-run"], _, site) do- Logger.info("DRY RUN: setup success email for #{site.domain}")- end-- defp send_setup_success_email(_, user, site) do+ defp send_setup_success_email(user, site) doPlausibleWeb.Email.site_setup_success(user, site)|> Plausible.Mailer.send_email()@@ -102,11 +88,7 @@ defmodule Mix.Tasks.SendSiteSetupEmails do}])end- defp send_setup_help_email(["--dry-run"], _, site) do- Logger.info("DRY RUN: setup help email for #{site.domain}")- end-- defp send_setup_help_email(_, user, site) do+ defp send_setup_help_email(user, site) doPlausibleWeb.Email.site_setup_help(user, site)|> Plausible.Mailer.send_email()diff --git a/lib/workers/send_trial_notifications.ex b/lib/workers/send_trial_notifications.exnew file mode 100644index 0000000000..13aec9f086--- /dev/null+++ b/lib/workers/send_trial_notifications.ex@@ -0,0 +1,64 @@+defmodule Plausible.Workers.SendTrialNotifications do+ use Plausible.Repo+ use Oban.Worker, queue: :trial_notification_emails+ require Logger++ @impl Oban.Worker+ def perform(_args, _job) do+ users = Repo.all(+ from u in Plausible.Auth.User,+ left_join: s in Plausible.Billing.Subscription, on: s.user_id == u.id,+ where: is_nil(s.id),+ order_by: u.inserted_at+ )++ for user <- users do+ case Timex.diff(user.trial_expiry_date, Timex.today(), :days) do+ 7 ->+ if Plausible.Auth.user_completed_setup?(user) do+ send_one_week_reminder(user)+ end+ 1 ->+ if Plausible.Auth.user_completed_setup?(user) do+ send_tomorrow_reminder(user)+ end+ 0 ->+ if Plausible.Auth.user_completed_setup?(user) do+ send_today_reminder(user)+ end+ -1 ->+ if Plausible.Auth.user_completed_setup?(user) do+ send_over_reminder(user)+ end+ _ ->+ nil+ end+ end++ :ok+ end++ defp send_one_week_reminder(user) do+ PlausibleWeb.Email.trial_one_week_reminder(user)+ |> Plausible.Mailer.send_email()+ end++ defp send_tomorrow_reminder( user) do+ usage = Plausible.Billing.usage(user)++ PlausibleWeb.Email.trial_upgrade_email(user, "tomorrow", usage)+ |> Plausible.Mailer.send_email()+ end++ defp send_today_reminder(user) do+ usage = Plausible.Billing.usage(user)++ PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)+ |> Plausible.Mailer.send_email()+ end++ defp send_over_reminder(user) do+ PlausibleWeb.Email.trial_over_email(user)+ |> Plausible.Mailer.deliver_now()+ end+enddiff --git a/mix.exs b/mix.exsindex 3ebce89274..84b7e2cfae 100644--- a/mix.exs+++ b/mix.exs@@ -87,6 +87,7 @@ defmodule Plausible.MixProject do{:oauther, "~> 1.1"},{:nanoid, "~> 2.0.2"},{:siphash, "~> 3.2"},+ {:oban, "~> 1.2"},{:clickhousex, [git: "https://github.com/atlas-forks/clickhousex.git"]}]enddiff --git a/mix.lock b/mix.lockindex 8b0bb239d9..4508c7dc58 100644--- a/mix.lock+++ b/mix.lock@@ -37,6 +37,7 @@"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},"nanoid": {:hex, :nanoid, "2.0.2", "f3f7b4bf103ab6667f22beb00b6315825ee3f30100dd2c93d534e5c02164e857", [:mix], [], "hexpm", "3095cb1fac7bbc78843a8ccd99f1af375d0da1d3ebaa8552e846b73438c0c44f"},"oauther": {:hex, :oauther, "1.1.1", "7d8b16167bb587ecbcddd3f8792beb9ec3e7b65c1f8ebd86b8dd25318d535752", [:mix], [], "hexpm", "9374f4302045321874cccdc57eb975893643bd69c3b22bf1312dab5f06e5788e"},+ "oban": {:hex, :oban, "1.2.0", "7cca94d341be43d220571e28f69131c4afc21095b25257397f50973d3fc59b07", [:mix], [{:ecto_sql, "~> 3.1", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.14", [hex: :postgrex, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ba5f8b3f7d76967b3e23cf8014f6a13e4ccb33431e4808f036709a7f822362ee"},"parallel_stream": {:hex, :parallel_stream, "1.0.6", "b967be2b23f0f6787fab7ed681b4c45a215a81481fb62b01a5b750fa8f30f76c", [:mix], [], "hexpm", "639b2e8749e11b87b9eb42f2ad325d161c170b39b288ac8d04c4f31f8f0823eb"},"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},"phoenix": {:hex, :phoenix, "1.4.17", "1b1bd4cff7cfc87c94deaa7d60dd8c22e04368ab95499483c50640ef3bd838d8", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.8.1 or ~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3a8e5d7a3d76d452bb5fb86e8b7bd115f737e4f8efe202a463d4aeb4a5809611"},diff --git a/priv/repo/migrations/20200529071028_add_oban_jobs_table.exs b/priv/repo/migrations/20200529071028_add_oban_jobs_table.exsnew file mode 100644index 0000000000..e9ae074b21--- /dev/null+++ b/priv/repo/migrations/20200529071028_add_oban_jobs_table.exs@@ -0,0 +1,11 @@+defmodule Plausible.Repo.Migrations.AddObanJobsTable do+ use Ecto.Migration++ def up do+ Oban.Migrations.up()+ end++ def down do+ Oban.Migrations.down(version: 1)+ end+enddiff --git a/test/mix/tasks/fetch_tweets_test.exs b/test/workers/fetch_tweets_test.exssimilarity index 92%rename from test/mix/tasks/fetch_tweets_test.exsrename to test/workers/fetch_tweets_test.exsindex dfbe399472..51048a5609 100644--- a/test/mix/tasks/fetch_tweets_test.exs+++ b/test/workers/fetch_tweets_test.exs@@ -1,6 +1,6 @@-defmodule Mix.Tasks.FetchTweetsTest do+defmodule Plausible.Workers.FetchTweetsTest douse Plausible.DataCase- alias Mix.Tasks.FetchTweets+ alias Plausible.Workers.FetchTweetsdescribe "processing tweet entities" dotest "inlines links to the body" dodiff --git a/test/mix/tasks/obtain_ssl_certificates_test.exs b/test/workers/provision_ssl_certificates_test.exssimilarity index 79%rename from test/mix/tasks/obtain_ssl_certificates_test.exsrename to test/workers/provision_ssl_certificates_test.exsindex fcac82c326..8d654abc1a 100644--- a/test/mix/tasks/obtain_ssl_certificates_test.exs+++ b/test/workers/provision_ssl_certificates_test.exs@@ -1,6 +1,6 @@-defmodule Mix.Tasks.ObtainSslCertificatesTest do+defmodule Plausible.Workers.SslCertificatesTest douse Plausible.DataCase- alias Mix.Tasks.ObtainSslCertificates+ alias Plausible.Workers.ProvisionSslCertificatesimport Doubletest "makes ssh call to certbot" do@@ -8,7 +8,7 @@ defmodule Mix.Tasks.ObtainSslCertificatesTest doinsert(:custom_domain, site: site, domain: "custom-site.com")system_stub = stub(System, :cmd, fn(_cmd, _args) -> {"", 0} end)- ObtainSslCertificates.execute(system_stub)+ ProvisionSslCertificates.perform(nil, nil, system_stub)assert_receive({System, :cmd, ["ssh", ["-t", "[email protected]", "sudo certbot certonly --nginx -n -d custom-site.com"]]})end@@ -18,7 +18,7 @@ defmodule Mix.Tasks.ObtainSslCertificatesTest doinsert(:custom_domain, site: site, domain: "custom-site.com")system_stub = stub(System, :cmd, fn(_cmd, _args) -> {"", 0} end)- ObtainSslCertificates.execute(system_stub)+ ProvisionSslCertificates.perform(nil, nil, system_stub)domain = Repo.get_by(Plausible.Site.CustomDomain, site_id: site.id)assert domain.has_ssl_certificate@@ -29,7 +29,7 @@ defmodule Mix.Tasks.ObtainSslCertificatesTest doinsert(:custom_domain, site: site, domain: "custom-site.com")failing_system_stub = stub(System, :cmd, fn(_cmd, _args) -> {"", 1} end)- ObtainSslCertificates.execute(failing_system_stub)+ ProvisionSslCertificates.perform(nil, nil, failing_system_stub)domain = Repo.get_by(Plausible.Site.CustomDomain, site_id: site.id)refute domain.has_ssl_certificatediff --git a/test/mix/tasks/send_check_stats_emails_test.exs b/test/workers/send_check_stats_emails_test.exssimilarity index 84%rename from test/mix/tasks/send_check_stats_emails_test.exsrename to test/workers/send_check_stats_emails_test.exsindex 0f6bbde3f2..76f763dc5d 100644--- a/test/mix/tasks/send_check_stats_emails_test.exs+++ b/test/workers/send_check_stats_emails_test.exs@@ -1,12 +1,17 @@-defmodule Mix.Tasks.SendCheckStatsEmailsTest do+defmodule Plausible.Workers.SendCheckStatsEmailsTest douse Plausible.DataCaseuse Bamboo.Test+ defp perform() do+ Plausible.Workers.SendCheckStatsEmails.new(%{}) |> Oban.insert!()+ Oban.drain_queue(:check_stats_emails)+ end+test "does not send an email before a week has passed" douser = insert(:user, inserted_at: days_ago(6), last_seen: days_ago(6))insert(:site, domain: "test-site.com", members: [user])- Mix.Tasks.SendCheckStatsEmails.execute()+ perform()assert_no_emails_delivered()end@@ -15,7 +20,7 @@ defmodule Mix.Tasks.SendCheckStatsEmailsTest douser = insert(:user, inserted_at: days_ago(9), last_seen: days_ago(6))insert(:site, domain: "test-site.com", members: [user])- Mix.Tasks.SendCheckStatsEmails.execute()+ perform()assert_no_emails_delivered()end@@ -25,7 +30,7 @@ defmodule Mix.Tasks.SendCheckStatsEmailsTest dosite = insert(:site, domain: "test-site.com", members: [user])insert(:weekly_report, site: site, recipients: ["[email protected]"])- Mix.Tasks.SendCheckStatsEmails.execute()+ perform()assert_no_emails_delivered()end@@ -34,7 +39,7 @@ defmodule Mix.Tasks.SendCheckStatsEmailsTest douser = insert(:user, inserted_at: days_ago(8), last_seen: days_ago(8))insert(:site, domain: "test-site.com", members: [user])- Mix.Tasks.SendCheckStatsEmails.execute()+ perform()assert_email_delivered_with(to: [{user.name, user.email}],diff --git a/test/mix/tasks/send_email_reports_test.exs b/test/workers/send_email_reports_test.exssimilarity index 83%rename from test/mix/tasks/send_email_reports_test.exsrename to test/workers/send_email_reports_test.exsindex f8432acfca..40dfc6cf30 100644--- a/test/mix/tasks/send_email_reports_test.exs+++ b/test/workers/send_email_reports_test.exs@@ -1,7 +1,12 @@-defmodule Mix.Tasks.EmailReportsTest do+defmodule Plausible.Workers.EmailReportsTest douse Plausible.DataCaseuse Bamboo.Test- alias Mix.Tasks.SendEmailReports+ alias Plausible.Workers.SendEmailReports++ defp perform(args) do+ SendEmailReports.new(args) |> Oban.insert!()+ Oban.drain_queue(:email_reports)+ enddescribe "weekly reports" dotest "sends weekly report on Monday 9am local timezone" do@@ -9,7 +14,7 @@ defmodule Mix.Tasks.EmailReportsTest doinsert(:weekly_report, site: site, recipients: ["[email protected]"])time = Timex.now() |> Timex.beginning_of_week |> Timex.shift(hours: 14) # 2pm UTC is 10am EST- SendEmailReports.execute(time)+ perform(%{"current_time" => time})assert_email_delivered_with(subject: "Weekly report for #{site.domain}", to: [nil: "[email protected]"])end@@ -19,7 +24,7 @@ defmodule Mix.Tasks.EmailReportsTest doinsert(:weekly_report, site: site, recipients: ["[email protected]"])time = Timex.now() |> Timex.beginning_of_week |> Timex.shift(hours: 12) # 12pm UTC is 8am EST- SendEmailReports.execute(time)+ perform(%{"current_time" => time})assert_no_emails_delivered()end@@ -29,7 +34,7 @@ defmodule Mix.Tasks.EmailReportsTest doinsert(:weekly_report, site: site, recipients: ["[email protected]"])time = Timex.now() |> Timex.beginning_of_week |> Timex.shift(days: 1, hours: 10)- SendEmailReports.execute(time)+ perform(%{"current_time" => time})assert_no_emails_delivered()end@@ -39,11 +44,11 @@ defmodule Mix.Tasks.EmailReportsTest doinsert(:weekly_report, site: site, recipients: ["[email protected]"])time = Timex.now() |> Timex.beginning_of_week |> Timex.shift(hours: 10)- SendEmailReports.execute(time)+ perform(%{"current_time" => time})assert_email_delivered_with(subject: "Weekly report for #{site.domain}", to: [nil: "[email protected]"])- SendEmailReports.execute(time)+ perform(%{"current_time" => time})assert_no_emails_delivered()endend@@ -54,7 +59,7 @@ defmodule Mix.Tasks.EmailReportsTest doinsert(:monthly_report, site: site, recipients: ["[email protected]"]){:ok, time, _} = DateTime.from_iso8601("2019-04-01T14:00:00Z")- SendEmailReports.execute(time)+ perform(%{"current_time" => time})assert_email_delivered_with(subject: "March report for #{site.domain}", to: [nil: "[email protected]"])end@@ -64,7 +69,7 @@ defmodule Mix.Tasks.EmailReportsTest doinsert(:monthly_report, site: site, recipients: ["[email protected]"])time = Timex.now() |> Timex.beginning_of_month |> Timex.shift(hours: 12) # 12pm UTC is 8am EST- SendEmailReports.execute(time)+ perform(%{"current_time" => time})assert_no_emails_delivered()end@@ -74,7 +79,7 @@ defmodule Mix.Tasks.EmailReportsTest doinsert(:monthly_report, site: site, recipients: ["[email protected]"])time = Timex.now() |> Timex.beginning_of_month |> Timex.shift(days: 1, hours: 10)- SendEmailReports.execute(time)+ perform(%{"current_time" => time})assert_no_emails_delivered()end@@ -84,11 +89,11 @@ defmodule Mix.Tasks.EmailReportsTest doinsert(:monthly_report, site: site, recipients: ["[email protected]"]){:ok, time, _} = DateTime.from_iso8601("2019-02-01T11:00:00Z")- SendEmailReports.execute(time)+ perform(%{"current_time" => time})assert_email_delivered_with(subject: "January report for #{site.domain}", to: [nil: "[email protected]"])- SendEmailReports.execute(time)+ perform(%{"current_time" => time})assert_no_emails_delivered()endenddiff --git a/test/mix/tasks/send_site_setup_emails_test.exs b/test/workers/send_site_setup_emails_test.exssimilarity index 85%rename from test/mix/tasks/send_site_setup_emails_test.exsrename to test/workers/send_site_setup_emails_test.exsindex 7f0fb1c548..f66a62afd1 100644--- a/test/mix/tasks/send_site_setup_emails_test.exs+++ b/test/workers/send_site_setup_emails_test.exs@@ -1,14 +1,20 @@-defmodule Mix.Tasks.SendSiteSetupEmailsTest do+defmodule Plausible.Workers.SendSiteSetupEmailsTest douse Plausible.DataCaseuse Bamboo.Testimport Plausible.TestUtils+ alias Plausible.Workers.SendSiteSetupEmails++ defp perform() do+ SendSiteSetupEmails.new(%{}) |> Oban.insert!()+ Oban.drain_queue(:site_setup_emails)+ enddescribe "when user has not managed to set up the site" dotest "does not send an email 47 hours after site creation" douser = insert(:user)insert(:site, members: [user], inserted_at: hours_ago(47))- Mix.Tasks.SendSiteSetupEmails.execute()+ perform()assert_no_emails_delivered()end@@ -17,7 +23,7 @@ defmodule Mix.Tasks.SendSiteSetupEmailsTest douser = insert(:user)insert(:site, members: [user], inserted_at: hours_ago(49))- Mix.Tasks.SendSiteSetupEmails.execute()+ perform()assert_email_delivered_with(to: [{user.name, user.email}],@@ -29,7 +35,7 @@ defmodule Mix.Tasks.SendSiteSetupEmailsTest douser = insert(:user)insert(:site, members: [user], inserted_at: hours_ago(73))- Mix.Tasks.SendSiteSetupEmails.execute()+ perform()assert_no_emails_delivered()end@@ -40,7 +46,7 @@ defmodule Mix.Tasks.SendSiteSetupEmailsTest douser = insert(:user)insert(:site, members: [user], domain: "test-site.com")- Mix.Tasks.SendSiteSetupEmails.execute()+ perform()assert_email_delivered_with(to: [{user.name, user.email}],@@ -52,7 +58,7 @@ defmodule Mix.Tasks.SendSiteSetupEmailsTest douser = insert(:user)site = insert(:site, members: [user], inserted_at: hours_ago(49))- Mix.Tasks.SendSiteSetupEmails.execute()+ perform()assert_email_delivered_with(to: [{user.name, user.email}],@@ -60,7 +66,7 @@ defmodule Mix.Tasks.SendSiteSetupEmailsTest do)create_pageviews([%{domain: site.domain}])- Mix.Tasks.SendSiteSetupEmails.execute()+ perform()assert_email_delivered_with(to: [{user.name, user.email}],@@ -73,7 +79,7 @@ defmodule Mix.Tasks.SendSiteSetupEmailsTest dotest "does not send an email before 48h have passed" doinsert(:user, inserted_at: hours_ago(47))- Mix.Tasks.SendSiteSetupEmails.execute()+ perform()assert_no_emails_delivered()end@@ -81,7 +87,7 @@ defmodule Mix.Tasks.SendSiteSetupEmailsTest dotest "sends the create site email after 48h" douser = insert(:user, inserted_at: hours_ago(49))- Mix.Tasks.SendSiteSetupEmails.execute()+ perform()assert_email_delivered_with(to: [{user.name, user.email}],diff --git a/test/mix/tasks/send_trial_notifications_test.exs b/test/workers/send_trial_notifications_test.exssimilarity index 85%rename from test/mix/tasks/send_trial_notifications_test.exsrename to test/workers/send_trial_notifications_test.exsindex 9787dbe38a..80b5dbe19f 100644--- a/test/mix/tasks/send_trial_notifications_test.exs+++ b/test/workers/send_trial_notifications_test.exs@@ -1,14 +1,19 @@-defmodule Mix.Tasks.SendTrialNotificationsTest do+defmodule Plausible.Workers.SendTrialNotificationsTest douse Plausible.DataCaseuse Bamboo.Test+ defp perform() do+ Plausible.Workers.SendTrialNotifications.new(%{}) |> Oban.insert!()+ Oban.drain_queue(:trial_notification_emails)+ end+test "does not send a notification if user didn't set up their site" doinsert(:user, inserted_at: Timex.now |> Timex.shift(days: -14))insert(:user, inserted_at: Timex.now |> Timex.shift(days: -29))insert(:user, inserted_at: Timex.now |> Timex.shift(days: -30))insert(:user, inserted_at: Timex.now |> Timex.shift(days: -31))- Mix.Tasks.SendTrialNotifications.execute()+ perform()assert_no_emails_delivered()end@@ -18,7 +23,7 @@ defmodule Mix.Tasks.SendTrialNotificationsTest douser = insert(:user, trial_expiry_date: Timex.now |> Timex.shift(days: 7))insert(:site, domain: "test-site.com", members: [user])- Mix.Tasks.SendTrialNotifications.execute()+ perform()assert_delivered_email(PlausibleWeb.Email.trial_one_week_reminder(user))end@@ -27,7 +32,7 @@ defmodule Mix.Tasks.SendTrialNotificationsTest douser = insert(:user, trial_expiry_date: Timex.now |> Timex.shift(days: 1))insert(:site, domain: "test-site.com", members: [user])- Mix.Tasks.SendTrialNotifications.execute()+ perform()assert_delivered_email(PlausibleWeb.Email.trial_upgrade_email(user, "tomorrow", 3))end@@ -36,7 +41,7 @@ defmodule Mix.Tasks.SendTrialNotificationsTest douser = insert(:user, trial_expiry_date: Timex.today())insert(:site, domain: "test-site.com", members: [user])- Mix.Tasks.SendTrialNotifications.execute()+ perform()assert_delivered_email(PlausibleWeb.Email.trial_upgrade_email(user, "today", 3))end@@ -45,7 +50,7 @@ defmodule Mix.Tasks.SendTrialNotificationsTest douser = insert(:user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))insert(:site, domain: "test-site.com", members: [user])- Mix.Tasks.SendTrialNotifications.execute()+ perform()assert_delivered_email(PlausibleWeb.Email.trial_over_email(user))end@@ -55,7 +60,7 @@ defmodule Mix.Tasks.SendTrialNotificationsTest doinsert(:site, domain: "test-site.com", members: [user])insert(:subscription, user: user)- Mix.Tasks.SendTrialNotifications.execute()+ perform()assert_no_emails_delivered()end