2020-06-28 13:24:54 +03:00
|
|
|
defmodule Plausible.Workers.ScheduleEmailReports do
|
|
|
|
use Plausible.Repo
|
|
|
|
use Oban.Worker, queue: :schedule_email_reports
|
|
|
|
alias Plausible.Workers.SendEmailReport
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@impl Oban.Worker
|
|
|
|
@doc """
|
|
|
|
Email reports should be sent on Monday at 9am according to the timezone
|
|
|
|
of a site. This job runs every day at midnight to ensure that all sites
|
|
|
|
have a scheduled job for email reports.
|
|
|
|
"""
|
2021-04-26 11:32:18 +03:00
|
|
|
def perform(_job) do
|
2020-06-28 13:24:54 +03:00
|
|
|
schedule_weekly_emails()
|
|
|
|
schedule_monthly_emails()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp schedule_weekly_emails() do
|
2020-07-21 09:58:00 +03:00
|
|
|
weekly_jobs =
|
|
|
|
from(
|
|
|
|
j in Oban.Job,
|
|
|
|
where:
|
|
|
|
j.worker == "Plausible.Workers.SendEmailReport" and
|
|
|
|
fragment("(? ->> 'interval')", j.args) == "weekly"
|
|
|
|
)
|
2020-06-28 13:24:54 +03:00
|
|
|
|
|
|
|
sites =
|
|
|
|
Repo.all(
|
|
|
|
from s in Plausible.Site,
|
2020-07-21 09:58:00 +03:00
|
|
|
join: wr in Plausible.Site.WeeklyReport,
|
|
|
|
on: wr.site_id == s.id,
|
|
|
|
left_join: job in subquery(weekly_jobs),
|
|
|
|
on:
|
|
|
|
fragment("(? -> 'site_id')::int", job.args) == s.id and
|
|
|
|
job.state not in ["completed", "discarded"],
|
2020-06-28 13:24:54 +03:00
|
|
|
where: is_nil(job),
|
|
|
|
preload: [weekly_report: wr]
|
|
|
|
)
|
|
|
|
|
|
|
|
for site <- sites do
|
2020-07-21 09:58:00 +03:00
|
|
|
SendEmailReport.new(%{site_id: site.id, interval: "weekly"},
|
|
|
|
scheduled_at: monday_9am(site.timezone)
|
|
|
|
)
|
2020-06-28 13:24:54 +03:00
|
|
|
|> Oban.insert!()
|
|
|
|
end
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def monday_9am(timezone) do
|
|
|
|
Timex.now(timezone)
|
|
|
|
|> Timex.shift(weeks: 1)
|
|
|
|
|> Timex.beginning_of_week()
|
|
|
|
|> Timex.shift(hours: 9)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp schedule_monthly_emails() do
|
2020-07-21 09:58:00 +03:00
|
|
|
monthly_jobs =
|
|
|
|
from(
|
|
|
|
j in Oban.Job,
|
|
|
|
where:
|
|
|
|
j.worker == "Plausible.Workers.SendEmailReport" and
|
|
|
|
fragment("(? ->> 'interval')", j.args) == "monthly"
|
|
|
|
)
|
2020-06-28 13:24:54 +03:00
|
|
|
|
|
|
|
sites =
|
|
|
|
Repo.all(
|
|
|
|
from s in Plausible.Site,
|
|
|
|
join: mr in Plausible.Site.MonthlyReport,
|
|
|
|
on: mr.site_id == s.id,
|
|
|
|
left_join: job in subquery(monthly_jobs),
|
|
|
|
on:
|
|
|
|
fragment("(? -> 'site_id')::int", job.args) == s.id and
|
|
|
|
job.state not in ["completed", "discarded"],
|
|
|
|
where: is_nil(job),
|
|
|
|
preload: [monthly_report: mr]
|
|
|
|
)
|
|
|
|
|
|
|
|
for site <- sites do
|
2020-07-21 09:58:00 +03:00
|
|
|
SendEmailReport.new(%{site_id: site.id, interval: "monthly"},
|
|
|
|
scheduled_at: first_of_month_9am(site.timezone)
|
|
|
|
)
|
2020-06-28 13:24:54 +03:00
|
|
|
|> Oban.insert!()
|
|
|
|
end
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_of_month_9am(timezone) do
|
|
|
|
Timex.now(timezone)
|
|
|
|
|> Timex.shift(months: 1)
|
|
|
|
|> Timex.beginning_of_month()
|
|
|
|
|> Timex.shift(hours: 9)
|
|
|
|
end
|
|
|
|
end
|