Elixir Redix 基于名称的池示例 - 主管签名不存在

Elixir Redix name-based pool example - Supervisor signature doesn't exist

所以我在遵循这个 "super simple" 示例时遇到了问题。 Elixir 新手(主要来自 Ruby 背景)。

只是尝试在主应用程序监督树下创建一个 redis 连接池设置以用于活动用户会话,但实际上 redis 将在未来更有用,所以我正在尝试进行此设置对了。

This line:
start: {Supervisor, :start_link, [children_redix]}

 Gives me the following error:

** (Mix) Could not start application gametime: 
Gametime.Application.start(:normal, []) returned an error: shutdown: 
failed to start child: RedixSupervisor
** (EXIT) an exception was raised:
    ** (UndefinedFunctionError) function Supervisor.start_link/1 is undefined or private
        (elixir) Supervisor.start_link([%{id: {Redix, 0}, start: {Redix, :start_link, [[name: :redix_0]]}, type: :worker}, %{id: {Redix, 1}, start: {Redix, :start_link, [[name: :redix_1]]}, type: :worker}, %{id: {Redix, 2}, start: {Redix, :start_link, [[name: :redix_2]]}, type: :worker}, %{id: {Redix, 3}, start: {Redix, :start_link, [[name: :redix_3]]}, type: :worker}, %{id: {Redix, 4}, start: {Redix, :start_link, [[name: :redix_4]]}, type: :worker}])

如果我从上一行的 children_redix 中删除方括号,我会得到:

 ** (UndefinedFunctionError) function Supervisor.start_link/5 is undefined or private

这是我正在关注的文档:

https://hexdocs.pm/redix/real-world-usage.html

这是应用程序启动函数:

   defmodule Gametime.Application do                                                            
      use Application                                                                            
      use Supervisor                                                                           


      def start(_type, _args) do                                                                 

        pool_size = 5                                                                            

        #creates redix children processes                                                                                   
        children_redix =                                                                         
          for i <- 0..(pool_size - 1) do                                                         
            Supervisor.child_spec({Redix, name: :"redix_#{i}"}, id: {Redix, i})                  
          end                                                                                    

        children = [                                                                             
          # child spec for the supervisor I am trying to add to the main supervision tree,                                                                                
          %{                                                                                     
            id: RedixSupervisor,                                                                 
            type: :supervisor,                                                                   
            start: {Supervisor, :start_link, [children_redix]}                                   
          },                                                                                     

          # This way is now deprecated  - although this was generated by phoenix so not going to touch this just yet.                                       
          supervisor(Gametime.Repo, []),                                                                                    
          supervisor(GametimeWeb.Endpoint, []),                                                  

          # Start your own worker by calling: Gametime.Worker.start_link(arg1, arg2, arg3)       
          # worker(Gametime.Worker, [arg1, arg2, arg3]),                                         
        ]                                                                                        


        opts = [strategy: :one_for_one, name: Gametime.Supervisor]                                                                                             
        Supervisor.start_link(children, opts)                                                    
      end 

我只是不确定我哪里出错了。我觉得这可能是文档中关于我应该知道什么的假设——但不幸的是,我不知道我不知道什么。任何帮助都会很棒 - 干杯。

您可能需要换行:

Supervisor.child_spec(%{ id: RedixSupervisor, type: :supervisor, start: {RedixSupervisor, :start_link, [children_redix]} })

并且在 start 元组上使用您的 RedixSupervisor 模块,而不是通用的 Supervisor。从 RedixSupervisor 中定义的 start_link/1 中,您将在其中接收参数(在本例中为 children_redixSupervisor.start_link,并使用传递的子项作为参数init 函数

例如:

defmodule RedixSupervisor do
  use Supervisor
  require Logger

  def start_link(children_redix) do
    Logger.info("RedixSupervisor Start Link")
    Supervisor.start_link(__MODULE__, children_redix, name: __MODULE__)
  end

  def init(children_redix) do
    children = children_redix
    Supervisor.init(children, strategy: :one_for_one)
  end
end

哇哦 - 所以我猜文档已经过时了。经历了一些痛苦来弄清楚这一点 - 所以我想我会分享。

...   %{                                                                                                
      id: RedixSupervisor,                                                                            
      type: :supervisor,                                                                              
      start: {Supervisor, :start_link, [  children_redix , [strategy: :one_for_one] ]}                                                                        
    },  ...

就是答案。文档似乎只是通过了 children_redix child_spec 映射 - 但这不是 Supervisor.start_link/2 期望的签名。

故事中我可能遗漏了更多内容,并且 how to 中可能还有一些我没有注意到的内容,因为可能假设人们应该已经知道什么。

干杯