Skip to content

Commit

Permalink
[refs #16] logs loading errors during startup/reload
Browse files Browse the repository at this point in the history
  • Loading branch information
mneudert committed May 1, 2017
1 parent 8406c14 commit 08667ab
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v0.14.0-dev

- Enhancements
- Errors occurring while initially loading databases
(or when calling `Geolix.reload_databases/0`) are now sent to
`Logger.error` ([#16](https://github.com/elixir-geolix/geolix/issues/16))

## v0.13.0 (2017-04-12)

- Backwards incompatible changes
Expand Down
15 changes: 14 additions & 1 deletion lib/geolix/database/loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule Geolix.Database.Loader do

use GenServer

require Logger

alias Geolix.Database.Supervisor, as: DatabaseSupervisor


Expand Down Expand Up @@ -44,7 +46,18 @@ defmodule Geolix.Database.Loader do
end

def handle_cast(:reload_databases, state) do
:ok = state |> Keyword.values() |> Enum.each(&load_database/1)
:ok =
state
|> Keyword.values()
|> Enum.each(fn (db) ->
case load_database(db) do
:ok -> :ok
{ :error, error } ->
Logger.error "Failed to load database" <>
" #{ inspect(db[:id]) }:" <>
" #{ inspect(error) }"
end
end)

{ :noreply, state }
end
Expand Down
47 changes: 47 additions & 0 deletions test/geolix/database/loader_error_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule Geolix.Database.LoaderErrorTest do
use ExUnit.Case, async: false

import ExUnit.CaptureLog

alias Geolix.Adapter.MMDB2
alias Geolix.Database.Supervisor, as: DatabaseSupervisor


setup do
databases = Application.get_env(:geolix, :databases)

on_exit fn ->
:ok = Application.put_env(:geolix, :databases, databases)
end
end

defp restart_supervisor() do
true =
DatabaseSupervisor
|> Process.whereis()
|> Process.exit(:kill)

:timer.sleep(250)

:ok
end


test "(re-) loading databases at startup logs errors" do
id = :initially_broken
db = %{
id: id,
adapter: MMDB2,
source: Path.join([ __DIR__, "does-not-exist" ])
}

assert capture_log(fn ->
:ok = Application.put_env(:geolix, :databases, [ db ])
:ok = restart_supervisor()
:ok = Geolix.reload_databases()

# ensure GenServer.cast/1 was processed
:timer.sleep(100)
end) =~ "does not exist"
end
end

0 comments on commit 08667ab

Please sign in to comment.