tcoopman
Create a new module at runtime based on existing code and save it in database
So I have the following use case.
I have some code / modules that I want to run different versions of.
Let’s say I have a user, when they sign up - they interact with some code v0.
I now want to deploy new changes to the that code, but this first user should still interact with the code of v0. Another user with v1 and maybe some other on v2.
At some point in time the user could decide to update to v1 or v2, but it’s their decision.
I was thinking, if I could do something like:
Module.create(ModuleVersionX, module_contents, ...)
bytecode = capture_bytecode_of(ModuleVersionX)
save_bytecode_to database
// later
bytecode = capture_bytecode_from_db()
reload_module(bytecode)
My specific questions:
- how do I specify
module_contentsbased on an existing compiled module? - I think I can use
@after_compileto capture the bytecode of this new module? If I’m able to add this@after_compilehook in themodule_contents - I’m not sure how to load the bytecode again from the database.
- What are the catches of an approach like this?
I found the following topic that might be interesting, but the link in there doesn’t work anymore: Any ideas on how to generate compile-time module based on other modules?
Marked As Solved
tcoopman
I think I found something that can actually help me fix this.
It’s not exactly what I’m looking for but it gets me a lot closer I believe: The horus application
From the readme:
Horus is a library that extracts an anonymous function’s code as well as the code of the all the functions it calls, and creates a standalone version of it in a new module at runtime.
The goal is to have a storable and transferable function which does not depend on the availability of the modules that defined it or were called.
Also Liked
LostKobrakai
You need to enforce unique names and figure out how to make the correct one be called. This is also effectively a remote code execution attack vector, so you want to take good care of who is able to affect those modules.
If there’s no hard need for elixir you can consider luerl / lua (Lua — Lua v0.4.0) instead, which would allow you to retain the ability to code up logical parts, while being able to be fully sandboxed.
If this happens to be around templating then I’d suggest GitHub - edgurgel/solid: Liquid template engine in Elixir · GitHub, which would be even more focused.
axelson
I have only minimal experience with this but I think you’d use a function like :code.get_object_code/1 to get the binary representation of a module :code.load_binary/3. Although if you’re running on IEx then you could get the binary code directly from the defmodule result: {:module, _mode, binary, _} = defmodule Bench do def run do 42 end end and use Code.eval_string to start from a raw string:
{{:module, _mode, binary, _}, _} = Code.eval_string("defmodule Bench do def run do 42 end end")
Also I’d recommend storing the source code representation alongside the compiled binary. That will likely be necessary for checking for abuse, and also if you upgrade your Elixir/Erlang version.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









