Events for loaded modules in erlang - erlang

How can I get an event when a module has been loaded in erlang?
The "event" can be in the form of execution of a fun/MFA, message to arbitrary process etc. If there is no support in the standard lib what is the best work around?
The workarounds I can think of are:
run a trace on the code module
scan loaded module times in a loop
An additional restriction is that there shouldn't have to be special hooks in the module being loaded because this is for a debugging app. Standard library modules are not expected to be reloaded and it is assumed the tools application is running.

There is -on_load() module directive. You can set some function to run when module is loaded using this directive. See more information in documentation.

Related

How to use the erlang-server stubs from OpenAPI generator?

I am currently trying to develop a REST API using Erlang. I used OpenAPI (Swagger) to define the API and wanted to use their openapi-generator to create the stubs and some helper code. This also works and some code is generated, but I cannot find any documentation on what I have to do from there.
For the explanation of my problem, I will refer to their sample output from code generation for erlang-server.
I have two main problems at the moment:
I cannot find an entry point into the code (e.g. a init/0 or start/0 function)
I am not sure which function I have to implement to execute my code on each endpoint. I think that I have to implement handle_request, but the execution path seems a bit weird to me.
Which function to implement
In openapi_router they define openapi_pet_handler to be the handler for 'AddPet'. In that module, there is a function called handle_request_json, which tries to populate the request and in case of OK executes openapi_logic_handler:handle_request(LogicHandler, ...).
openapi_logic_handler:handle_request(Handler, ...) takes that handler and executes Handler:handle_request(OperationID, Req, Context).. This leads me to the conclusion, that I probably have to implement openapi_pet_handler:handle_request with my custom code.
How to start the server
I checked the code and it seems to use cowboy as a web server. At least it includes dependencies to cowboy in the generated openapi.app.src. However, in rebar.config there is no reference to cowboy.
In the cowboy tutorial, they use erlang.mk to build their project. This allows them to run the code with make run. With rebar this does not seem to be possible. I did not find a specific command for rebar to start a program. You seem to have to know the entrypoint (init function) if you use rebar.
However, I am not able to find any function that looks like it could be an init function in the generated stub code.
Summary
Can somebody explain what has to be done to use the stub generated by openapi-generator for erlang-server? Do I have to setup my own cowboy project and then somehow link the stubs into it? If yes, how?

How can I load a lua library in rc.lua for later use by awesome-client?

For background, I'm currently using notify-send to produce dbus notifications when a long script finishes but I'd like to make better notifications with naughty.
I found this simple script https://gist.github.com/lgaggini/51a35f363b7f1966971e869f3fbb5335 for sending naughty notifications using awesome-client. It initializes the naughty library every time it runs, which seems messy since it only needs to be loaded once per session.
In my rc.lua, I tried changing the line
local naughty=require("naughty")
remove local: still needed to require naughty through a lua prompt or awesome-client after restarting awesome
add global: that's not actually a keyword in lua so it breaks my config
Is there something I can do here or elsewhere in my rc.lua that will make this (or any other) library available to awesome-client without an extra step?

How to write a vendor programme/module for ZFT

I would like to write a Zend Framework 2 vendor module, hosted on github, installable via composer and given to the world at large!
Now while I have no problems writing modules, where I am struggling is the mechanics around this.
My initial instinct would be to do something like this:
Set up a zend framework 2 skeleton app
Add my module as a normal module
Navigate to the module folder and create a git repo
Work on the module and update the module to my gitto...
Now logic tells me this is not the right way to go. So I am thinking, maybe I write the module as a stand alone outside of the skeleton app, push to the gito, and then pull from the gito to a working applicaiton...
If you can direct me to a tutorial, please let me know or if you can confirm or deny my proposed thinking that would be great too.
You probably need some app around your module to check if it works, however that shouldn't affect the way you create that module.
When it comes to Zend Framework 2 integrating modules, all you really need is a class that is called SomeNameSpace\Module which must be autoloadable via Composer. Note that there is NO requirement to place this file at a certain location. ZF2 will detect that you are using it with Composer and simply check with class_exists() instead of trying to locate a file, include it and then check for the class.
This class should reveal some configuration info about your module in the way ZF2 expects modules to do this, i.e. add bootstrap event listeners, return configuration data via getConfig() etc. It need not return anything for getAutoloaderConfig(), because that's what Composer is for.
To add the module to the ZF2 application you add the SomeNameSpace name to the file config/application.config.php:
return array(
'modules' => array(
'OtherStuffForTheDemoApp',
'SomeNameSpace' # this enables your module in your demo app
# and anywhere else where it's being used
),
ZF2 will see the module mentioned, try to instantiate SomeNameSpace\Module, ask it about all the configuration you defined, possibly calling the hook functions like onBootstrap() you provided there - and that's about it. Your module is responsible for doing the rest, i.e. provide a service manager configuration, provide controllers etc. All classes are autoloaded by Composer.
I believe the question of how to expose resources like images of a module hasn't been answered by Zend itself - at least I saw these questions being raised, but unanswered in the most current version of the documentation: http://framework.zend.com/manual/current/en/modules/zend.module-manager.intro.html
The organisation of files inside your module is completely up to you, although it seems common practice to place the source in a folder named src, tests probably go into tests.

How can I load code from another Dart package, known only at runtime?

I am building a Dart application. It needs to load code from a third-party package, which is only known at runtime. My application needs to:
auto-discover the dependency
load a library from that dependency
interact with the dependency
Ideally, I do not want to require that my users specify the third-party dependency. The application should auto-discover the dependency.
For example, a workflow could be something like this:
User installs my app (pub global activate my_app)
User installs a "plugin" (pub global activate plugin_for_my_app)
User runs my app (my_app)
The app auto-discovers that plugin_for_my_app exists.
The app loads the plugin (via spawnUri perhaps?)
The app calls into the plugin
Requirements:
Must run from the command-line.
Must work on Windows, Mac, Linux.
Should (but doesn't have to) run when compiled to JavaScript.
pub run support is optional (pub run makes it tricky, because it rewrites your import URIs, so it's not a requirement)
What's the best way to do this?
This package https://pub.dartlang.org/packages/plugins seems to do exactly what you want (haven't used it myself yet though) by loading plugins into isolates.
This is not directly answering the question (CL tools as requested), but I use plugins in the browser and wanted to share my "pattern".
My web application imports includes.dart which is a dynamically generated file that imports all .dart files found in a specified directory. The file gets (re-)generated at app startup by the backend, just before serving the files to the browser. The found .dart files implement a public api (eg. init() and run()) so that the main application can call their code. The plugin code are not loaded into separate isolates but are executed in the same isolate as the main app which gives the benefit of plugins sharing the same heap and you're able to access the plugin code directly. This solution also assumes the plugins resolve their own dependencies.
This setup works fairly well for my use case. However, as there's no real dynamic code reloading in Dart (yet, I hope. see https://code.google.com/p/dart/issues/detail?id=10530), there's always a refresh step needed to load the new code.
In the plugins package, currently, there is no way to resolve dependencies through pub. When I originally designed the API it was assumed that the dependencies were already retrieved through pub get. With that said, plugin discovery on the file system is simple.
As you can see in the example in the README, loading plugins was as simple as new PluginManager().loadAll(String directory) which would automatically discover all plugins inside the directory and load them. This solution is ideal if all plugins are to be in one folder.
It is possible to do individual plugin directory loading as well, provided it follows the plugin directory structure. Using a PluginLoader you can load in a directory that contains the necessary files for a plugin to run properly. It is not necessary to call load() since the PluginManager will take care of calling in load for you.
A simplified way of loading a plugin
Instantiate the PluginManager.
PluginManager pm = new PluginManager();
Determine the plugin you want loaded. The plugins library will automatically
determine the pub cache directory. As per the documentation of pub, the PUB_CACHE environment variable is also supported.
Load in the plugin. A Future is returned with a Plugin object that provides basic information about the plugin. The plugin requires a pubspec.yaml with a name, a packages directory, and a bin/main.dart source file. However we are loading from the pub cache, so we do not need to worry about the setup of the plugin, the only requirement is the package from the pub cache supports the plugins package.
pm.loadFromCache("test-1.0.0").then((Plugin plugin) {
print("Plugin loaded!");
handle();
});
After all the plugins you desire to be loaded are initialized, the manager can now listen for requests properly. Simply use the listener to 'see' the incoming data.
The plugin side
The plugin simply uses a receiver as provided by the plugins API.
void main(List<String> args, SendPort port) {
Receiver rec = new Receiver(port);
rec.listen((Map<dynamic, dynamic> data) {
print("Received data: $data");
});
}
I wrapped the plugins package with a bit of sugar to provide some extra things like declarative RPC. It's very flexible, and samrg472 (the author of plugins.dart) is a good friend, so I have asked him to comment as well.
https://github.com/PolymorphicBot/PolymorphicBot/blob/master/lib/src/core/plugins/handler.dart

Using modules in Deployd

I have started to use Deployd. It works great. However, I need to use other node modules and I am running into problems - how am I supposed to use functionality from other node modules in my Deployd app?
For instance, I am using Paymill.com for credit card payments and Paymill provides a node module for using their API. How can I use their module in my app? I have tried to load the module (using require("name-of-the-module")) in the event scripts in the dashboard but it fails becaus require() is not defined.
What is the preferred solution?
Deployd has its own module system:
http://docs.deployd.com/docs/developing-modules/creating-modules.md
Building a paymill module for deployd would definitively benefit the community :)
There's some possibilities to have require() work in the event script, but it's not a very clean approach.

Resources