analytics/lib/workers/send_site_setup_emails.ex
Uku Taht 25d8f2f3f9
Schedule regular jobs with Oban (#69)
* Use oban for work

* Remove mix task for email reports

* Convert remaining tasks to Oban workers

* Add crontab for scheduled jobs

* Only enable cron if configured to do so
2020-06-02 13:37:38 +03:00

101 lines
2.7 KiB
Elixir

defmodule Plausible.Workers.SendSiteSetupEmails do
use Plausible.Repo
use Oban.Worker, queue: :site_setup_emails
require Logger
alias Plausible.Stats.Clickhouse, as: Stats
@impl Oban.Worker
def perform(_args, _job) do
send_create_site_emails()
send_setup_help_emails()
send_setup_success_emails()
:ok
end
defp send_create_site_emails() do
q =
from(s in Plausible.Auth.User,
left_join: se in "create_site_emails", on: se.user_id == s.id,
where: is_nil(se.id),
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval") and s.inserted_at < fragment("(now() at time zone 'utc') - '48 hours'::interval"),
preload: :sites
)
for user <- Repo.all(q) do
if Enum.count(user.sites) == 0 do
send_create_site_email(user)
end
end
end
defp send_setup_help_emails() do
q =
from(s in Plausible.Site,
left_join: se in "setup_help_emails", on: se.site_id == s.id,
where: is_nil(se.id),
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval"),
preload: :members
)
for site <- Repo.all(q) do
owner = List.first(site.members)
setup_completed = Stats.has_pageviews?(site)
hours_passed = Timex.diff(Timex.now(), site.inserted_at, :hours)
if !setup_completed && hours_passed > 47 do
send_setup_help_email(owner, site)
end
end
end
defp send_setup_success_emails() do
q =
from(s in Plausible.Site,
left_join: se in "setup_success_emails", on: se.site_id == s.id,
where: is_nil(se.id),
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval"),
preload: :members
)
for site <- Repo.all(q) do
owner = List.first(site.members)
if Stats.has_pageviews?(site) do
send_setup_success_email(owner, site)
end
end
end
defp send_create_site_email(user) do
PlausibleWeb.Email.create_site_email(user)
|> Plausible.Mailer.send_email()
Repo.insert_all("create_site_emails", [%{
user_id: user.id,
timestamp: NaiveDateTime.utc_now()
}])
end
defp send_setup_success_email(user, site) do
PlausibleWeb.Email.site_setup_success(user, site)
|> Plausible.Mailer.send_email()
Repo.insert_all("setup_success_emails", [%{
site_id: site.id,
timestamp: NaiveDateTime.utc_now()
}])
end
defp send_setup_help_email(user, site) do
PlausibleWeb.Email.site_setup_help(user, site)
|> Plausible.Mailer.send_email()
Repo.insert_all("setup_help_emails", [%{
site_id: site.id,
timestamp: NaiveDateTime.utc_now()
}])
end
end