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:
- define a path for ALL custom modules in Application.start
Code.append_path(Application.get_env(:bothost, :modules))
-
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?