 
          {:ok, _tag}  <- Basic.consume(chan, q_base <> "_from_clients", self(), [no_ack: true]),
          {:ok, _tag}  <- Basic.consume(chan, q_base <> "_auto_sync", self(), [no_ack: true]),
-         opts      <- [conn: conn, chan: chan, bot: device],
-         state <- struct(State, opts)
+         state <- %State{conn: conn, chan: chan, bot: device}
     do
-      true = Process.link(conn.pid)
-      true = Process.link(chan.pid)
+      _ = Process.monitor(conn.pid)
+      _ = Process.monitor(chan.pid)
+      _ = Process.flag(:sensitive, true)
+      _ = Process.flag(:trap_exit, true)
       {:consumer, state, subscribe_to: [Farmbot.BotState, Farmbot.Logger]}
     else
       {:error, {:auth_failure, msg}} = fail ->


  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
