Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.register Not Applying to Atom

Tags:

elixir

I am currently trying to make an atom for a database connection to Redis. Usually I would use the name option in start of GenServer, but that is not available. In the Getting Started (Elixir 1.0.5), the example seems to show Process.register(pid, :kv) as an option. Maybe there is a better option to make this process accessible?

Following is showing how it doesn't match the guard in query for the connection client.

iex(2)> redis_client = Exredis.start
#PID<0.367.0>
iex(3)> Process.register(redis_client, :redis_client)
true
iex(4)> :redis_client |> Exredis.query ["GET", "test"]
** (FunctionClauseError) no function clause matching in Exredis.query/2
    (exredis) lib/exredis.ex:95: Exredis.query(:redis_client, ["GET", "test"])
iex(4)> redis_client |> Exredis.query ["GET", "test"] 
:undefined
iex(5)> redis_client |> Exredis.query ["SET", "test", "value"]
"OK"
iex(6)> redis_client |> Exredis.query ["GET", "test"]         
"value"
iex(7)> :redis_client
:redis_client
like image 635
rockerBOO Avatar asked Jul 05 '15 14:07

rockerBOO


1 Answers

Your error most likely comes from these lines in ExRedis' source:

def query(client, command) when is_pid(client) and is_list(command), do:
    client |> :eredis.q(command) |> elem(1)

As you can see, start_link/2 only accepts a pid as the first argument (when is_pid(client)).

You can use Process.whereis/1 to find the pid of a registered process and pass that to ExRedis:

iex> :redis_client |> Process.whereis |> Exredis.query(["SET", "test", "value"])
"OK"
like image 188
whatyouhide Avatar answered Nov 07 '22 10:11

whatyouhide