Add Instances.check_all_unreachable/0 and Instance.check_unreachable/1
This commit is contained in:
parent
8a05516862
commit
29f7607910
4 changed files with 85 additions and 0 deletions
|
|
@ -24,4 +24,13 @@ defmodule Pleroma.Instances do
|
||||||
url_or_host
|
url_or_host
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc "Schedules reachability checks for all unreachable instances"
|
||||||
|
def check_all_unreachable do
|
||||||
|
get_unreachable()
|
||||||
|
|> Enum.each(fn {domain, _} ->
|
||||||
|
Pleroma.Workers.ReachabilityWorker.new(%{"domain" => domain})
|
||||||
|
|> Oban.insert()
|
||||||
|
end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -304,4 +304,10 @@ defmodule Pleroma.Instances.Instance do
|
||||||
DeleteWorker.new(%{"op" => "delete_instance", "host" => host})
|
DeleteWorker.new(%{"op" => "delete_instance", "host" => host})
|
||||||
|> Oban.insert()
|
|> Oban.insert()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc "Schedules reachability check for instance"
|
||||||
|
def check_unreachable(domain) when is_binary(domain) do
|
||||||
|
Pleroma.Workers.ReachabilityWorker.new(%{"domain" => domain})
|
||||||
|
|> Oban.insert()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -249,4 +249,34 @@ defmodule Pleroma.Instances.InstanceTest do
|
||||||
args: %{"op" => "delete_instance", "host" => "mushroom.kingdom"}
|
args: %{"op" => "delete_instance", "host" => "mushroom.kingdom"}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "check_unreachable/1" do
|
||||||
|
test "schedules a ReachabilityWorker job for the given domain" do
|
||||||
|
domain = "test.example.com"
|
||||||
|
|
||||||
|
# Call check_unreachable
|
||||||
|
assert {:ok, _job} = Instance.check_unreachable(domain)
|
||||||
|
|
||||||
|
# Verify that a ReachabilityWorker job was scheduled
|
||||||
|
jobs = all_enqueued(worker: Pleroma.Workers.ReachabilityWorker)
|
||||||
|
assert length(jobs) == 1
|
||||||
|
[job] = jobs
|
||||||
|
assert job.args["domain"] == domain
|
||||||
|
end
|
||||||
|
|
||||||
|
test "handles multiple calls for the same domain (uniqueness enforced)" do
|
||||||
|
domain = "duplicate.example.com"
|
||||||
|
|
||||||
|
assert {:ok, _job1} = Instance.check_unreachable(domain)
|
||||||
|
|
||||||
|
# Second call for the same domain
|
||||||
|
assert {:ok, %Oban.Job{conflict?: true}} = Instance.check_unreachable(domain)
|
||||||
|
|
||||||
|
# Should only have one job due to uniqueness
|
||||||
|
jobs = all_enqueued(worker: Pleroma.Workers.ReachabilityWorker)
|
||||||
|
assert length(jobs) == 1
|
||||||
|
[job] = jobs
|
||||||
|
assert job.args["domain"] == domain
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ defmodule Pleroma.InstancesTest do
|
||||||
alias Pleroma.Instances
|
alias Pleroma.Instances
|
||||||
|
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
|
use Oban.Testing, repo: Pleroma.Repo
|
||||||
|
|
||||||
describe "reachable?/1" do
|
describe "reachable?/1" do
|
||||||
test "returns `true` for host / url with unknown reachability status" do
|
test "returns `true` for host / url with unknown reachability status" do
|
||||||
|
|
@ -69,4 +70,43 @@ defmodule Pleroma.InstancesTest do
|
||||||
assert {:error, _} = Instances.set_unreachable(1)
|
assert {:error, _} = Instances.set_unreachable(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "check_all_unreachable/0" do
|
||||||
|
test "schedules ReachabilityWorker jobs for all unreachable instances" do
|
||||||
|
domain1 = "unreachable1.example.com"
|
||||||
|
domain2 = "unreachable2.example.com"
|
||||||
|
domain3 = "unreachable3.example.com"
|
||||||
|
|
||||||
|
Instances.set_unreachable(domain1)
|
||||||
|
Instances.set_unreachable(domain2)
|
||||||
|
Instances.set_unreachable(domain3)
|
||||||
|
|
||||||
|
Instances.check_all_unreachable()
|
||||||
|
|
||||||
|
# Verify that ReachabilityWorker jobs were scheduled for all unreachable domains
|
||||||
|
jobs = all_enqueued(worker: Pleroma.Workers.ReachabilityWorker)
|
||||||
|
assert length(jobs) == 3
|
||||||
|
|
||||||
|
domains = Enum.map(jobs, & &1.args["domain"])
|
||||||
|
assert domain1 in domains
|
||||||
|
assert domain2 in domains
|
||||||
|
assert domain3 in domains
|
||||||
|
end
|
||||||
|
|
||||||
|
test "does not schedule jobs for reachable instances" do
|
||||||
|
unreachable_domain = "unreachable.example.com"
|
||||||
|
reachable_domain = "reachable.example.com"
|
||||||
|
|
||||||
|
Instances.set_unreachable(unreachable_domain)
|
||||||
|
Instances.set_reachable(reachable_domain)
|
||||||
|
|
||||||
|
Instances.check_all_unreachable()
|
||||||
|
|
||||||
|
# Verify that only one job was scheduled (for the unreachable domain)
|
||||||
|
jobs = all_enqueued(worker: Pleroma.Workers.ReachabilityWorker)
|
||||||
|
assert length(jobs) == 1
|
||||||
|
[job] = jobs
|
||||||
|
assert job.args["domain"] == unreachable_domain
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue