Skip to content

Instantly share code, notes, and snippets.

@matheusca
Last active December 27, 2015 18:29
Show Gist options
  • Save matheusca/7370352 to your computer and use it in GitHub Desktop.
Save matheusca/7370352 to your computer and use it in GitHub Desktop.
defmodule Test.Start.File do
use GenServer.Behaviour
def start do
Test.Supervisor.start_link(Test.Supervisor, [])
enable
end
def enable do
Test.Supervisor.start_child(Test.Supervisor, __MODULE__, [])
if Process.whereis(__MODULE__) do
Process.put(:elixir_ensure_compiled, true)
Process.flag(:error_handler, Test.ErrorHandler)
:ok
else
:error
end
end
def start_link do
:gen_server.start({ :local, __MODULE__ }, __MODULE__, [], [])
end
def init(_args) do
{:ok, "./test.ex"}
end
def handle_call(:last_date, _from, file) do
{:ok, File.Stat[mtime: mtime]} = File.stat(file)
{:reply, mtime, file}
end
def load_files do
try do
Kernel.ParallelRequire.files(Path.wildcard("./*.ex"))
catch
kind, reason ->
:erlang.raise(kind, reason, System.stacktrace)
end
end
end
defmodule Test.ErrorHandler do
def undefined_function(module, fun, args) do
load_module(module)
:error_handler.undefined_function(module, fun, args)
end
defp load_module(module) do
case Code.ensure_loaded(module) do
{:module, _} -> :ok
{:error, _} -> Test.Start.File.load_files
end
end
end
defmodule Test.Supervisor do
use Supervisor.Behaviour
def start_link(name, opts) do
:supervisor.start_link({:local, name}, __MODULE__, opts)
end
def start_child(supervisor, name, args, opts // []) do
:supervisor.start_child(supervisor, worker(name, args, opts))
end
def init(args) do
opts = Keyword.put args, :strategy, :one_for_one
supervise([], [{:strategy, :one_for_one}])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment