[Erlang_Question27]如何监控指定目录下的*.beam文件,如果有改动就更新到指定的节点?

在Erlang In Anger第二章中讲到使用rebar来创建一个Erlang项目(Application或Project)

但美中不足的只是给出了指引,但没有给出详细的步骤。

下面我们就使用rebar一步步来创建一个典型的Application。

但在此之前,最好先理解下Erlang In Anger中第二章节所讲关于application结构的部分:http://zhongwencool.gitbooks.io/erlang_in_anger/

通过本小节,你可以了解使用rebar加入开源依赖项目的完整步骤。

本示例github地址:https://github.com/zhongwencool/autoload

1. 下载并安装rebar:

$ git clone git://github.com/rebar/rebar.git
$ cd rebar
$ ./bootstrap
Recompile: src/getopt
...
Recompile: src/rebar_utils
==> rebar (compile)
Congratulations! You now have a self-contained script called "rebar" in
your current working directory. Place this script anywhere in your path
and you can use rebar to build OTP-compliant apps.
为了全局使用rebar,把你的路径加到PATH下:在~/.profile里面加入:
export PATH=$PATH:Your_Rebar_Path
然后重启你的shell或source ~/.profile使它生效
2.创建application:
./rebar create-app appid=autoload
==> rebar (create-app)
Writing src/autoload.app.src
Writing src/autoload_app.erl
Writing src/autoload_sup.erl
生成上面三个模板文件(也可以修改rebar配置自定义模板),
  • myapp.app.src - The OTP application specification用于rebar自动生成.app文件放到ebin/下。
  • myapp_app.erl - An implementation of the OTP Application behaviour:实现你代码逻辑的地方。
  • myapp_sup.erl - An implementation of the OTP Supervisor behaviour (top level supervisor of the application)此application最高等级的监控树。

3.监控指定目录的逻辑是用的开源项目:inotify: https://github.com/sheyll/inotify

因为不想去自己用erlang实现一个每秒循环监控目录下文件的gen_server【这样很没效率喂!】

其实把引用依赖项目很简单,只需要在rebar.config里面加入:

{deps, [{inotify, ".*", {git,
"https://github.com/sheyll/inotify.git"}}]}.

4. 编写自己的逻辑:

4.1 把指定节点名和cookie,指定的监控目录,和log文件存放的位置都放到application的配置文件autoload.app.src中:

{env, [{autoload_path,"./dev_patch/"},   {node_cookie,[{[email protected],best},{[email protected],best}]},  
 {log,"./log/console.log"}]}. 

这样设置后就可以在代码里使用:

{ok,NodeCookies} = application:get_env(autoload,node_cookie). 

这样的函数在全局中得到对应的值。

4.2 建立一个autoload.erl(gen_server)把它放到auto_load_sup监控树下: 在auto_load_sup.erl里面修改:

 init([]) ->
       {ok, { {one_for_one, 5, 10}, [?CHILD(autoload,worker)]} }.

4.3 在autoload.erl gen_server里面写逻辑:

其实关键在于如何高效地监控到目录下文件的变化,如果这个搞定啦,其它都是小case。

5. 编译代码:

$rebar get-deps
$ rebar compile

6. 启动application

erl -pa "./ebin" -pz "./dev_patch" -sname autoload -s autoload -detached

把启动命令写到start.sh里面

这个命令会启动application并把日志记录在log/console.log中

  使用下面命令查看日志:

tail ./log/console.log -f

以上就是使用依赖项目创建自己的application的过程,

结论:rebar非常简单好用。

PS:rebar还集成了release,eunit,coverage statistics doc功能。

如果感兴趣,可以去官网了解下,非常全面的文档哦。

参考资料:

1.Rebar github地址:https://github.com/rebar/rebar

2.Erlang In Anger中英对照版本:http://zhongwencool.gitbooks.io/erlang_in_anger/

3.本项目github地址:https://github.com/zhongwencool/autoload


时间: 2024-08-05 00:17:34

[Erlang_Question27]如何监控指定目录下的*.beam文件,如果有改动就更新到指定的节点?的相关文章

python之查询指定目录下的最新文件

使用os模块查询指定目录下的最新文件 1 import os 2 3 # 输入目录路径,输出最新文件完整路径 4 def find_new_file(dir): 5 '''查找目录下最新的文件''' 6 file_lists = os.listdir(dir) 7 file_lists.sort(key=lambda fn: os.path.getmtime(dir + "\\" + fn) 8 if not os.path.isdir(dir + "\\" + f

c# 获取指定目录下的所有文件并显示在网页上

参考文献: FileInfo 的使用  https://msdn.microsoft.com/zh-cn/library/system.io.fileinfo_methods(v=vs.110).aspx 网页表格的生成  http://www.w3school.com.cn/html/html_tables.asp C# 通过文件扩展名获取图标和描述 http://www.csframework.com/archive/2/arc-2-20110514-1478.htm   http://ww

javaFile循环列出指定目录下的所有文件(源代码)

package javatest.basic22; import java.io.File; import java.io.IOException; public class FileTest { public void getFileName(File file){ //判断是否是文件 if(file.isFile()){ System.out.println(file.getPath()+file.getName()); }else{ //如果是目录,列出当前目录下所有目录 File[] f

Windows Phone获得IsolatedStorage中指定目录下的所有文件

在Windows Phone 中对隔离存储空间中的文件操作需要通过System.Io.IsolatedStorage下的类进行操作 获得指定文件夹下的所有文件: 参数:是指定文件夹的路径加上通配符,格式:\folder1\* List<string> GetFileNames(string _strFolder) { List<string> returnlst = new List<string>(); using (IsolatedStorageFile stora

使用for循环创建在指定目录下批量创建文件并重命名所有文件

要求1: 使用for循环在/root/scripts/practice/q1/oldboy 目录下创建十个文件.名称依次为: oldboy-1, oldboy-2, ..... oldboy-10. 脚本实现: #!/bin/bash #Question1 shopt -s expand_aliases alias ll="ls -l" cd /root/scripts/practice/q1 #首先判断目录是否存在,不存在则创建目录 if [ ! -e oldboy ] then  

linux复制指定目录下的全部文件到另一个目录中

linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir2.怎样才能将dir1下所有文件复制到dir2下了如果dir2目录不存在,则可以直接使用cp -r dir1 dir2即可.如果dir2目录已存在,则需要使用cp -r dir1/. dir2如果这时使用cp -r dir1 dir2,则也会将dir1目录复制到dir2中,明显不符合要求.ps:di

C#直接删除指定目录下的所有文件及文件夹(保留目录)

? ????#region 直接删除指定目录下的所有文件及文件夹(保留目录) /// <summary> /// 直接删除指定目录下的所有文件及文件夹(保留目录) /// </summary> /// <param name="strPath">文件夹路径</param> /// <returns>执行结果</returns> public bool DeleteDir(string strPath) { try

linux复制指定目录下的全部文件到另一个目录

linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir2.怎样才能将dir1下所有文件复制到dir2下了如果dir2目录不存在,则可以直接使用cp -r dir1 dir2即可.如果dir2目录已存在,则需要使用cp -r dir1/. dir2如果这时使用cp -r dir1 dir2,则也会将dir1目录复制到dir2中,明显不符合要求.ps:di

SHELL编程练习-获得指定目录下的所有文件及文件夹的大小

一.目的编写一个脚本,可以获得指定目录下的所有文件及文件夹的大小.输出的样式与 ls -lh 命令相同.命令执行示例,其中和 ls -lh 命令输出的唯一区别是目录的大小:1.1.ls -lh命令的输出(注意目录大小) [[email protected] ~]# ls -lh /root/dir1/ total 24M drwxr-xr-x 3 root root 4.0K Feb 6 13:54 dir2 drwxr-xr-x 2 root root 4.0K Feb 6 13:55 dir