dmitriid

dmitriid

Create a module in memory, refresh/reload on change

It’s one of those “if you try it, you can figure it out” questions. I’m just hoping someone has already done this before and has some quick tips or a link to look at :slight_smile:

The meat of the question is: I have something in a file (data, or a custom DSL, or anything, really). Using this something, I want to create and compile an Elixir module and load it into the running application. When that something changes (let’s say, a file watcher tells me that contents have changed), I want to re-create/re-compile that module and reload it.

I know that I should start looking at Module.create/3. Perhaps there are other things I can/should look at and consider?

First 5 of 5 Posts Switch mode

kokolegorille

kokolegorille

You might use @external_resource attribute on a module…

If the external resource file change, the module will recompile.

03juan

03juan

You could try Code.eval_string/3 if you want to parse the changed “something” file from within Elixir to produce a string representation of the module def.

Alternatively Code.eval_file/2 if you dynamically write/update an .ex file from your “something” file.

mudasobwa

mudasobwa

Creator of Cure

This is compilation-time dependency, which helps to mark a module as “recompilation needed” for the regular elixir compiler when say mix compile is to be invoked next time. It has zero value here, unfortunately.

Yes, kinda. There is a pitfall one should be aware of: the module recompilation is not instant.

I would spawn the process as gateway access to the module to be recompiled that both recompiles whatever is needed and its mailbox acts as a queue for both subsequent recompilations and all the interactions with this module. That way one can be sure that the module gets accessed if and only it’s available.

Please note, that Module.create/3 accepts an AST. If you want to load a regular .ex[s] file, use Code.eval_file/2 as suggested by @03juan.

kokolegorille

kokolegorille

Oops, my bad… I did not catch it should be for runtime.

dmitriid

dmitriid OP

Good point/catch about the queue. Especially if I want this to be based on a custom DSL or custom data, compilation will definitely not be instant.

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement