undefined start_link代理

时间:2017-11-12 17:52:02

标签: elixir phoenix-framework

我正在关注一本凤凰书并建立代理以使用以上几个后端:

# Proxy
defmodule Rumbl.InfoSys do
  # we will have several backends; wolfram, google search, google pictures, etc
  @backends [Rumbl.InfoSys.WolFram]

  defmodule Result do
    defstruct score: 0, text: nil, url: nil, backend: nil
  end

  def start_link(backend, query, query_ref, owner, limit) do
    backend.start_link(query, query_ref, owner, limit)
  end

  def compute(query, opts \\ []) do
    limit = opts[:limit] || 10
    backends = opts[:backends] || @backends

    backends
    |> Enum.map(&spawn_query(&1, query, limit))
  end

  defp spawn_query(backend, query, limit) do
    query_ref = make_ref()
    opts = [backend, query, query_ref, self(), limit]
    {:ok, pid} = Supervisor.start_child(Rumbl.InfoSys.Supervisor, opts)
    {pid, query_ref}
  end
end

现在我只定义了一个后端Rumbl.InfoSys.WolFram

这是wolfram后端的定义

defmodule Rumbl.InfoSys.Wolfram do
  import SweetXml
  alias Rumbl.InfoSys.Result

  def start_link(query, query_ref, owner, limit) do
    Task.start_link(__MODULE__, :fetch, [query, query_ref, owner, limit])
  end

  def fetch(query_str, query_ref, owner, _limit) do
    query_str
    |> fetch_xml()
    |> xpath(~x"/queryresult/pod[contains(@title, 'Result') or
                                 contains(@title, 'Definitions')]
                                 /subpod/plaintext/text()")
    |> send_results(query_ref, owner)
  end

  defp send_results(nil, query_ref, owner) do
    send(owner, {:results, query_ref, []})
  end

  defp send_results(answer, query_ref, owner) do
    results = [%Result{backend: "wolfram", score: 95, text: to_string(answer)}]
    send(owner, {:results, query_ref, results})
  end

  defp fetch_xml(query_str) do
    {:ok, {_, _, body}} = :httpc.request(
      String.to_char_list("http://api.wolframalpha.com/v2/query" <>
        "?appid=#{app_id()}" <>
        "&input=#{URI.encode(query_str)}&format=plaintext"))
    body
  end

  defp app_id, do: Application.get_env(:rumbl, :wolfram)[:app_id]
end

一旦你定义了wolfram后端,它会告诉你在shell上测试它:

iex> Rumbl.InfoSys.compute("what is elixir?")
[{#PID<0.1703.0>, #Reference<0.0.3.8938>}]

但对我来说,这只是工作:

iex(1)> Rumbl.InfoSys.compute("what is elixir?")
** (MatchError) no match of right hand side value: {:error, {:EXIT, {:undef, [{Rumbl.InfoSys.WolFram, :start_link, ["what is elixir?", #Reference<0.1501712023.3692298244.231940>, #PID<0.521.0>, 10], []}, {:supervisor, :do_start_child_i, 3, [file: 'supervisor.erl', line: 381]}, {:supervisor, :handle_call, 3, [file: 'supervisor.erl', line: 406]}, {:gen_server, :try_handle_call, 4, [file: 'gen_server.erl', line: 636]}, {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 665]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 247]}]}}}
    (rumbl) lib/rumbl/info_sys.ex:25: Rumbl.InfoSys.spawn_query/3
    (elixir) lib/enum.ex:1270: Enum."-map/2-lists^map/1-0-"/2

只要我理解它抱怨start_link Rumbl.InfoSys.Wolfram未定义?

:undef, [{Rumbl.InfoSys.WolFram, :start_link

我认为这与这本书有点旧有关,凤凰和elixir版本不同但不知道如何解决它。

1 个答案:

答案 0 :(得分:1)

看起来编译器正在说你的一切 - Supervisor.start_child返回一个错误元组,你的模式匹配在第25行失败:

{:ok, pid} = Supervisor.start_child(Rumbl.InfoSys.Supervisor, opts)

该消息还说明Rumbl.InfoSys.WolFram未定义,当我查看WolFram模块定义时,它的名称为Rumbl.InfoSys.Wolfram。请更新modul的名称并考虑在spawn_query / 3函数中添加模式匹配中的错误路径:)