Updated timezone logic to prevent boot crash

This commit is contained in:
Kieran Eglin 2024-10-30 10:14:04 -07:00
parent 8c0df65c0c
commit 3c10d78e78
No known key found for this signature in database
GPG key ID: 193984967FCF432D
2 changed files with 21 additions and 10 deletions

View file

@ -67,15 +67,6 @@ if config_env() == :prod do
# For running PF in a subdirectory via a reverse proxy
base_route_path = System.get_env("BASE_ROUTE_PATH", "/")
enable_ipv6 = String.length(System.get_env("ENABLE_IPV6", "")) > 0
attempted_timezone = System.get_env("TIMEZONE") || System.get_env("TZ") || "UTC"
valid_timezone =
if Timex.Timezone.exists?(attempted_timezone) do
attempted_timezone
else
Logger.warning("Invalid timezone #{attempted_timezone}, defaulting to UTC")
"UTC"
end
config :logger, level: String.to_existing_atom(System.get_env("LOG_LEVEL", "debug"))
@ -88,7 +79,8 @@ if config_env() == :prod do
tmpfile_directory: Path.join([System.tmp_dir!(), "pinchflat", "data"]),
dns_cluster_query: System.get_env("DNS_CLUSTER_QUERY"),
expose_feed_endpoints: expose_feed_endpoints,
timezone: valid_timezone,
# This is configured in application.ex
timezone: "UTC",
log_path: log_path,
base_route_path: base_route_path

View file

@ -4,9 +4,12 @@ defmodule Pinchflat.Application do
@moduledoc false
use Application
require Logger
@impl true
def start(_type, _args) do
check_and_update_timezone()
children = [
PinchflatWeb.Telemetry,
Pinchflat.Repo,
@ -47,4 +50,20 @@ defmodule Pinchflat.Application do
:ok = Oban.Telemetry.attach_default_logger()
:telemetry.attach_many("job-telemetry-broadcast", events, &PinchflatWeb.Telemetry.job_state_change_broadcast/4, [])
end
# This has to be here (rather than runtime.exs) since the `tzdata` application
# has to be started before we can check the timezone
defp check_and_update_timezone do
attempted_timezone = System.get_env("TIMEZONE") || System.get_env("TZ") || "UTC"
valid_timezone =
if Tzdata.zone_exists?(attempted_timezone) do
attempted_timezone
else
Logger.warning("Invalid timezone #{attempted_timezone}, defaulting to UTC")
"UTC"
end
Application.put_env(:pinchflat, :timezone, valid_timezone)
end
end