elixir mix app无法理解清楚

时间:2017-05-24 07:31:01

标签: erlang elixir otp

我尝试使用灵药。

关于application.ex

有点难以理解
defmodule PluralsightTweet.Application do
  # See http://elixir-lang.org/docs/stable/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    # Define workers and child supervisors to be supervised
    children = [
      # Starts a worker by calling: PluralsightTweet.Worker.start_link(arg1, arg2, arg3)
       worker(PluralsightTweet.TweetServer, [])
    ]

    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: PluralsightTweet.Supervisor]
    process = Supervisor.start_link(children, opts)
    PluralsightTweet.Scheduler.schedule_file("* * * * *", Path.join("#{:code.priv_dir(:pluralsight_tweet)}",
    "sample.txt"))
    process
  end
end

我正在关注复数灵药教程 这是调度程序,可以在阅读文本文件的每一分钟内发送文本

任务是成功的,但对过程没有明确的理想

有人可以解释一下application.ex中发生的事情 作为主管应用程序运行

1 个答案:

答案 0 :(得分:1)

use Application

此行表示当前模块是应用程序的入口。可以在mix.exs中配置此模块作为一个单元启动。

# Inside mix.exs
def application do
  [
    extra_applications: [:logger],
    mod: {PluralsightTweet.Application, []}  # <-- this line
  ]
end

start函数

此功能是应用程序启动时的回调。您可以将其视为其他语言中的main函数。

import Supervisor.Spec, warn: false

您只需在拨打workersupervisorsupervise时省略模块名称即可。即使您没有调用任何这些功能,warn: false部分也会禁止警告。

children = [worker(PluralsightTweet.TweetServer, [])]

此行指定应用程序监督的子进程。请注意,此时,子进程尚未生成。

worker(mod, args)只定义了稍后将启动的工作规范。启动工作人员时,args将传递给mod的{​​{1}}函数。

start_link

主管选项。

有关opts = [strategy: :one_for_one, name: PluralsightTweet.Supervisor]及其他策略的含义,请参阅strategies documentation

由于您只有一名工作人员,因此strategy: :one_for_one以外的所有策略的工作方式都相同。

棘手的部分是:simple_one_for_one。你可能想知道模块name: PluralsightTweet.Supervisor来自哪里。事实是它不是一个模块。它只是一个原子PluralsightTweet.Supervisor,它作为管理程序进程的名称。

:"Elixir.PluralsightTweet.Supervisor"

现在生成了主管流程及其子流程。