简体   繁体   English

Phoenix可以调用Elixir应用程序吗?

[英]Can Phoenix call an Elixir app?

I have an Elixir app running on a cluster of nodes (n1@127.0.0.1 and n2@127.0.0.1), let's call it Myapp.Server . 我有一个运行在节点集群(n1@127.0.0.1和n2@127.0.0.1)上的Elixir应用程序,我们称之为Myapp.Server

Inside of it, I have a module run(parameter) which goal is to contact a Rest Web Service to grab some data (parameter is the number of data to be grabbed), transform it into XML then put it into a file which will be finally transferred to an external FTP. 在其中,我有一个模块run(parameter) ,其目标是联系Rest Web Service来获取一些数据(parameter是要获取的数据数量),将其转换为XML,然后将其放入一个文件中,该文件将最终转移到外部FTP。

Calling 调用

Myapp.Server.run(any_number)

is thus giving me the expected result. 因此给了我预期的结果。

I'm just in the beginning of learning Elixir and Phoenix, but two questions are constantly popping in my mind and despite my efforts, I can't find any hints. 我才刚刚开始学习Elixir和Phoenix,但是我脑海中不断浮现着两个问题,尽管我做了很多努力,但我找不到任何提示。 (or probably I'm not able to clearly understand it) (或者我可能无法清楚地理解它)

So my two questions, 所以我的两个问题

Can an Elixir application (running on a node or cluster of nodes) broadcast to a Phoenix application? Elixir应用程序(在节点或节点群集上运行)可以广播到Phoenix应用程序吗? (for an end user to be aware of a result for example) (例如,让最终用户知道结果)

On the other way, 另一方面,

Can we initiate a request to launch an Elixir module by clicking on a button within a Phoenix page running on a different node? 我们是否可以通过单击在不同节点上运行的Phoenix页面中的按钮来启动启动Elixir模块的请求?

Can please someone guide me on this or point me an article? 可以有人指导我或给我写一篇文章吗?

Regards, 问候,

Pierre 皮埃尔

PS: What I'm reading so far. PS:到目前为止,我正在阅读。

  • Programming Elixir 1.2 by Dave Thomas 编程Elixir 1.2,作者Dave Thomas
  • Programming Phoenix by Chris McCord, Bruce Tate and José Valim) 克里斯·麦克科德(Chris McCord),布鲁斯·泰特(Bruce Tate)和何塞·瓦利姆(JoséValim)编写的《凤凰卫视》
  • Udemy/Elixir intro from Mr. EMSON EMSON先生的Udemy / Elixir简介

A Phoenix app can absolutely call Elixir functions on other nodes (as long as the network is allowing the communication). Phoenix应用程序可以绝对调用其他节点上的Elixir函数(只要网络允许通信即可)。 Your Phoenix app is just another elixir app with nothing really special about it. 您的Phoenix应用程序只是另一个灵丹妙药应用程序,没有什么特别的。

To call functions on another node, see the Process.send function, specifically where the dest is a tuple: {atom, node} . 要在另一个节点上调用函数,请参见Process.send函数,尤其是dest是元组的地方: {atom, node} The atom is a named process on the other node and the node is the name of the node, in your case n1@127.0.0.1 . 原子是另一个节点上的命名进程,而该节点是该节点的名称,在本例中为n1@127.0.0.1 ie. 即。 send({:SomeNamedProcess, "n1@127.0.0.1"} some_message)

GenServers also can accept this {atom, node} tuple as referenced in call , cast and the server type . GenServers还可以接受在callcast服务器类型中引用的{atom, node}元组。 ie. 即。 GenServer.call({:SomeNamedProcess, "n1@127.0.0.1"}, {:do_something, some_message}) . GenServer.call({:SomeNamedProcess, "n1@127.0.0.1"}, {:do_something, some_message})

Additionally, you can spawn ad-hoc functions on another node using the [Node.spawn_link](e. send({:SomeNamedProcess, "n1@127.0.0.1"} some_message) ) function. 另外,您可以使用[Node.spawn_link](例如send({:SomeNamedProcess, "n1@127.0.0.1"} some_message) )函数在另一个节点上生成即席函数。 ie. 即。 Node.spawn_link :"n1@127.0.0.1", fn -> IO.puts("Hello from #{inspect self}") end . Node.spawn_link :"n1@127.0.0.1", fn -> IO.puts("Hello from #{inspect self}") end

You may also choose to spawn Elixir Tasks on another node . 您也可以选择在另一个节点上生成Elixir任务

Choosing one of these techniques is really a matter of your exact use case. 选择其中一种技术实际上取决于您的确切用例。

You may abein terested in process registries as another level of indirection between the nodes: 您可能会对进程注册表感到不满意,因为它们是节点之间的另一种间接层:

https://m.alphasights.com/process-registry-in-elixir-a-practical-example-4500ee7c0dcc#.sws1ye9e9 https://m.alphasights.com/process-registry-in-elixir-a-practical-example-4500ee7c0dcc#.sws1ye9e9

https://medium.com/@StevenLeiva1/elixir-process-registries-a27f813d94e3#.5xvkv03k5 https://medium.com/@StevenLeiva1/elixir-process-registries-a27f813d94e3#.5xvkv03k5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM