Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lorenzosinisi/woke
A watchdog library for Elixir
https://github.com/lorenzosinisi/woke
Last synced: 3 days ago
JSON representation
A watchdog library for Elixir
- Host: GitHub
- URL: https://github.com/lorenzosinisi/woke
- Owner: lorenzosinisi
- License: apache-2.0
- Created: 2020-07-31T07:01:47.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-01-13T15:25:56.000Z (almost 4 years ago)
- Last Synced: 2024-12-09T15:58:41.393Z (19 days ago)
- Language: Elixir
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Woke
## Monitor a new external resource
Create 2 modules and add the supervisor to your children in application.ex
1. Create the interface for the external resource, for instance one named Postgres:
```elixir
defmodule MyApp.Woke.Postgres do
@moduledoc false
@behaviour Woke.ExternalResource@impl Woke.ExternalResource
def try_connect(opts \\ []) do
try do
Ecto.Adapters.SQL.query(MyApp.Repo, "select 1", []):connected
rescue
error ->
{:error, error}
catch
_, error ->
{:error, error}
end
end
end
```2. Configure the external resource being monitored using a GenServer:
```elixir
defmodule MyApp.Woke.PostgresConnections do
use Supervisor
alias Woke.ExternalResource
alias MyApp.Woke.Postgresdef start_link(_opts) do
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end@impl true
def init(_opts) do
children = all_child_specs()
Supervisor.init(children, strategy: :one_for_one)
enddefp all_child_specs() do
database_number = [1,2,3]Enum.map(database_number, fn shard_id ->
opts = [
name: "postgres_#{shard_id}",
check_every: :timer.seconds(30),
try_connect_mod: Postgres,
timeout: :timer.seconds(3)
]Supervisor.child_spec({ExternalResource, opts}, id: :"postgres_#{shard_id}")
end)
end
end
```3. Add `MyApp.Woke.PostgresConnections` to your app supervisor children list:
```elixir
use Application# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec:gen_event.swap_handler(
:alarm_handler,
{:alarm_handler, :swap},
{Woke.AlarmHandler, :ok}
)# Define workers and child supervisors to be supervised
children = [
supervisor(MyApp.Endpoint, []),
MyApp.Woke.PostgresConnections
]opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end```