genserver实现打开redis状态
我想打开一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经拥有
IO.inspect(conn)
,它必须向您显示conn
是一个pid
。在下一行中,您尝试调用conn.pid
,它被视为函数调用,映射到:erlang.apply(#PID<0.1075.0>, :pid, [ ])
并且明显加注。以下最有可能做到这一点。旁注:使用
GenServer.handle_continue/2
回调,而不是向self
发送消息,而 self 正是为这种初始化而存在的。此外,通常会在应用程序监督树中启动
Redix
,如 文档。You already have
IO.inspect(conn)
which must have shown youconn
is apid
. Right in the next line you try to callconn.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.Sidenote: use
GenServer.handle_continue/2
callback instead of sending the message toself
which exists for exactly this kind of initialization.Also, one usually starts
Redix
within the application supervision tree as described in the documentation.