esl / MongooseIM

MongooseIM is Erlang Solutions' robust, scalable and efficient XMPP server, aimed at large installations. Specifically designed for enterprise purposes, it is fault-tolerant and can utilise the resources of multiple clustered machines.
Other
1.67k stars 427 forks source link

Custom Module not working #1974

Closed areno97 closed 6 years ago

areno97 commented 6 years ago

MongooseIM version: 3.0.0 Installed from: pkg Erlang/OTP version: 18 Ubuntu 16.04

I am having trouble creating a standard base for a custom module. I want to create a simple hello world program as outlined in the documentation for ejabberd (https://docs.ejabberd.im/developer/extending-ejabberd/modules/#mod-hello-world), however, I cannot get it to work for MongooseIM. Are there any instructions for how to do this? As a beginner I am just looking for building blocks to creating my own modules, and everything I look at is a little too complex for what I am trying to achieve at the moment.

Here is the code for my module: (taken from ejabberd) https://docs.ejabberd.im/developer/extending-ejabberd/modules/#mod-hello-world

screen shot 2018-07-11 at 9 06 00 am

And here is my log error: screen shot 2018-07-11 at 9 17 08 am

I have added the following line in my config file with all other running modules: {mod_hello_world, []}

I am assuming it has something to do with the compilation and there being no .beam file created for the modules as well as some syntax errors specific to MongooseIM. I am also unfamiliar with documentation for compiling modules when using a pre-built pkg as opposed to installing from source.

GalaxyGorilla commented 6 years ago

Here is a minimum working custom module:

-module(mod_hello_world).

-behavior(gen_mod).

-export([start/2, stop/1]).

-include_lib("mongooseim/include/mongoose.hrl").

start(_Host, _Opts) ->
    ?INFO_MSG("Hello!", []).

stop(_Host) ->
    ?INFO_MSG("Bye!", []).

If this is not working and you get the error message you already got then the beam file was surely not found. Also MongooseIM is not really ejabberd ;)) - you should take the dedicated documentation into consideration.

vkatsuba commented 6 years ago

Hi, you try use not exist .hrl. So, remove -include("logger.hrl"). And for first time try to use io:format, by eg:

-module(mod_hello_world).

-behaviour(gen_mod).

%% gen_mod API callbacks
-export([start/2, stop/1]).

start(_Host, _Opts) ->
  io:format("Hello~n"),
  ok.

stop(_Host) ->
  io:format("Bye~n"),
  ok.

And add this module into config file in section

%% Modules enabled in all ejabberd virtual hosts.
%% For list of possible modules options, check documentation.
%%
{modules, 
... ,
{mod_hello_world, []}

P.S. Don't forget do

$ make clean && make rel
areno97 commented 6 years ago

@vkatsuba what command should I run if I have installed from a package? The make command is stopped

areno97 commented 6 years ago

@GalaxyGorilla I still receive the same error even after making these changes. I don't think my code is compiling but I am not exactly sure why

GalaxyGorilla commented 6 years ago

Whoops I didn't realize that you use the package. Not sure how using custom modules is possible with the package, I guess it is not possible at all because the module is simply not compiled :P. Why not try with the source code here? Put your module in /src, try make rel and check what happens ...

See https://github.com/esl/MongooseIM/blob/master/doc/user-guide/How-to-build.md

areno97 commented 6 years ago

@GalaxyGorilla That's what I tried but the make rel command is stopped with the error "No rule to make target rel. Stop."

GalaxyGorilla commented 6 years ago

Execute

git clone https://github.com/esl/MongooseIM.git && cd MongooseIM && make rel

This will probably result in an error. Just check the link I provided before :).

areno97 commented 6 years ago

@GalaxyGorilla got it, thanks. I will try this and get back to you

areno97 commented 6 years ago

@GalaxyGorilla I have done everything described above and mongooseim runs properly after compiling the module (and I am able to locate the beam file), however, I would like it to print "hello world" to the log file. Is there additional code I need to add in order to do this? Or should I be looking somewhere other than the ejabberd.log file for the printing of "hello world" and "bye" with the starting and stopping of the node?

GalaxyGorilla commented 6 years ago

@allireno You looked for _build/prod/rel/mongooseim/log/ejabberd.log ?

Maybe your logs were dropped because the logger framework has a builtin rate limiter, you can set the option error_logger_hwm to something like 500 in _build/prod/rel/mongooseim/etc/app.config to test this.

areno97 commented 6 years ago

@GalaxyGorilla even after making this change, the string still does not show up in the log. I am looking in _build/prod/rel/mongooseim/log/ejabberd.log

GalaxyGorilla commented 6 years ago

@allireno can you check in your ejabberd.cfg that the log level is 4 (and not e.g. 3)? I just checked again with MIM 3.0.0 and I noticed that the log level is on warning (3) by default. The thing is, there is a special log level for file logs in the app.config, but that one is overwritten on app start by the ejabberd.cfg log severity.

areno97 commented 6 years ago

Changing the log level and using the code for the minimum custom module you gave above allowed it to work. Thank you!