Skip to content

Commit

Permalink
ResourceUnavailableJob : permet d'ignorer certaines ressources (#3471)
Browse files Browse the repository at this point in the history
* ResourceUnavailableJob : permet d'ignorer certaines ressources

* Add log
  • Loading branch information
AntoineAugusti authored Sep 19, 2023
1 parent 785bb48 commit 30b13cd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
27 changes: 22 additions & 5 deletions apps/transport/lib/jobs/resource_unavailable_job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ defmodule Transport.Jobs.ResourceUnavailableJob do
require Logger
alias DB.{Repo, Resource, ResourceUnavailability}

# Set this env variable to a list of `resource.id`s (comma separated) to bypass
# `AvailabilityChecker.available?`. This is *not* something that should be used
# for too long or for too many resources.
# Example values: `42,1337`
# https://github.com/etalab/transport-site/issues/3470
@bypass_ids_env_name "BYPASS_RESOURCE_AVAILABILITY_RESOURCE_IDS"

@impl Oban.Worker
def perform(%Oban.Job{args: %{"resource_id" => resource_id}}) do
Logger.info("Running ResourceUnavailableJob for #{resource_id}")
Expand All @@ -68,14 +75,24 @@ defmodule Transport.Jobs.ResourceUnavailableJob do
{true, resource}
end

defp check_availability({:updated, status_code, %Resource{format: format, url: url} = resource})
defp check_availability({:updated, status_code, %Resource{url: url} = resource})
when status_code != 200 do
{Transport.AvailabilityChecker.Wrapper.available?(format, url), resource}
perform_check(resource, url)
end

defp check_availability({:no_op, %Resource{} = resource}) do
perform_check(resource, Resource.download_url(resource))
end

defp check_availability({:no_op, %Resource{format: format} = resource}) do
check_url = Resource.download_url(resource)
{Transport.AvailabilityChecker.Wrapper.available?(format, check_url), resource}
defp perform_check(%Resource{id: resource_id, format: format} = resource, check_url) do
bypass_resource_ids = @bypass_ids_env_name |> System.get_env("") |> String.split(",")

if to_string(resource_id) in bypass_resource_ids do
Logger.info("is_available=true for resource##{resource_id} because the check is bypassed")
{true, resource}
else
{Transport.AvailabilityChecker.Wrapper.available?(format, check_url), resource}
end
end

# GOTCHA: `filetype` is set to `"file"` for exports coming from ODS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,34 @@ defmodule Transport.Test.Transport.Jobs.ResourceUnavailableJobTest do
] = all_enqueued(worker: Transport.Jobs.Workflow)
end

test "does not perform a real availability check if the resource is bypassed" do
url = "https://example.com/stop-monitoring"

resource =
insert(:resource,
url: url,
latest_url: latest_url = url,
filetype: "file",
is_available: true,
format: "SIRI",
datagouv_id: Ecto.UUID.generate()
)

System.put_env("BYPASS_RESOURCE_AVAILABILITY_RESOURCE_IDS", "#{resource.id},42")

Transport.HTTPoison.Mock
|> expect(:get, fn ^latest_url ->
{:ok, %HTTPoison.Response{status_code: 405}}
end)

# `Transport.AvailabilityChecker.Mock` is not called

assert :ok == perform_job(ResourceUnavailableJob, %{"resource_id" => resource.id})

assert 0 == count_resource_unavailabilities()
assert %DB.Resource{is_available: true} = Repo.reload(resource)
end

test "performs a GET request and allows a 401 response for a SIRI resource" do
resource =
insert(:resource,
Expand Down

0 comments on commit 30b13cd

Please sign in to comment.