Last active
February 5, 2020 06:50
-
-
Save iantbutler01/22b0b7a7d05aaee385b7a8e66eb75f15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CrawlerExample.Queue do | |
use GenServer | |
#client | |
def start_link(opts \\ []) do | |
GenServer.start_link(__MODULE__, nil, opts) | |
end | |
#server | |
@impl true | |
def init(_init_arg) do | |
schedule_write_to_disk(5000) | |
{:ok, :queue.new} | |
end | |
@impl true | |
def handle_info(:schedule_write_to_disk, state) do | |
current_state = :queue.to_list(state) | |
state_json = Poison.encode!(current_state) | |
File.write("/tmp/crawler_state.json", state_json) | |
schedule_write_to_disk(interval) | |
end | |
defp schedule_write_to_disk(interval), do: :erlang.send_after(interval, self(), :schedule_write_to_disk) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment