genserver实现打开redis状态

发布于 2025-01-20 22:25:57 字数 2115 浏览 0 评论 0原文

我想打开一个redis连接,并为系统内的持续连接编写了类似的代码。我正在这样做的方式正在调用使用redix客户端的handle_info函数。 I have defined the dependency for redix

def start_link(opts \\ [name: __MODULE__]) do
    GenServer.start_link(__MODULE__, nil, Keyword.merge(opts, name: __MODULE__))
  end

  @impl true
  def init(_) do
    send(self(), :connect)
    {:ok, nil}
  end

  @impl true
  def handle_info(:connect, _conn) do
    host = "redis://localhost:6379/3"

    case Redix.start_link(host) do
      {:ok, conn} ->
        IO.inspect(conn)
        Process.monitor(conn.pid)
        {:noreply, conn}

      {:error, _} ->
       
        Process.send_after(self(), :connect, @reconnect_interval)
        {:noreply, nil}
    end
  end

I call with redix client to the server with handle info.当我使用PID设置redis中的键 something like this

Redix.command!(conn, ["SET", "queue"])

I get this error

    [error] GenServer Zuppler.Utils.Redis.Connection terminating
** (RuntimeError) attempted to cast GenServer Zuppler.Utils.Redis.Connection but no handle_cast/2 clause was provided
    (zuppler_utils 0.1.12) lib/gen_server.ex:824: Zuppler.Utils.Redis.Connection.handle_cast/2
    (stdlib 3.17.1) gen_server.erl:695: :gen_server.try_dispatch/4
    (stdlib 3.17.1) gen_server.erl:771: :gen_server.handle_msg/6
    (stdlib 3.17.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: {:"$gen_cast", {:pipeline, [["llen", "list_name"]], {#PID<0.694.0>, #Reference<0.884395455.1828454401.122583>}, 5000}}
State: #PID<0.801.0>
** (EXIT from #PID<0.694.0>) shell process exited with reason: an exception was raised:
    ** (RuntimeError) attempted to cast GenServer Zuppler.Utils.Redis.Connection but no handle_cast/2 clause was provided
        (zuppler_utils 0.1.12) lib/gen_server.ex:824: Zuppler.Utils.Redis.Connection.handle_cast/2
        (stdlib 3.17.1) gen_server.erl:695: :gen_server.try_dispatch/4
        (stdlib 3.17.1) gen_server.erl:771: :gen_server.handle_msg/6
        (stdlib 3.17.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

I want to open a Redis connection and I have written a code something like this for persistent connection within the system. The way I'm doing this I'm calling a handle_info function which uses the Redix client. I have defined the dependency for redix

def start_link(opts \\ [name: __MODULE__]) do
    GenServer.start_link(__MODULE__, nil, Keyword.merge(opts, name: __MODULE__))
  end

  @impl true
  def init(_) do
    send(self(), :connect)
    {:ok, nil}
  end

  @impl true
  def handle_info(:connect, _conn) do
    host = "redis://localhost:6379/3"

    case Redix.start_link(host) do
      {:ok, conn} ->
        IO.inspect(conn)
        Process.monitor(conn.pid)
        {:noreply, conn}

      {:error, _} ->
       
        Process.send_after(self(), :connect, @reconnect_interval)
        {:noreply, nil}
    end
  end

I call with redix client to the server with handle info. When I use the PID to set a key in Redis
something like this

Redix.command!(conn, ["SET", "queue"])

I get this error

    [error] GenServer Zuppler.Utils.Redis.Connection terminating
** (RuntimeError) attempted to cast GenServer Zuppler.Utils.Redis.Connection but no handle_cast/2 clause was provided
    (zuppler_utils 0.1.12) lib/gen_server.ex:824: Zuppler.Utils.Redis.Connection.handle_cast/2
    (stdlib 3.17.1) gen_server.erl:695: :gen_server.try_dispatch/4
    (stdlib 3.17.1) gen_server.erl:771: :gen_server.handle_msg/6
    (stdlib 3.17.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: {:"$gen_cast", {:pipeline, [["llen", "list_name"]], {#PID<0.694.0>, #Reference<0.884395455.1828454401.122583>}, 5000}}
State: #PID<0.801.0>
** (EXIT from #PID<0.694.0>) shell process exited with reason: an exception was raised:
    ** (RuntimeError) attempted to cast GenServer Zuppler.Utils.Redis.Connection but no handle_cast/2 clause was provided
        (zuppler_utils 0.1.12) lib/gen_server.ex:824: Zuppler.Utils.Redis.Connection.handle_cast/2
        (stdlib 3.17.1) gen_server.erl:695: :gen_server.try_dispatch/4
        (stdlib 3.17.1) gen_server.erl:771: :gen_server.handle_msg/6
        (stdlib 3.17.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡间太子 2025-01-27 22:25:57

您已经拥有 IO.inspect(conn),它必须向您显示 conn 是一个 pid。在下一行中,您尝试调用 conn.pid ,它被视为函数调用,映射到 :erlang.apply(#PID<0.1075.0>, :pid, [ ]) 并且明显加注。以下最有可能做到这一点。

- Process.monitor(conn.pid)
+ Process.monitor(conn)

旁注:使用 GenServer.handle_continue/2 回调,而不是向 self 发送消息,而 self 正是为这种初始化而存在的。

此外,通常会在应用程序监督树中启动 Redix,如 文档

You already have IO.inspect(conn) which must have shown you conn is a pid. Right in the next line you try to call conn.pid which is treated as a function call, which maps to :erlang.apply(#PID<0.1075.0>, :pid, []) and obviously raises. The following would most likely do.

- Process.monitor(conn.pid)
+ Process.monitor(conn)

Sidenote: use GenServer.handle_continue/2 callback instead of sending the message to self which exists for exactly this kind of initialization.

Also, one usually starts Redix within the application supervision tree as described in the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文