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?

2 Likes

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

3 Likes

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

1 Like

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
4 Likes