Skip to content

Commit

Permalink
Host checks execution scheduler (#1780)
Browse files Browse the repository at this point in the history
* Adjust scheduler config for host execution

* Add host checks exection scheduler funtion and test

* Add cluster checks execution test
  • Loading branch information
EMaksy authored Sep 4, 2023
1 parent 259c5fd commit 1aae5a2
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 10 deletions.
7 changes: 7 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ config :trento, Trento.Scheduler,
task: {Trento.Clusters, :request_clusters_checks_execution, []},
run_strategy: {Quantum.RunStrategy.Random, :cluster},
overlap: false
],
hosts_checks_execution: [
# Runs every five minutes
schedule: "*/5 * * * *",
task: {Trento.Hosts, :request_hosts_checks_execution, []},
run_strategy: {Quantum.RunStrategy.Random, :cluster},
overlap: false
]
],
debug_logging: false
Expand Down
3 changes: 3 additions & 0 deletions config/demo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ config :trento, Trento.Scheduler,
clusters_checks_execution: [
schedule: {:extended, "@hourly"}
],
hosts_checks_execution: [
schedule: {:extended, "@hourly"}
],
heartbeat_fake: [
schedule: {:extended, "*/5"},
task: {Trento.Heartbeats.Faker, :send_heartbeats, []},
Expand Down
3 changes: 3 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ unless IEx.started?() do
],
clusters_checks_execution: [
schedule: {:extended, "@hourly"}
],
hosts_checks_execution: [
schedule: {:extended, "@hourly"}
]
]
end
Expand Down
5 changes: 4 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ if config_env() in [:prod, :demo] do

config :trento, Trento.Scheduler,
jobs: [
# Runs every five minutes by default
clusters_checks_execution: [
# Runs every five minutes by default
schedule: "*/#{System.get_env("CHECKS_INTERVAL", "5")} * * * *"
],
hosts_checks_execution: [
schedule: "*/#{System.get_env("CHECKS_INTERVAL", "5")} * * * *"
]
]
Expand Down
20 changes: 20 additions & 0 deletions lib/trento/application/usecases/hosts/hosts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ defmodule Trento.Hosts do
end
end

def request_hosts_checks_execution do
query =
from(h in HostReadModel,
select: h.id,
where: is_nil(h.deregistered_at)
)

query
|> Repo.all()
|> Enum.each(fn host_id ->
case request_checks_execution(host_id) do
:ok ->
:ok

{:error, reason} ->
Logger.error("Failed to request checks execution, host: #{host_id}, reason: #{reason}")
end
end)
end

@spec select_checks(String.t(), [String.t()]) :: :ok | {:error, any}
def select_checks(host_id, checks) do
Logger.debug("Selecting checks, host: #{host_id}")
Expand Down
67 changes: 64 additions & 3 deletions test/trento/application/usecases/clusters_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ defmodule Trento.ClustersTest do

import Trento.Factory

alias Trento.Clusters
alias Trento.{ClusterEnrichmentData, ClusterReadModel, Clusters}

alias Trento.ClusterEnrichmentData
alias Trento.ClusterReadModel
alias Trento.Checks.V1.{
ExecutionRequested,
Target
}

require Trento.Domain.Enums.ClusterType
require Logger

setup [:set_mox_from_context, :verify_on_exit!]

Expand Down Expand Up @@ -64,6 +69,62 @@ defmodule Trento.ClustersTest do

assert {:error, :amqp_error} = Clusters.request_checks_execution(cluster_id)
end

test "should request cluster checks execution when checks are selected" do
checks = [Faker.UUID.v4(), Faker.UUID.v4()]
%{id: cluster_id} = insert(:cluster, id: Faker.UUID.v4(), selected_checks: checks)
%{id: host_id1} = insert(:host, id: Faker.UUID.v4(), cluster_id: cluster_id)
%{id: host_id2} = insert(:host, id: Faker.UUID.v4(), cluster_id: cluster_id)

%{id: cluster_id2} =
insert(:cluster,
id: Faker.UUID.v4(),
selected_checks: checks,
deregistered_at: DateTime.utc_now()
)

%{id: host_id3} = insert(:host, id: Faker.UUID.v4(), cluster_id: cluster_id2)
%{id: host_id4} = insert(:host, id: Faker.UUID.v4(), cluster_id: cluster_id2)

expect(Trento.Infrastructure.Messaging.Adapter.Mock, :publish, fn "executions",
%ExecutionRequested{
group_id: ^cluster_id,
targets: [
%Target{
agent_id: ^host_id1,
checks: ^checks
},
%Target{
agent_id: ^host_id2,
checks: ^checks
}
]
} ->
:ok
end)

expect(Trento.Infrastructure.Messaging.Adapter.Mock, :publish, 0, fn "executions",
%ExecutionRequested{
group_id:
^cluster_id2,
targets: [
%Target{
agent_id:
^host_id3,
checks: ^checks
},
%Target{
agent_id:
^host_id4,
checks: ^checks
}
]
} ->
:ok
end)

assert :ok = Clusters.request_clusters_checks_execution()
end
end

describe "get clusters" do
Expand Down
65 changes: 59 additions & 6 deletions test/trento/application/usecases/hosts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ defmodule Trento.HostsTest do
use ExUnit.Case
use Trento.DataCase

import Mox

import ExUnit.CaptureLog
import Trento.Factory
import Mox

alias Trento.Hosts
alias Trento.Repo

alias Trento.Domain.Commands.SelectHostChecks
alias Trento.{Hosts, Repo, SlesSubscriptionReadModel}

alias Trento.SlesSubscriptionReadModel
alias Trento.Checks.V1.{
ExecutionRequested,
Target
}

require Logger

@moduletag :integration

Expand Down Expand Up @@ -171,5 +173,56 @@ defmodule Trento.HostsTest do

assert {:error, :amqp_error} = Hosts.request_checks_execution(host_id)
end

test "should request host checks execution for hosts when checks are selected" do
checks = [Faker.UUID.v4(), Faker.UUID.v4()]
%{id: host_id1} = insert(:host, id: Faker.UUID.v4(), selected_checks: checks)

%{id: host_id2} =
insert(:host,
selected_checks: checks,
deregistered_at: DateTime.utc_now()
)

expect(Trento.Infrastructure.Messaging.Adapter.Mock, :publish, 1, fn "executions",
%ExecutionRequested{
group_id: ^host_id1,
targets: [
%Target{
agent_id:
^host_id1,
checks: ^checks
}
]
} ->
:ok
end)

expect(Trento.Infrastructure.Messaging.Adapter.Mock, :publish, 0, fn "executions",
%ExecutionRequested{
group_id: ^host_id2,
targets: [
%Target{
agent_id:
^host_id2,
checks: ^checks
}
]
} ->
:ok
end)

assert :ok = Hosts.request_hosts_checks_execution()
end

test "should log an error message when host checks execution is requested but no checks selected" do
%{id: host_id} = insert(:host, selected_checks: [])

expected_logger_message =
"Failed to request checks execution, host: #{host_id}, reason: no_checks_selected"

assert capture_log(fn -> Hosts.request_hosts_checks_execution() end) =~
expected_logger_message
end
end
end

0 comments on commit 1aae5a2

Please sign in to comment.