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?

Most Liked

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

You might use @external_resource attribute on a module…

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

https://hexdocs.pm/elixir/1.12/Module.html

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
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
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement