|
@@ -0,0 +1,37 @@
|
|
|
|
|
+defmodule CvGen.Watchdog do
|
|
|
|
|
+ use GenServer
|
|
|
|
|
+ require Logger
|
|
|
|
|
+
|
|
|
|
|
+ def start_link(_), do: GenServer.start(__MODULE__, [])
|
|
|
|
|
+
|
|
|
|
|
+ @impl true
|
|
|
|
|
+ def init(_), do: {:ok, %{tpl_hash: "", json_hash: ""}, {:continue, :trigger_timer}}
|
|
|
|
|
+
|
|
|
|
|
+ @impl true
|
|
|
|
|
+ def handle_info(:timer_expired, state) do
|
|
|
|
|
+ state =
|
|
|
|
|
+ with tpl_hash <- hash("lib/templates/cv.html.eex"),
|
|
|
|
|
+ json_hash <- hash("lib/templates/cv.json") do
|
|
|
|
|
+ if tpl_hash != state.tpl_hash or json_hash != state.json_hash do
|
|
|
|
|
+ Logger.info("Content has changed, reloading")
|
|
|
|
|
+ CvGen.generate()
|
|
|
|
|
+ %{tpl_hash: tpl_hash, json_hash: json_hash}
|
|
|
|
|
+ else
|
|
|
|
|
+ state
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ {:noreply, state, {:continue, :trigger_timer}}
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ defp hash(path) do
|
|
|
|
|
+ contents = File.read!(path)
|
|
|
|
|
+ :crypto.hash(:md5, contents) |> Base.encode16()
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ @impl true
|
|
|
|
|
+ def handle_continue(:trigger_timer, state) do
|
|
|
|
|
+ Process.send_after(self(), :timer_expired, 100)
|
|
|
|
|
+ {:noreply, state}
|
|
|
|
|
+ end
|
|
|
|
|
+end
|