Created
February 20, 2016 12:26
-
-
Save patrobinson/c802202c33d17a54f2fd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
defmodule DieCloudfilesDie.Devil.Minion do | |
use GenServer | |
@name __MODULE__ | |
def start_link(container, agent_pid, marker_pid, parent_pid) do | |
{:ok, pid} = GenServer.start_link(__MODULE__, [container, agent_pid, marker_pid, parent_pid], name: @name) | |
end | |
def init(container, agent_pid, marker_pid, parent_pid) do | |
# Retrieve Marker | |
marker = DieCloudfilesDie.Devil.MarkerAgent.get_marker marker_pid | |
{container, marker, agent_pid} = add_files(container, marker, agent_pid, parent_pid) | |
# Should never get here? | |
{:ok, {container, marker, agent_pid, marker_pid}} | |
end | |
def add_files(container, marker, agent_pid, parent_pid) do | |
# Retrieve some files | |
files = Rackspace.Api.CloudFiles.Object.list(container, %{region: "DFW", limit: 1000, marker: marker}) |> Enum.map(fn(x) -> URI.encode(x.name) end) | |
case files do | |
[] -> | |
send parent_pid, {:ok, "No more files to collect"} | |
exit(:shutdown) | |
nil -> send parent_pid, {:error, "Got nil!"} | |
_ -> | |
# Push files onto queue | |
DieCloudfilesDie.Devil.Agent.add_files(agent_pid, files) | |
# Update marker | |
marker = List.last(files) | |
send parent_pid, {:ok, "At marker #{marker}", self} | |
end | |
# Work recursively | |
add_files(container, marker, agent_pid, parent_pid) | |
end | |
def terminate(_reason, {container, marker, agent_pid, marker_pid, parent_pid}) do | |
# Push marker | |
DieCloudfilesDie.Devil.MarkerAgent.set_marker marker_pid, marker | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment