How do I modify the code search path for a remote Erlang node? - erlang

I'm connected to an Erlang node with -remsh. How do I modify the code path, in order to load a library that wasn't packaged into my release?

All necessary functions to manipulate code loading, path... are in the code module (see doc at erlang otp code module).
You could add the system paths to the list by doing the following:
[code:add_pathz(P) || P <- filelib:wildcard("/usr/lib/erlang/lib/*/ebin")].

After compiling some test code and connecting to a running node I was able to make it work with this:
(app#127.0.0.1)1> code:add_pathz("/path/to/my/compiled/beam").
(app#127.0.0.1)2> tester:hi().
hi!
ok
(app#127.0.0.1)3>

Related

Function from other module not detecting

I two modules in same src folder. mod1 declares function I wish to use in module mod2:
-module(mod1).
-export([myfunc/1]).
myfunc(A) -> {ok}.
In other module I not import mod1:
-module(mod2).
If I do "mod1:" in mod2 it recognizes "myfunc", problem is at run-time when I call mod1:myfunc(A) I get "undefined function mod1:myfunc/1"
I not understand why I get error if intellisense detect my mod1 function in mod2?
From the shell, you could try doing mod1:module_info(exports) to see the list of all the exported functions, though if your module is written as it is above, it should be generating a syntax error.
If, however, I'm wrong, and you actually do have it written properly in your module, (ie, it's just a typo here), try doing the following at the erlang shell:
c(mod1).
c(mod2).
And see if that works for you. This will compile and load the modules for you. If you don't have the module compiled (ie, it's just a .erl file in the directory), that's insufficient.
EDIT
Also, make sure that the beam files are being loaded properly when erlang launches. This is typically done by launching erl with erl -pa /path/to/beams

How to run an Erlang MapReduce function on Riak?

I'm new to Riak and new to Erlang. I have successfully created and run JS MapReduce jobs, now I'm trying to execute the following Reduce function:
http://web.archive.org/web/20130329021000/http://contrib.basho.com/delete_keys.html
What are the steps to execute this reduce function? Specifically,
Where should I put the code?
What should I type into addReducePhase( ) ?
Thanks
You should write something like:
MapReduceResult result = client.mapReduce("myBucket")
.addLinkPhase("bucketX", "test", false)
.addMapPhase(new NamedJSFunction("Riak.mapValuesJson"), false)
.addReducePhase(new NamedErlangFunction("riak_kv_mapreduce", "reduce_sort"), true)
.execute();
In this case the reduce phase call the function reduce_sort of erlang module riak_kv_mapreduce.
In your case you should call
.addReducePhase(new NamedErlangFunction("reduce_functions", "delete"), true)
(the true at the end is a flag that set if results have to be returned to client - last step - or not -intermediate step -)
The Erlang files should be compiled as .beam before use and added to 'add_paths' parameters in riak.config like
{add_paths, ["/home/vinz/riak/custom_modules/"]}
where /home/vinz/riak/custom_modules is the directory containing the compiled Erlang modules.
Refer to Basho Java Client readme for further information.
I hope this helps!

Lua check if a file is open or not

I'm trying to script a lua file to check if a certain file is open. Then I want it to close that file if it is open. I know how to check if the file exist but I need to know how to check if the file is open, meaning the file is running.
Lua, like C, C++, and pretty much every other language, can only close files that it opens itself. You cannot close files open by other people (not with standard Lua calls); this would be incredibly rude.
So you can't test to see if a file is opened by someone else. Nor can you close their file. There may be system API calls you could make to do this, but you would have to give Lua scripts access to those APIs yourself. Lua's standard libraries can't do this.
Sounds like you want to check which if any programs have a given file open.
first thing that comes to mind is parsing the output of lsof on linux.
fd = io.popen("lsof path/to/my/file")
fileopened = (#fd:read("a*") > 0)
Kind of a hacky way to do it, but it works:
processname = "process_name_here.exe"
filedata = io.popen("tasklist /NH /FO CSV /FI \"IMAGENAME eq "..processname.."\"")
output = filedata:read()
filedata:close()
if output ~= "INFO: No tasks are running which match the specified criteria." then
-- Program is running. Close the program
os.execute("taskkill -im "..processname)
else
-- Program is not running
end
Just make sure to replace "process_name_here.exe" with the process name that shows up in task manager
Alternatively you can just use this to close it without checking if it was actually running:
os.execute("taskkill -im process_name_here.exe")

Erlang: add libraries to application

I use erlIDE (based on Eclipse) to work on Erlang projects. Till today everything was fine, but today I have to use external library (couchbeam) in my application. I found out, what is hell, btw.)
The problem is simple – I cannot include external library to compiler path. I've used rebar to get couchbeam's dependencies and it also downloaded ibrowse, mochiweb and ejson.
How can I include those libraries to compiler path without modifing ERL_LIBS to work on project in erlIDE?
I do not want modify ERL_LIBS, because I can change projects's path, start new one (then I should modify ERL_LIBS again) and so on.
I've tried compiler options in erlIDE:
{pa, {pa, 'site_stater/deps/couchbeam/'}}
or
{pa, {pa, '../deps/couchbeam/'}}
where 'site_stater' – is project's name
I wonder how professional erlang programmers organaze their projects workflow (where they write erlang progs, how debuggin it, deal with external libraries and so on).
Many thanks for your attension.
UPDATE
I wrote simple function to load libraries, but I think it is still wrong way to deal with this problem:
load_libraries() ->
ProjectRoot = filename:join([filename:absname("./"), "site_stater"]),
{ok, DepsList} = file:list_dir(ProjectRoot ++ "/deps/"),
lists:foreach(fun (Folder) ->
RealFolder = ProjectRoot ++ "/deps/" ++ Folder,
case filelib:is_dir(RealFolder) of
true ->
code:add_patha(filename:join([RealFolder, "/ebin"]));
false -> ok
end
end,
DepsList),
ok.
I can't verify it right now, but you should be able to use {pa, '../deps/couchbeam/'} in the compiler options. If that doesn't work, please try using an absolute path.
The compiler settings are not finished yet, we plan to have some simpler way to refer to external libraries but we're not there yet. Every such query from users increases the importance of fixing it!
regards,
Vlad

Spawn remote process w/o common file system

(nodeA#foo.hyd.com)8> spawn(nodeA#bar.del.com, tut, test, [hello, 5]).
I want to spawn a process on bar.del.com which has no file system access to foo.hyd.com (from where I am spawning the process), running subroutine "test" of module "tut".
Is there a way to do so, w/o providing the nodeA#bar.del.com with the compiled "tut" module file?
You can use the following function to load a module at remote node without providing the file itself:
load_module(Node, Module) ->
{_Module, Bin, Filename} = code:get_object_code(Module),
rpc:call(Node, code, load_binary, [Module, Filename, Bin]).
As noted in code:load_binary/3 Filename argument is used only to track the path to module and the file it points to is not used by local node_server.
I'm interpreting your question as a desire to not copy the *.beams from your filesystem to the remote file system.
If you are just testing things you can use the nl(Mod) call in the erl shell to load the module on all (currently) known nodes. That is, those that show up in nodes().
This will send the code over and load it from the memory copy, it will not store it in the remote filesystem.
You can also start the remote node using the slave module. A slave accesses its master's filesystem and code server. Ordinary auto-loading will then make sure the module exists in the slave when you call your test:tut/2 function.
You can send the local code to the remote node:
> {Mod, Bin, File} = code:get_object_code(Module).
> rpc:call(RemoteNode, code, load_binary, [Mod, File, Bin]).

Resources