fix image uploader

pull/363/head
connor rigby 2017-11-09 14:25:53 -08:00
parent 8d5157546f
commit b9479fd266
3 changed files with 35 additions and 30 deletions

View File

@ -10,51 +10,53 @@ defmodule Farmbot.HTTP.ImageUploader do
@doc """
Starts the Image Watcher
"""
def start_link() do
def start_link do
GenServer.start_link(__MODULE__, [], [name: __MODULE__])
end
def init([]) do
File.rm_rf!(@images_path)
File.mkdir_p!(@images_path)
Logger.debug 3, "Ensuring #{@images_path} exists."
Application.stop(:fs)
Application.put_env(:fs, :path, @images_path)
File.rm_rf! @images_path
File.mkdir_p! @images_path
:fs_app.start(:normal, [])
:fs.subscribe()
Process.flag(:trap_exit, true)
{:ok, %{uploads: %{}}}
end
def handle_info({_pid, {:fs, :file_event}, {path, [:modified, :closed]}}, state) do
if matches_any_pattern?(path, [~r{/tmp/images/.*(jpg|jpeg|png|gif)}]) do
[x, y, z] = [-9000, -9000, -9000]
#FIXME
def handle_info({_pid, {:fs, :file_event}, {path, _}}, state) do
matches? = matches_any_pattern?(path, [~r{/tmp/images/.*(jpg|jpeg|png|gif)}])
already_uploading? = Enum.find(state.uploads, fn({_pid, {find_path, _meta, _count}}) ->
find_path == path
end) |> is_nil() |> Kernel.!()
if matches? and (not already_uploading?) do
Logger.info 2, "Uploading: #{path}"
%{x: x, y: y, z: z} = Farmbot.BotState.get_current_pos()
meta = %{x: x, y: y, z: z}
pid = spawn(__MODULE__, :upload, [state.http, path, meta])
pid = spawn __MODULE__, :upload, [path, meta]
{:noreply, %{state | uploads: Map.put(state.uploads, pid, {path, meta, 0})}}
else
# Logger.warn 3, "Not uploading: match: #{matches?} already_uploading?: #{already_uploading?}"
{:noreply, state}
end
end
def handle_info({:EXIT, pid, reason}, state) do
case state.uploads[pid] do
nil ->
{:noreply, state}
nil -> {:noreply, state}
{path, _meta, 6 = ret} ->
Logger.error(2, "Failed to upload #{path} #{ret} times. Giving up.")
File.rm(path)
Logger.error 1, "Failed to upload #{path} #{ret} times. Giving up."
File.rm path
{:noreply, %{state | uploads: Map.delete(state.uploads, pid)}}
{path, meta, retries} ->
Logger.warn(2, "Failed to upload #{path} #{inspect(reason)}. Going to retry.")
{path, meta, retries} ->
Logger.warn 2, "Failed to upload #{path} #{inspect reason}. Going to retry."
Process.sleep(1000 * retries)
new_pid = spawn(__MODULE__, :upload, [path, meta])
new_uploads =
state
new_pid = spawn __MODULE__, :upload, [path, meta]
new_uploads = state
|> Map.delete(pid)
|> Map.put(new_pid, {path, meta, retries + 1})
{:noreply, %{state | uploads: new_uploads}}
end
end
@ -67,16 +69,20 @@ defmodule Farmbot.HTTP.ImageUploader do
# /lib/phoenix_live_reload/channel.ex#L27
defp matches_any_pattern?(path, patterns) do
path = to_string(path)
Enum.any?(patterns, fn pattern ->
String.match?(path, pattern)
end)
if String.contains?(path, "~") do
false
else
Enum.any?(patterns, fn pattern ->
String.match?(path, pattern)
end)
end
end
@spec upload(Path.t, map) :: {:ok, any} | {:error, any}
def upload(file_path, meta) do
Logger.busy(3, "Image Watcher trying to upload #{file_path}", type: :busy)
Logger.busy 3, "Image Watcher trying to upload #{file_path}"
Farmbot.HTTP.upload_file(file_path, meta)
File.rm!(file_path)
Logger.success(3, "Image Watcher uploaded #{file_path}", type: :success)
Logger.success 3, "Image Watcher uploaded #{file_path}"
end
end

View File

@ -10,7 +10,8 @@ defmodule Farmbot.HTTP.Supervisor do
def init([]) do
children = [
worker(Farmbot.HTTP, [])
worker(Farmbot.HTTP, []),
worker(Farmbot.HTTP.ImageUploader, [])
]
opts = [strategy: :one_for_all]

View File

@ -115,10 +115,8 @@ defmodule Farmbot.Mixfile do
{:nerves_runtime, "~> 0.4"},
{:nerves_firmware_ssh, "~> 0.2"},
{:nerves_network, "~> 0.3", github: "nerves-project/nerves_network", override: true},
# {:dhcp_server, path: "/home/connor/oss/elixir/nerves/dhcp_server", override: true},
{:dhcp_server, github: "nerves-project/dhcp_server", branch: "elixirize-go!", override: true},
# {:nerves_init_gadget, github: "nerves-project/nerves_init_gadget", branch: "dhcp", only: :dev},
# {:nerves_init_gadget, path: "/home/connor/oss/elixir/nerves/nerves_init_gadget", branch: "dhcp", only: :dev},
]
end