     end
   end
 
+  def handle_info({:DOWN, _, :process, pid, reason}, %{conn: %{pid: pid}} = state) do
+    {:stop, reason, state}
+  end
+
+  def handle_info({:DOWN, _, :process, pid, reason}, %{chan: %{pid: pid}} = state) do
+    {:stop, reason, state}
+  end
+
   def handle_info({:DOWN, _, :process, pid, reason}, state) do
     unless reason in [:normal, :noproc] do
       Logger.warn 3, "CeleryScript: #{inspect pid} died: #{inspect reason}"


  def init([]) do
    token = ConfigStorage.get_config_value(:string, "authorization", "token")

    import Farmbot.Jwt, only: [decode: 1]
    with {:ok, %{bot: device, mqtt: mqtt_host, vhost: vhost}} <- decode(token),
         {:ok, conn}  <- open_connection(token, device, mqtt_host, vhost),
         {:ok, chan}  <- AMQP.Channel.open(conn),
         q_base       <- device,


    end
  end

  defp open_connection(token, email, bot, mqtt_server, vhost) do
    opts = [
    client_properties: [
      {"version", :longstr, Farmbot.Project.version()},
      {"commit", :longstr, Farmbot.Project.commit()},
      {"target", :longstr, Farmbot.Project.target()},
      {"opened", :longstr, to_string(DateTime.utc_now())},
      {"product", :longstr, "farmbot_os"},
      {"bot", :longstr, bot},
      {"email", :longstr, email},
      {"node", :longstr, to_string(node())},
    ],
    host: mqtt_server,
    username: bot,
    password: token,
    virtual_host: vhost]

    case AMQP.Connection.open(opts) do
      {:ok, conn} -> {:ok, conn}
      {:error, reason} ->
        Logger.error 1, "Error connecting to AMPQ: #{inspect reason}"
        Process.sleep(5000)
        open_connection(token, email, bot, mqtt_server, vhost)
    end
  end

  def terminate(reason, state) do
