CharlesO

CharlesO

Dynamically compile and load modules

Is it possible to have Elixir modules compiled and loaded at run time dynamically?

For example, say I have Elixir modules with custom logic for specific customers.

I need to load / call functions from these modules at runtime based on inbound requests.

What i have tried:

  1. define a path for ALL custom modules in Application.start

Code.append_path(Application.get_env(:bothost, :modules))

  1. check if a given module is loaded; if not, compile/ load it, then call an expected entry point function on the module:

    defp process([{, %BotHost.BotInfo{module: module} = bot}], req) when module == nil, do: BotHost.Processor.process(bot, req) # use generic processor
    defp process([{
    , %BotHost.BotInfo{module: module} = bot}], req) do
    case Code.ensure_compiled(module) do
    {:module, _module} → apply(module, :process, [bot, req])
    {:error, err} → log “load failed: #{module} #{err}” ; BotHost.Processor.process(bot, req) # unable to load custom processor, use generic processor instead
    end
    end

Please can we use the functions from the “Code” module for this purpose?

Marked As Solved

bryanjos

bryanjos

Maybe Code.require_file or Kernel.ParallelCompiler.files will do what you need?

Also Liked

CharlesO

CharlesO

Your suggestion of Code.require_file/2 works.

  defp process([{_, %BotHost.BotInfo{module: module} = bot}], req) when module == nil, do: BotHost.Processor.process(bot, req) # use generic processor
  defp process([{_, %BotHost.BotInfo{module: module, module_file: file} = bot}], req) do
    case Code.require_file(file, "modules") do
      nil -> apply(module, :process, [bot, req])
      [{module,  _}] -> apply(module, :process, [bot, req])
      err -> log "load failed: #{module} - #{inspect err}" ; BotHost.Processor.process(bot, req) # unable to load bot-specific module, use generic processor instead
    end
  end
CharlesO

CharlesO

Thanks for the reply. I’ll keep investigating the options.

Right now i’m looking at this:

to understand what others have tried. Unfortunately there seems to be very little information on what i’m trying to do out there

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New

We're in Beta

About us Mission Statement