tcoopman

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:

  1. how do I specify module_contents based on an existing compiled module?
  2. I think I can use @after_compile to capture the bytecode of this new module? If I’m able to add this @after_compile hook in the module_contents
  3. I’m not sure how to load the bytecode again from the database.
  4. 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

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.

Found it here: Horus - extract an anonymous function as a standalone module - #7 by dumbbell - Libraries - Erlang Programming Language Forum - Erlang Forums

Also Liked

LostKobrakai

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.0.20) 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, which would be even more focused.

axelson

axelson

Scenic Core Team

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.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement